draw.js
13.0 KB
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
(function () {
//画布
var canvas ;
var context ;
//蒙版
var canvas_bak;
var context_bak;
var canvasWidth = 880;
var canvasHeight = 718;
var recordPencilResult={};
var recordPencilObj={};
var recordPencilTotal=[];
var recordPencilId;
var CanvasConInfo;
if (document.documentElement.clientWidth>1366){
canvasWidth = 880;
canvasHeight = 718;
}else if(document.documentElement.clientWidth<=1366&&document.documentElement.clientWidth>1280){
canvasWidth = 626;
canvasHeight = 700;
}else if (document.documentElement.clientWidth<=1280){
canvasWidth = 581;
canvasHeight = 700;
}
function xyToPer(x,y){
var recordPencilObject=new Object();
recordPencilObject.w=parseInt(x/canvasWidth*10000)/100;
recordPencilObject.h=parseInt(y/canvasHeight*10000)/100;
console.log("xyToPer +++++++++++++++++++++++++++++++++++++++++++++++++");
console.log(recordPencilObject);
return recordPencilObject;
}
var canvasTop;
var canvasLeft;
//画笔大小
var size = 1;
var color = '#b8242a';
//画图形
function draw_graph(graphType,obj){
//把蒙版放于画板上面
$("#canvas_bak").css("z-index",1);
//先画在蒙版上 再复制到画布上
chooseImg(obj);
var canDraw = false;
var startX;
var startY;
//鼠标按下获取 开始xy开始画图
var mousedown = function(e){
scroolTop = $(window).scrollTop();
scroolLeft = $(window).scrollLeft();
canvasTop = $(canvas).offset().top - scroolTop;
canvasLeft = $(canvas).offset().left - scroolLeft;
context.strokeStyle= $('#chooseColor')[0].title;
context_bak.strokeStyle= $('#chooseColor')[0].title;
context_bak.lineWidth = size;
startX = e.clientX - canvasLeft;
startY = e.clientY - canvasTop;
context_bak.moveTo(startX ,startY );
canDraw = true;
if(graphType == 'pencil'){
context_bak.beginPath();
// console.log("=======================================++++++++++++++++++++++++++++++++++");
}else if(graphType == 'circle'){
context.beginPath();
context.moveTo(startX ,startY );
context.lineTo(startX +1 ,startY+1);
context.stroke();
}else if(graphType == 'rubber'){
// context.clearRect(startX - size * 10 , startY - size * 10 , size * 20 , size * 20);
}
// 阻止点击时的cursor的变化,draw
e=e||window.event;
e.preventDefault();
};
//鼠标离开 把蒙版canvas的图片生成到canvas中
var mouseup = function(e){
e=e||window.event;
canDraw = false;
console.log("鼠标松开===================================================");
recordPencilResult.type=0;
recordPencilResult.curPage=1;
recordPencilResult.parentId=0;
recordPencilResult.pointGroup=recordPencilTotal;
recordPencilResult.color=$('#chooseColor')[0].title;
recordPencilResult.thickness=size;
console.log("发送canvas绘图=================================");
console.log(recordPencilResult);
client.sendInsertAnnotaion(recordPencilResult);
if (canDraw = false){
recordPencilTotal.splice(0,recordPencilTotal.length);
}else{
recordPencilTotal=[];
}
console.log("当前画笔--------------------------");
console.log(recordPencilTotal);
};
//选择功能按钮 修改样式
function chooseImg(obj){
var imgAry = $(".draw_controller li");
for(var i=0;i<imgAry.length;i++){
$(imgAry[i]).removeClass('active');
$(imgAry[i]).addClass('normal');
}
$(obj).removeClass("normal");
$(obj).addClass("active");
}
// 鼠标移动
var mousemove = function(e){
scroolTop = $(window).scrollTop();
scroolLeft = $(window).scrollLeft();
canvasTop = $(canvas).offset().top - scroolTop;
canvasLeft = $(canvas).offset().left - scroolLeft;
e=e||window.event;
var x = e.clientX - canvasLeft;
var y = e.clientY - canvasTop;
//方块 4条直线搞定
if(graphType == 'square'){
if(canDraw){
context_bak.beginPath();
clearContext();
context_bak.moveTo(startX , startY);
context_bak.lineTo(x ,startY );
context_bak.lineTo(x ,y );
context_bak.lineTo(startX ,y );
context_bak.lineTo(startX ,startY );
context_bak.stroke();
}
//直线
}else if(graphType =='line'){
if(canDraw){
context_bak.beginPath();
clearContext();
context_bak.moveTo(startX , startY);
context_bak.lineTo(x ,y );
context_bak.stroke();
console.log("--------------------------------------------------------------");
console.log(startX,startY);
console.log(x,y);
var lineData={};
var arr= new Array();
var start={};
start.x=startX;
start.y=startY;
var end={};
end.x=x;
end.y=y;
lineData.type=0;
lineData.color=$('#chooseColor')[0].title;
arr.unshift(start,end);
lineData.pointGroup=arr;
console.log("2222222222222222222222222222222222222222");
console.log(lineData);
}
//画笔
}else if(graphType == 'pencil'){
if(canDraw){
context_bak.lineTo(Math.round(e.clientX-canvasLeft),Math.round(e.clientY-canvasTop));
context_bak.stroke();
// console.log("铅笔工具开始----------------------->");
var mouseX=e.offsetX;
var mouseY=e.offsetY;
console.log("---------------------------------------------------");
console.log(Math.round(e.clientX-canvasLeft),Math.round(e.clientY-canvasLeft))
console.log(mouseX,mouseY);
recordPencilObj=xyToPer(mouseX,mouseY);
recordPencilTotal.push(recordPencilObj);
}
//圆 未画得时候 出现一个小圆
}else if(graphType == 'circle'){
clearContext();
if(canDraw){
// 鼠标点击移动时产生的圆
context_bak.beginPath();
var radii = Math.sqrt((startX - x) * (startX - x) + (startY - y) * (startY - y));
context_bak.arc(startX,startY,radii,0,Math.PI * 2,false);
context_bak.stroke();
}else{
context_bak.beginPath();
context_bak.arc(x,y,20,0,Math.PI * 2,false);
context_bak.stroke();
}
//涂鸦 未画得时候 出现一个小圆
}else if(graphType == 'handwriting'){
if(canDraw){
// 鼠标点击移动产生的圆圈
context_bak.beginPath();
context_bak.strokeStyle= $('#chooseColor')[0].title;
context_bak.fillStyle= $('#chooseColor')[0].title;
//计算当前点和上一个点的距离
var tmpX = x - startX;
var tmpY = y - startY;
var dist = Math.sqrt(Math.pow(tmpX, 2) + Math.pow(tmpY, 2));
dist = Math.round(dist);
//定义递增点
var ix = tmpX / dist;
var iy = tmpY / dist;
//定义绘制点
var currX = startX;
var currY = startY;
for(var i = 0; i < dist; i++) {
context_bak.arc(currX, currY, size * 10, 0, Math.PI * 2, false);
currX += ix;
currY += iy;
}
context_bak.fill();
context_bak.stroke();
context_bak.restore();
//保存上一次的点
startX = x;
startY = y;
}else{
clearContext();
context_bak.beginPath();
context_bak.strokeStyle = color;
context_bak.fillStyle = color;
context_bak.arc(x,y,size*10,0,Math.PI * 2,false);
context_bak.fill();
context_bak.stroke();
}
//橡皮擦 不管有没有在画都出现小方块 按下鼠标 开始清空区域
}else if(graphType == 'rubber'){
// context_bak.lineWidth = 1;
// clearContext();
// context_bak.beginPath();
// context_bak.strokeStyle = '#000000';
// context_bak.moveTo(x - size * 10 , y - size * 10 );
// context_bak.lineTo(x + size * 10 , y - size * 10 );
// context_bak.lineTo(x + size * 10 , y + size * 10 );
// context_bak.lineTo(x - size * 10 , y + size * 10 );
// context_bak.lineTo(x - size * 10 , y - size * 10 );
// context_bak.stroke();
// if(canDraw){
// context.clearRect(x - size * 10 , y - size * 10 , size * 20 , size * 20);
//
// }
}
};
//鼠标离开区域以外 除了涂鸦 都清空
var mouseout = function(){
if(graphType != 'pencil'){
console.log("清屏-------------------------------------------------")
clearContext();
}
}
$(canvas_bak).unbind();
$(canvas_bak).bind('mousedown',mousedown);
$(canvas_bak).bind('mousemove',mousemove);
$(canvas_bak).bind('mouseup',mouseup);
$(canvas_bak).bind('mouseout',mouseout);
}
//绘图
function drawCanvas(canvasInfo) {
if(canvasInfo==null){
return;
}
//1.判断是否清除canvas
if (canvasInfo.isFresh){
clearContextCan('1');
}
//2.绘制标注数据
var canvasMsgArr=canvasInfo.annotaionItems;
console.log(canvasMsgArr.length);
if (canvasMsgArr&&canvasMsgArr.length>0){
for (var i=0;i<canvasMsgArr.length;i++){
var canvasMsgItem=canvasMsgArr[i];
if(canvasMsgItem){
switch (canvasMsgItem.type){
//pencil
case 0:
draw_pencil(canvasMsgItem);
break;
case 1:
//draw_pencil(canvasMsgItem);
break;
case 2:
//draw_pencil(canvasMsgItem);
break;
case 3:
//draw_pencil(canvasMsgItem);
break;
case 4:
//draw_pencil(canvasMsgItem);
break;
default:
break;
}
}
}
}
}
//只绘制铅笔
function draw_pencil(_canvasMsgItem) {
if(_canvasMsgItem){
context_bak.beginPath();
context_bak.lineWidth = size;
context_bak.color=String(_canvasMsgItem.color);
context.strokeStyle= String(_canvasMsgItem.color);
context_bak.strokeStyle= String(_canvasMsgItem.color);
var point=_canvasMsgItem.pointGroup[0];
context_bak.moveTo((point.w)*canvasWidth/100,(point.h)*canvasHeight/100);
for (var j=1;j<_canvasMsgItem.pointGroup.length;j++){
point=_canvasMsgItem.pointGroup[j];
context_bak.lineTo((point.w)*canvasWidth/100,(point.h)*canvasHeight/100);
}
context_bak.stroke();
}
}
function clearContext(type){
client.sendDeleteAllAnnotation();
// if(!type){
// context_bak.clearRect(0,0,canvasWidth,canvasHeight);
// console.log("执行清空层------------------------------1");
// clearCanvasCon(CanvasConInfo);
// // clearCanvasCon(CanvasConInfoTran);
//
//
// }else{
// context.clearRect(0,0,canvasWidth,canvasHeight);
// context_bak.clearRect(0,0,canvasWidth,canvasHeight);
// console.log("执行清空层------------------------------2");
// clearCanvasCon(CanvasConInfo);
//
// }
}
//清空层
function clearContextCan(type){
if(!type){
context_bak.clearRect(0,0,canvasWidth,canvasHeight);
}else{
context.clearRect(0,0,canvasWidth,canvasHeight);
context_bak.clearRect(0,0,canvasWidth,canvasHeight);
}
}
function clearDocCanvas() {
console.log("清空Doc---canvas");
var docCanvas=document.getElementById("docCanvas");
var ctxC=docCanvas.getContext("2d");
ctxC.clearRect(0,0,docCanvas.style.width,docCanvas.style.height);
console.log("已清空=====>");
}
//初始化
function initCanvas() {
canvas = document.getElementById("canvas");
canvas.width = canvasWidth;
canvas.height = canvasHeight;
context = canvas.getContext('2d');
canvasTop = $(canvas).offset().top;
canvasLeft = $(canvas).offset().left;
canvas_bak = document.getElementById("canvas_bak");
canvas_bak.width = canvasWidth;
canvas_bak.height = canvasHeight;
context_bak = canvas_bak.getContext('2d');
console.log("initCanvas-----------------------------",canvas_bak.height);
}
function clearCanvasCon(msg) {
console.log("要清空了============================================");
console.log(msg);
var paramInfo={};
for(var i=0;i<msg.length;i++){
paramInfo.itemIdx=msg[i];
console.log("遍历同步的数组");
console.log(paramInfo);
client.sendDeleteAnnotaion(paramInfo)
}
}
function trans(msg) {
CanvasConInfo=msg;
}
// function transInfo(msg) {
// CanvasConInfoTran=msg;
//
// }
window.draw={
initCanvas:initCanvas,
draw_graph:draw_graph,
clearContext:clearContext,
recordPencilResult:recordPencilResult,
drawCanvas:drawCanvas,
clearCanvasCon:clearCanvasCon,
trans:trans,
clearContextCan:clearContextCan,
clearDocCanvas:clearDocCanvas
};
})();