-
Notifications
You must be signed in to change notification settings - Fork 2
/
MyBoard.html
439 lines (429 loc) · 16.1 KB
/
MyBoard.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
<title>MyBoard</title>
<style>
body,div,canvas {
margin:0;
padding: 0;
-moz-user-select:none; /*火狐*/
-webkit-user-select:none; /*webkit浏览器*/
-ms-user-select:none; /*IE10*/
-khtml-user-select:none; /*早期浏览器*/
user-select:none;
}
#bigMain{
width: 500px;
height: auto;
margin: 10px auto;
}
#container{
width: 100%;
height: 500px;
border: 3px solid red;
box-shadow: 3px 3px 3px gray;
position: relative;
}
#canvas_01{
width: 100%;
height: 100%;
display: block;
background-color: #e4e4e4;
position: absolute;
top: 0;
left: 0;
z-index: 333;
}
#canvas_02{
width: 100%;
height: 100%;
display: block;
position: absolute;
top: 0;
left: 0;
z-index: 666;
cursor: url('./Images/brush-tool.ico'),pointer;
}
#LWcontainer{
width: 100%;
height: 30px;
background-color: transparent;
position: relative;
cursor: pointer;
overflow: hidden;
}
#LWC{
width: 0;
height:0;
border-left: 500px solid transparent;
border-bottom: 30px solid gray;
overflow: hidden;
position: absolute;
bottom: 0;
}
#LWCtool{
width: 0;
height:0;
border-left: 200px solid transparent;
border-bottom: 12px solid black;
overflow: hidden;
position: absolute;
bottom: 0;
}
#colorCoontainer{
width: 100%;
height: 40px;
}
#red{
background-image: linear-gradient(to left,rgb(255,0,0), rgb(0,0,0));
}
#blue{
background-image: linear-gradient(to left,rgb(0,0,255), rgb(0,0,0));
}
#yellow{
background-image: linear-gradient(to left,rgb(0,255,0), rgb(0,0,0));
}
.colorTool{
display: block;
width: 100%;
height: 10px;
margin-top: 3px;
cursor: pointer;
position: relative;
}
.colorMark{
width: 8px;
height: 8px;
border: 1px solid red;
border-radius: 8px;
background:rgba(255,250,250,.7);
box-shadow:0 0 1px rgba(255,255,255,1);
position: absolute;
}
#bot_bot{
display: flex;
width:100%;
height:auto;
}
input{
display: inline-block;
flex-grow: 1;
height: 50px;
cursor: pointer;
font-family: "隶书";
font-size: 30px;
}
#img_box{
display: flex;
flex-flow: wrap;
width:100%;
height:auto;
}
.img_c{
width:100%;
height:auto;
box-shadow:3px 3px 3px grey,0 0 1px grey;
}
</style>
<script>
window.onload=function(){
var startp={x:0,y:0},endp={x:0,y:0},speed= 20,isFinish=true,timer= 0,lastW,sW=window.screen.availWidth,isBegin=false,colorA={r:100,y:200,b:10},dColor="black",w1={x:500,y:500},w2={x:500,y:500};
var container=document.getElementById("container")
,canvas_01=document.getElementById("canvas_01")
,canvas_02=document.getElementById("canvas_02")
,controlTool=document.getElementById("LWcontainer")
,lwc=document.getElementById("LWC")
,tool=document.getElementById("LWCtool")
,bigMain=document.getElementById("bigMain")
,redTool=document.getElementById("red")
,yellowTool=document.getElementById("yellow")
,blueTool=document.getElementById("blue")
,inPut=document.getElementById("input")
,saveBtn=document.getElementById("save_btn");
//点击保存
saveBtn.addEventListener("click",save_pic);
if(sW<500){
var cW=sW*.95;
bigMain.style.width=cW+"px";
container.style.height=canvas_01.offsetWidth+"px";
lwc.style.borderLeftWidth=cW+"px";
tool.style.borderLeftWidth=cW*2/5+"px";
canvas_01.width=cW;
canvas_01.height=cW;
canvas_02.width=cW;
canvas_02.height=cW;
// saveBtn.addEventListener("touchstart",save_pic);
document.addEventListener("touchmove",function(event){
// event.preventDefault();
if(isFinish==true){return;}
endp.x=event.touches[0].clientX-(sW-cW)/2;
endp.y=event.touches[0].clientY-container.offsetTop;
drawLine();
});
//调用函数获取坐标
document.addEventListener("touchstart",function(event){
// event.preventDefault();
startp.x=event.touches[0].clientX-(sW-cW)/2;
startp.y=event.touches[0].clientY-container.offsetTop;
timer=new Date().getTime();
return isFinish=false;
});
//开始画线
document.addEventListener("touchend",function(){
// event.preventDefault();
return isFinish=false;
});
w1.x=w1.y=w2.x=w2.y=cW;
//控制笔触大小
controlTool.addEventListener("touchmove",function(event){
event.preventDefault();
var x= event.touches[0].clientX-(sW-cW)/2;
console.log(event.touches[0].clientX);
tool.style.borderLeftWidth=x+"px";
tool.style.borderBottomWidth=x*30/cW+"px";
speed=x/10;
isFinish=true;
return isBegin=true;
});
controlTool.addEventListener("touchstart",function(event){
if(isBegin==true){
event.preventDefault();
var x= event.touches[0].clientX-(sW-cW)/2;
tool.style.borderLeftWidth=x+"px";
tool.style.borderBottomWidth=x*30/cW+"px";
speed=x/10;
isFinish=true;
}});
controlTool.addEventListener("touchend",function(){
isFinish=false;
return isBegin=false;
});
//刷新
inPut.addEventListener("touchstart",newDraw);
//颜色选择
redTool.addEventListener("touchstart",function(event){
event.preventDefault();
var x=event.touches[0].clientX-(sW-cW)/2;
redTool.querySelector('.colorMark').style.left=x+"px";
colorA.r=x*255/cW;pColor();
});
blueTool.addEventListener("touchstart",function(event){
event.preventDefault();
var x=event.touches[0].clientX-(sW-cW)/2;
blueTool.querySelector('.colorMark').style.left=x+"px";
colorA.b=x*255/cW;
pColor();
});
yellowTool.addEventListener("touchstart",function(event){
event.preventDefault();
var x=event.touches[0].clientX-(sW-cW)/2;
yellowTool.querySelector('.colorMark').style.left=x+"px";
colorA.y=x*255/cW;pColor();
});
}
canvas_02.addEventListener("mousemove",function(event){
Coordinate(endp);
drawLine()
});
//调用函数获取坐标
canvas_02.addEventListener("mousedown",function(event){
Coordinate(startp);
timer=new Date().getTime();
return isFinish=false;
});
//开始画线
canvas_02.addEventListener("mouseup",function(){
return isFinish=true;
});
canvas_02.addEventListener("mouseout",function(){
return isFinish=true;
});
drawBoard();
//控制笔触大小
controlTool.addEventListener("mousedown",function(event){
event.preventDefault();
var e=event||window.event;
var x= e.clientX-(document.body.clientWidth-500)/2;
tool.style.borderLeftWidth=x+"px";
tool.style.borderBottomWidth=x*30/500+"px";
speed=x/10;
isBegin=true;
});
controlTool.addEventListener("mousemove",function(event){
event.preventDefault();
if(isBegin) {
var e = event || window.event;
var x = e.clientX - (document.body.clientWidth - 500) / 2;
tool.style.borderLeftWidth = x + "px";
tool.style.borderBottomWidth = x * 30 / 500 + "px";
speed = x / 10;
}
});
controlTool.addEventListener("mouseout",function(){isBegin=false;});
controlTool.addEventListener("mouseup",function(){
event.preventDefault();
isBegin=false;
});
//控制颜色选择
redTool.addEventListener("mousedown",function(event){
var e=event||window.event;
x= e.offsetX;
redTool.querySelector('.colorMark').style.left=x+"px";
colorA.r=x*255/500;
pColor();
});
blueTool.addEventListener("mousedown",function(event){
var e=event||window.event;
x= e.offsetX;
blueTool.querySelector('.colorMark').style.left=x+"px";
colorA.b=x*255/500;
pColor();
});
yellowTool.addEventListener("mousedown",function(event){
var e=event||window.event;
x= e.offsetX;
yellowTool.querySelector('.colorMark').style.left=x+"px";
colorA.y=x*255/500;
pColor();
});
//刷新画板
inPut.addEventListener("click",newDraw);
function newDraw(){
canvas_02.width=0;
canvas_02.width=w2.x;
}
//刷新笔色
function pColor(){
var a=Math.round(colorA.r),b=Math.round(colorA.y),c=Math.round(colorA.b);
tool.style.borderBottomColor='rgb('+a+','+b+','+c+')';
dColor="rgb("+a+","+b+","+c+")";
}
//画板定义
function drawBoard(){
var ctx=canvas_01.getContext("2d");
ctx.strokeStyle="rgba(255,0,0,.8)";
ctx.lineWidth=1;
ctx.moveTo(0,0);
ctx.lineTo(w1.x,w1.y);
ctx.moveTo(w1.x,0);
ctx.lineTo(0,w1.y);
ctx.moveTo(w1.x/2,0);
ctx.lineTo(w1.x/2,w1.y);
ctx.moveTo(0,w1.y/2);
ctx.lineTo(w1.x,w1.y/2);
ctx.stroke();
ctx.closePath();
}
//获取坐标
function Coordinate(choicp){
var e=e||window.event;
choicp.x= e.offsetX;
choicp.y= e.offsetY;
return choicp;
}
//画线
function drawLine(){
if(isFinish==true)return;
else{
var ctimer=new Date().getTime();
var ctx2=canvas_02.getContext("2d");
ctx2.lineWidth=Shad(speed,ctimer);
ctx2.strokeStyle=dColor;
ctx2.lineCap="round";
ctx2.lineJoin="round";
ctx2.beginPath();
ctx2.moveTo(startp.x,startp.y);
ctx2.lineTo(endp.x,endp.y);
ctx2.stroke();
ctx2.closePath();
startp.x=endp.x;
startp.y=endp.y;
timer=ctimer;
lastW=ctx2.lineWidth;
}
}
//计算速度
function moveSpeed(sp,ep,tt){
var s=Math.sqrt(Math.pow(ep.x-sp.x,2)+Math.pow(ep.y-sp.y,2));
var t=tt-timer;
var v=s/t;
return v;
}
//判断笔触粗细
function Shad(lineShad,ptt){
var tv=moveSpeed(startp,endp,ptt),
resultW;
if(tv<=0.1){
resultW=lineShad;
}else if(tv>=10){
resultW=1;
}else{
resultW=lineShad-(tv -0.1)/(10-0.1)*(lineShad-1);
}
if(lastW==-1){
return resultW;
}
else{
return lastW*2/3+resultW*1/3;
}
}
//保存
function save_pic() {
var img=document.createElement("img")
,oDiv=document.createElement("div")
,target_save=document.createElement("a")
,target_delect=document.createElement("span")
,img_box=document.getElementById("img_box");
oDiv.style.cssText="min-width:150px;height:auto;position:relative;margin-top:3px;flex:1";
img.src=canvas_02.toDataURL("image/png",1.0);
img.className='img_c';
img.style.flexGrow="1";
target_save.href=img.src;
target_save.setAttribute("download","pic");
target_save.innerHTML="保存";
target_delect.innerHTML="删除";
target_delect.className="delete";
target_save.style.cssText="background-color:rgba(0,0,0,.5);position:absolute;top:0;right:3px;padding:3px;color:#fff;text-decoration:none";
target_delect.style.cssText="position:absolute;top:0;left:3px;cursor:pointer;padding:3px;background-color:rgba(0,0,0,.5);color:#fff";
oDiv.appendChild(target_save);
oDiv.appendChild(target_delect);
oDiv.appendChild(img);
img_box.appendChild(oDiv);
var del=document.querySelectorAll(".delete");
for(var i=0;i<del.length;i++){
del[i].onclick=function () {
//删除父级元素以及父级元素内所有内容
this.parentNode.parentNode.removeChild(this.parentNode);
}
}
}
};
</script>
</head>
<body>
<div id="bigMain">
<div id="LWcontainer">
<div id="LWC"></div>
<div id="LWCtool"></div>
</div>
<div id="container">
<canvas id="canvas_01" width="500" height="500">Your browser cannot use canvas!</canvas>
<canvas id="canvas_02" width="500" height="500" onmousedown="" onmouseup="">Your browser cannot use canvas!</canvas>
</div>
<div id="bottom">
<div id="colorCoontainer">
<span class="colorTool" id="red"><div class="colorMark"></div></span>
<span class="colorTool" id="yellow"><div class="colorMark"></div></span>
<span class="colorTool" id="blue"><div class="colorMark"></div></span>
</div>
<div id="bot_bot">
<input type="button" id="input" value="重绘" >
<input type="button" id="save_btn" value="预览" >
</div>
</div>
<div id="img_box"></div>
</div>
</body>
</html>