-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
539 lines (521 loc) · 16.8 KB
/
index.js
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// 黑板
class ClassBoard {
constructor() {
this.version = '0.0.12'
this.state = {
openFile: false,
beginPoint: {},
menuPen: [],
menus: [
{text: "新建",key: 0, className:"hyfont xinjianyiye"},
{text: "上一页",key: 1,className:"hyfont ketang-shangyiye"},
{text: "下一页", key: 2, className:"hyfont ketang-xiayiye"},
{text: "橡皮",key: 3,className:"hyfont ketang-cachu"},
{text: "清屏", key: 4, className:"hyfont ketang-qingkong"},
{text: "画笔", key: 5, className:"hyfont ketang-huabi"},
{text: "保存",key: 6, className:"hyfont baocun"},
],
baseMenus: [
{text: "画笔", key: 5, className:"hyfont ketang-huabi"},
{text: "红", key: 7, className:"span-icon icon-red"},
{text: "白", key: 8, className:"span-icon icon-white"},
{text: "绿", key: 9, className:"span-icon icon-green"},
{text: "细", key: 10, className:"line-icon icon-fine"},
{text: "中", key: 11, className:"line-icon icon-center"},
{text: "粗", key: 12, className:"line-icon icon-thick"},
{text: "保存",key: 6, className:"hyfont baocun"},
],
bodies: { //点对象
color: 'white', //颜色
courseWareNum: '', //课件页数
drawState: -1, //是否画笔
isBord: 1, //黑板还是课件
isEraser: false,//画笔还是橡皮擦
lastLineId: '', //最后一条线id
lineId: 1, //线id
lineSize: 1, //粗细
pointId: 0,//点id
pointX: 0, //x坐标
pointY: 0, //y坐标
width: 0, //黑板对象 宽
height: 0 //黑板对象 高
},
board: {
canvas: null, //canvas dom
context: {}, //canvas 对象
drawWidth: 1000,
drawHeigth: 500
},
rate:1, //传入的宽高和实际canvas宽高 比
points: [], //一条线的点集合
boardlist: [], //黑板 预览数据
boardIndex: 0, // 黑板数量索引
changeBard: true, // 是否可以新建黑板
}
if((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
this.isMobile = true
} else {
this.isMobile = false
}
}
touchMove () {
const canvas = document.getElementById('canvas')
canvas.addEventListener('touchstart', this.canvasDown.bind(this), false)
canvas.addEventListener('touchmove', this.canvasMove.bind(this), false)
canvas.addEventListener('touchend', this.canvasUp.bind(this), false)
}
initCanvas(){
const canvas = document.getElementById('canvas')
const board = document.getElementById('board')
canvas.setAttribute('class', 'pencil')
canvas.setAttribute('width', board.clientWidth)
canvas.setAttribute('height', board.clientHeight - 80)
const context = canvas.getContext("2d");
context.mozImageSmoothingEnabled = false;
context.webkitImageSmoothingEnabled = false;
context.msImageSmoothingEnabled = false;
context.imageSmoothingEnabled = false;
context.fillStyle = '#333'
context.fillRect(0,0,board.clientWidth,board.clientHeight);
this.state.menuPen = this.state.menus
this.isOpen = false
// context.fillRect(0,0,this.state.board.clientWidth,this.state.board.clientHeight - 90);
Object.assign(this.state.board,{
drawWidth: board.clientWidth, //canvas 容器宽
drawHeigth: board.clientHeight -80, //canvas 容器高
canvas: canvas, //canvas dom
context: context //canvas 对象
})
this.appenedMenus()
}
appenedMenus() {
const hasUl = document.getElementById('boardMenus')
const board = document.getElementById('board')
if (hasUl) {
board.removeChild(hasUl)
}
const {boardIndex, boardlist} = this.state
const menus = document.createElement('ul')
menus.setAttribute('id','boardMenus')
const lis = this.state.menuPen.map(menu => {
let li = document.createElement('li')
let div = document.createElement('div')
let divText = document.createElement('div')
let span = document.createElement('span')
div.className = 'item-icon'
span.className = menu.className
divText.innerText = menu.text
divText.className = 'menu-text'
div.appendChild(span)
li.className = 'item-li'
if (menu.key ===0 && boardlist.length > 9) {
li.className = 'item-li disabled'
}
if (menu.key ===1) {
if (boardlist.length === 0 || boardIndex ===0) {
li.className = 'item-li disabled'
}
}
if (menu.key ===2) {
if(boardlist.length === 0) {
li.className = 'item-li disabled'
}
if(boardlist.length <=1) {
li.className = 'item-li disabled'
}
if(boardIndex === boardlist.length || (boardlist.length - boardIndex) ===1) {
li.className = 'item-li disabled'
}
}
if (menu.key ===5) {
div.style = `color: ${this.state.bodies.color}`
}
li.appendChild(div)
li.appendChild(divText)
li.addEventListener('click',()=> this.boardTool(menu.key))
menus.appendChild(li)
// return li
})
board.appendChild(menus)
}
//画线处理
dealPoint(x,y,state){
const len = this.state.points.length;
if(state != -1 && len > 1){
const before = this.state.points[len - 1],end={
x:x,
y:y
},cp={
x:(before.x + x) / 2,
y:(before.y + y) / 2
}
Object.assign(this.state.bodies, {
pointId: this.state.bodies.pointId + 0.5,
drawState: state,
pointX:Math.ceil(cp.x),
pointY:Math.ceil(cp.y)
});
this.sendPoint();
this.state.points.push(end);
this.state.pointsNum++;
Object.assign(this.state.bodies, {
pointId: Math.floor(this.state.bodies.pointId) + 1,
pointX:x,
pointY:y
});
this.sendPoint();
if (this.state.points.length > 2) {
if (this.state.bodies.isEraser) {
this.earseLine(this.state.beginPoint, before, cp);
} else {
this.drowline(this.state.beginPoint, before, cp);
}
this.state.beginPoint = cp
}
}else{
let lineId = parseInt(this.state.bodies.lastLineId) + 1
Object.assign(this.state.bodies, {
pointId: 0,
drawState: state,
pointX:x,
pointY:y,
lineId: lineId ? lineId :0,
lastLineId: parseInt(this.state.bodies.lastLineId) + 1
});
this.sendPoint();
this.state.points.push({x,y});
this.state.pointsNum++;
this.state.beginPoint = {x,y}
}
}
// 按下开始
canvasDown(e) {
// this.dynamic = -1; //关面板
this.canvasMoveUse = true;
e.stopPropagation();
e.preventDefault()
let hasEvent
try {
if (e instanceof TouchEvent) {
hasEvent = true
}
} catch (error) {
hasEvent = false
}
if(hasEvent){
this.dealPoint(e.touches[0].clientX,e.touches[0].clientY,-1)
}else{
this.dealPoint(e.offsetX,e.offsetY,-1)
}
}
//移动ing
canvasMove(e) {
e.stopPropagation();
e.preventDefault()
let hasEvent
try {
if (e instanceof TouchEvent) {
hasEvent = true
}
} catch (error) {
hasEvent = false
}
if(hasEvent){
this.dealPoint(e.touches[0].clientX,e.touches[0].clientY,0)
}else{
if (e.buttons == 1 && this.canvasMoveUse) {
this.dealPoint(e.offsetX,e.offsetY,0)
}else{
this.canvasMoveUse = false;
}
}
}
// 松开结束
canvasUp(e) {
e.stopPropagation();
e.preventDefault()
let hasEvent
try {
if (e instanceof TouchEvent) {
hasEvent = true
}
} catch (error) {
hasEvent = false
}
if(hasEvent){
this.dealPoint(this.state.bodies.pointX,this.state.bodies.pointY,1)
}else{
this.dealPoint(e.offsetX,e.offsetY,1)
}
this.canvasMoveUse = false;
if (this.state.pointsNum >= 3000) {
this.saveCanvas(res => {
this.state.pointsNum = 0;
});
}
this.points = [];
}
// 发送坐标
sendPoint() {
this.state.bodies.pointX=Math.ceil(this.state.bodies.pointX*this.state.rate);
this.state.bodies.pointY=Math.ceil(this.state.bodies.pointY*this.state.rate);
}
// 清空数据
closeBoard() {
this.clearCanvas()
this.state.boardIndex = 0
this.state.boardlist = []
}
clearCanvas() {
this.state.board.canvas.height = this.state.board.canvas.height;
const board = document.getElementById('board')
this.state.board.context.fillStyle = '#333'
this.state.board.context.fillRect(0,0,board.clientWidth,board.clientHeight);
}
// 本地保存图片
localSaveCanvas() {
const baseImg = this.state.board.canvas.toDataURL("image/jpeg", 1.0);
// this.state.boardlist.push()
const {boardIndex } = this.state
if (boardIndex === 0) {
this.state.boardlist.push(baseImg)
} else {
this.state.boardlist.splice(boardIndex, 1, baseImg)
}
// const imgData = this.state.board.canvas.toDataURL({format: 'png', quality:1, width:200, height:400});
// const strDataURI = imgData.substr(22, imgData.length);
// const imageData = this.state.board.canvas.toDataURL('image/png')
window.postMessage(this.state.boardlist, window.location.origin )
this.state.boardlist = []
this.state.boardIndex = 0
}
//保存canvas图片
saveCanvas() {
const baseImg = this.state.board.canvas.toDataURL("image/jpeg", 1.0);
if (this.state.changeBard) {
if (this.state.boardIndex === this.state.boardlist.length -1) {
this.state.boardlist.push(baseImg)
} else {
const index = this.state.boardIndex
// todo: 0 =》 1
this.state.boardlist.splice(index, 0,baseImg)
}
// ++ this.boardIndex
} else {
const index = this.state.boardIndex
// todo: 0 =》 1
this.state.boardlist.splice(index, 1, baseImg)
}
// document.getElementById('img').src = baseImg
// reset board
}
//橡皮檫
earseLine(start, controlPoint, endPoint) {
this.state.board.context.beginPath();
this.state.board.context.clearRect(
start.x,
start.y,
this.state.bodies.lineSize * 10,
this.state.bodies.lineSize * 10
);
}
//画线
drowline(start, control, end) {
//设置画布属性
this.setCanvasStyle(this.state.bodies.lineSize,2,this.state.bodies.color,this.state.bodies.color);
this.state.board.context.beginPath();
this.state.board.context.moveTo(start.x, start.y);
this.state.board.context.quadraticCurveTo(control.x, control.y, end.x, end.y );
this.state.board.context.stroke();
this.state.board.context.closePath();
}
// 设置绘画配置
setCanvasStyle(lineWidth, shadowBlur, shadowColor, strokeStyle) {
this.state.board.context.lineWidth = lineWidth;
this.state.board.context.shadowBlur = shadowBlur;
this.state.board.context.shadowColor = shadowColor;
this.state.board.context.strokeStyle = strokeStyle;
}
// 设置颜色
setColor(i) {
this.state.bodies.color = i;
this.state.dynamic = -1;
}
// 获取颜色
getColor() {
if (this.state.bodies.isEraser) {
return 'white'
}
return 'red';
}
// 插入图片
openImage(file) {
console.log('insert--imageFile', file)
// const reader = new FileReader();
// reader.onload = function(e){
// get_data(this.result);
// console.log('resxxx',e.target.result);
const image = new Image()
image.src = file.fileUrl
image.setAttribute("crossOrigin",'Anonymous')
image.onload =() => {
let widthScale, heightScale = 1
let tempWidth = image.width
let tempHeight = image.height
if (image.width > this.state.board.drawWidth) {
// tempWidth = this.state.board.drawWidth
// tempHeight = (image.height * this.state.board.drawWidth) / image.width
// widthScale = Math.floor((this.state.board.drawWidth / image.width) *10 ) /10
let oldwidth = image.width;
tempHeight = image.height * (this.state.board.drawWidth/oldwidth);
tempWidth = this.state.board.drawWidth;
// myimg.width = maxwidth;
}
if (tempHeight > this.state.board.drawHeigth) {
// heightScale = Math.floor((this.state.board.drawHeigth / image.height) *10 ) /10
// image.height = image.height * (Math.floor((image.width / image.height) *10 ) /10)
tempHeight = this.state.board.drawHeigth
// tempWidth = (image.width * this.state.board.drawHeigth) / image.height
// let oldheight = image.height;
// tempWidth = image.width * (this.state.board.drawHeigth/oldheight);
// tempHeight = this.state.board.drawHeigth;
}
const imageLeft = (this.state.board.drawWidth - image.width *widthScale)/2
const imageTop = (this.state.board.drawHeigth - image.height *heightScale)/2
console.log('image', tempWidth, tempHeight)
this.state.board.context.drawImage( image, 0, 0, tempWidth, tempHeight);
}
// }
// reader.readAsDataURL([file]);
}
// 新建
createBoard() {
this.state.boardIndex +=1
this.state.changeBard = true
// 最多十页
if (this.state.boardIndex >9) {
return
}
this.saveCanvas()
this.clearCanvas()
}
// 翻页
paging(num) {
if (this.state.boardIndex >= this.state.boardlist.length -1 && num != 1) {
console.log('没有下一页了')
return
}
if (this.state.boardIndex <= 0 && num !=2) {
console.log('没有上一页了')
return
}
if (num ===1) {
// 上一页 先保存后渲染
this.state.changeBard = false
this.saveCanvas()
this.state.boardIndex --
const image = new Image()
image.src = this.state.boardlist[this.state.boardIndex]
image.onload =() =>{
this.clearCanvas()
this.state.board.context.drawImage( image, 0, 0, image.width, image.height);
}
return
}
this.state.changeBard = false
this.saveCanvas()
this.state.boardIndex ++
const nextImage = new Image()
nextImage.src = this.state.boardlist[this.state.boardIndex]
nextImage.onload =() =>{
this.clearCanvas()
this.state.board.context.drawImage( nextImage, 0, 0, nextImage.width, nextImage.height);
}
}
renderMenus(menus) {
if (this.isMobile) {
this.state.menuPen = menus
return
}
const leftMenu = this.state.menuPen.slice(0, this.state.menus.length -2)
this.state.menuPen = [...leftMenu, ...this.state.baseMenus]
}
boardTool(index) {
console.log('index',index)
switch (index) {
case 0:
// this.openImage()
this.createBoard()
break
case 1:
case 2:
this.paging(index)
break
case 3:
this.state.bodies.isEraser = true;
document.getElementById('canvas').setAttribute('class','eraser')
break;
case 4:
this.clearCanvas()
break;
case 5: {
const len = this.state.menus.length
const lasetItem = this.state.menus.slice(len -1)
// const startItem = this.state.baseMenus.slice(0,len -1)
// this.menus = Object.assign([], this.menus,this.menuPen,lasetItem)
this.state.bodies.isEraser = false
if (!this.isOpen) {
this.renderMenus(this.state.baseMenus)
this.isOpen = true;
} else {
// [{text: "画笔", key: 5, className:"hyfont ketang-huabi"}, ...this.state.menuPen, ...lasetItem]
// this.state.menuPen = this.state.menus
this.renderMenus(this.state.menus)
this.isOpen = false;
}
document.getElementById('canvas').setAttribute('class','pencil')
// this.appenedMenus()
break;
}
case 6:// 保存
this.localSaveCanvas()
return;
case 7:
this.setColor('red')
this.state.menuPen = this.state.menus
this.isOpen = false;
break
case 8:
this.setColor('white')
this.state.menuPen = this.state.menus
this.isOpen = false;
break
case 9:
this.setColor('green')
this.state.menuPen = this.state.menus
this.isOpen = false;
break
case 10:
this.state.bodies.lineSize= 1
this.state.menuPen = this.state.menus
this.isOpen = false;
break
case 11:
this.state.bodies.lineSize = 3
this.state.menuPen = this.state.menus
this.isOpen = false;
break
case 12:
this.state.bodies.lineSize = 5
this.state.menuPen = this.state.menus
this.isOpen = false;
break
default:
break
}
setTimeout(() =>{
this.appenedMenus()
},200)
}
}
export default ClassBoard