WhiteBoardApe.js
10.3 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
//*
// 标注模块
// */
import Loger from "../Loger";
import Ape from "./Ape";
import xdysdk from "libs/xdysdk";
import DrawTool from "./DrawTool";
import LaserPen from "./LaserPen";
import $ from "jquery";
import ClassDataProxy from "proxy/ClassDataProxy";
let loger = Loger.getLoger('PC-WhiteBoardApe');
class WhiteBoardApe extends Ape {
constructor() {
super();
this.color = "#b8242a";
ClassDataProxy.isDraw = false;//是否可以绘制标注
this.drawTool;
this.thickness = 2;
this.isMouseDown = false;
this.canvasContent;
this.context;
this.canvasWidth = 0;
this.canvasHeight = 0;
this.scroolTop = 0;
this.scroolLeft = 0;
this.canvasTop = 0;
this.canvasLeft = 0;
this.pointGrou = [];
this.prevX=0;
this.prevY=0;
this.receiveData = [];//储存接收到的数据,用于canvas重绘时使用
this.addEvent();
this.init();
}
init() {
this.drawTool = new DrawTool();
this.drawTool.on(DrawTool.DRAW_TOOL_CHANGE, this._drawToolChnageHandler.bind(this));
//布局
//loger.log("docBox->", $(".docBox").width(), $(".docBox").height());
this.canvasContent = $(".canvasContent");
this.canvasWidth = $(".docBox")[0].clientWidth;
this.canvasHeight = $(".docBox")[0].clientHeight;
this.canvasContent[0].width = this.canvasWidth;
this.canvasContent[0].height = this.canvasHeight;
this.context = this.canvasContent[0].getContext("2d");
//loger.log(" this.canvasContent-->", this.canvasContent.width(), this.canvasContent.height());
//UI按钮点击事件
this.canvasContent.on('mousedown', this._mouseDownHandler.bind(this));
this.canvasContent.on('mousemove', this._mouseMoveHandler.bind(this));
document.addEventListener('mouseup',this._mouseUpHandler.bind(this),false);
document.addEventListener('mouseout',this._mouseOutHandler.bind(this),false)
}
addEvent() {
xdysdk.on("whiteboard_annotation_update", this._whiteboradUpdateHanlder.bind(this));
xdysdk.on("class_join_success", this._classJoinSuccessHandler.bind(this));
}
setCanvasSize(_data) {
//loger.log("setCanvasSize", _data);
if (this.canvasContent) {
//文档的尺寸已经更改,需要更新canvas大小
this.canvasWidth = _data.width;
this.canvasHeight = _data.height;
this.canvasContent[0].width = this.canvasWidth;
this.canvasContent[0].height = this.canvasHeight;
this._clearCanvas();
this._startDraw(this.receiveData);
//loger.log("setCanvasSize->success->需要重绘的数据->", this.receiveData.length);
}
}
clear() {
//loger.log("clear");
this.pointGrou = [];
this.receiveData = [];
this._clearCanvas();
}
_whiteboradUpdateHanlder(_data) {
if(ClassDataProxy.userRole==ClassDataProxy.USER_NOTMAL){
$(".laserBoard").css("z-index","-1");
}
loger.log("更新的白板标注->是否清除:", _data.isFresh,"数量->",_data.annotaionItems.length);
let annotaionItems = _data.annotaionItems;
//isFresh是白板的绘制模式,true-先清除canvas再绘制;false->在canvas上直接添加绘制
if (_data.isFresh) {
//清除重绘制数据
this.receiveData = [];
this._clearCanvas();
//本地储存一份数据
if (annotaionItems && annotaionItems.length > 0) {
for (let i = 0; i < annotaionItems.length; i++) {
this.receiveData.push(annotaionItems[i]);
}
}
//绘制全部数据
this._startDraw(annotaionItems);
}else {
//本地储存一份数据
let tempAnnos=[];
let nodeId=ClassDataProxy.nodeId;
if (annotaionItems && annotaionItems.length > 0) {
for (let k = 0; k < annotaionItems.length; k++) {
this.receiveData.push(annotaionItems[k]);
if(annotaionItems[k].initiator!=nodeId){
tempAnnos.push(annotaionItems[k]);
}
}
}
//绘制不是自己刚刚添加的数据
this._startDraw(tempAnnos);
}
}
_classJoinSuccessHandler(_data){
if (_data.userRole == ClassDataProxy.USER_NOTMAL || ClassDataProxy.isRecordPlayBack) {
this.color = '#0071bc';
$('#showColor').css('background','url("images/colorBlue.png") no-repeat');
}
}
//绘制数据到canvas
_startDraw(_data) {
if (_data && _data.length > 0) {
for (let i = 0; i < _data.length; i++) {
let item = _data[i];
if (item && item.parentId == ClassDataProxy.currentDocId && item.curPageNo == ClassDataProxy.currentPageNum) {
this._drawDataToCanvas(item);
}
}
}
}
//清除canvas
_clearCanvas() {
if (this.context) {
this.context.clearRect(0, 0, this.canvasWidth, this.canvasHeight);
}
}
//绘制数据到canvas
_drawDataToCanvas(_data) {
if (_data) {
switch (_data.type) {
case 0:
this._drawLine(_data);
break;
default:
loger.log("_drawDataToCanvas->未知类型不处理", _data.type);
break;
}
}
}
//绘制线条
_drawLine(_data) {
//loger.log("_drawLine---->");
let pointGroup = _data.pointGroup;
if (pointGroup && pointGroup.length > 1) {
this.context.beginPath();
this.context.lineWidth = parseInt(_data.thickness) || 2;
this.context.strokeStyle = _data.color || "#b8242a";
//起点
let point = this.xyFromPer(pointGroup[0].w, pointGroup[0].h);
let len = pointGroup.length;
this.context.moveTo(point.x, point.y);
let item = null;
for (let i = 1; i < len; i++) {
item = pointGroup[i];
point = this.xyFromPer(item.w, item.h);
this.context.lineTo(point.x, point.y);
}
this.context.stroke();
}
}
_drawToolChnageHandler(_data) {
loger.log("_drawToolChnageHandler", _data.color);
switch (_data.action) {
case "pencil":
//画笔切换
ClassDataProxy.isDraw = _data.isDraw;
break;
case "changeColor":
//颜色切换
this.color = _data.color;
break;
case "rescind":
//撤销
xdysdk.api("sendGotoPrev");
break;
case "clear":
//清除当前页的标注
xdysdk.api("sendDeleteCurPageAnnotation");
break;
default :
break
}
}
_mouseDownHandler(evt) {
this.pointGrou = [];
if (ClassDataProxy.isDraw) {
this.isMouseDown = true;
this.scroolTop = $(window).scrollTop();
this.scroolLeft = $(window).scrollLeft();
this.canvasTop = this.canvasContent.offset().top - this.scroolTop;
this.canvasLeft = this.canvasContent.offset().left - this.scroolLeft;
this.prevX=evt.clientX - this.canvasLeft;;
this.prevY=evt.clientY - this.canvasTop;
this.pointGrou.push(this.xyToPer(Math.round( this.prevX), Math.round( this.prevY)));
}
}
_mouseMoveHandler(evt) {
if (ClassDataProxy.isDraw && this.isMouseDown) {
let toX = evt.clientX - this.canvasLeft;
let toY = evt.clientY - this.canvasTop;
if(this.prevX==toX&&this.prevY==toY){
return;
}
this.context.beginPath();
this.context.moveTo(this.prevX,this.prevY);
this.context.lineTo(toX,toY);
this.context.strokeStyle= this.color;
this.context.lineWidth=this.thickness;
this.context.lineCap='round';
this.context.lineJoin='round';
this.context.stroke();
this.prevX=toX;
this.prevY=toY;
this.pointGrou.push(this.xyToPer(Math.round(toX), Math.round(toY)));
}
}
_mouseUpHandler(evt) {
if (ClassDataProxy.isDraw && this.isMouseDown) {
loger.log("_mouseUpHandler");
this.isMouseDown = false;
this._sendDrawLine();
}
this.pointGrou = [];
}
_mouseOutHandler(evt) {
if (ClassDataProxy.isDraw && this.isMouseDown) {
loger.log("_mouseOutHandler");
this.isMouseDown = false;
this._sendDrawLine();
}
this.pointGrou = [];
}
//发送新添加的标注数据
_sendDrawLine() {
if (this.pointGrou.length > 1) {
let paramInfo = {
"type": 0,
//"curPage": ClassDataProxy.currentPageNum,//页码是第1页
//"parentId": ClassDataProxy.currentDocId,//父级(文档)的itemIdx标识,如果当前没有文档,设置为0即可
"pointGroup": this.pointGrou,//有多个坐标点组成
"color": this.color,
"thickness":this.thickness //线条粗细放大一倍(这个问题之后要解决,不能放大)
};
console.log("发送标注数据->",paramInfo);
xdysdk.api("sendInsertAnnotaion", paramInfo);
this.pointGrou = [];
}
}
//把X Y坐标转换为相对canvas宽度的百分比
xyToPer(x, y) {
let recordPencilObject = {};
recordPencilObject.w = parseInt(x / this.canvasWidth * 10000) / 100;
recordPencilObject.h = parseInt(y / this.canvasWidth * 10000) / 100;
//loger.log("xyToPer",recordPencilObject);
return recordPencilObject;
}
xyFromPer(w, h) {
let xyObj = {};
xyObj.x = Math.round((w * this.canvasWidth) / 100) ;//加0.5偏移 平滑
xyObj.y = Math.round((h * this.canvasWidth ) / 100);
//loger.log("xyFromPer",xyObj);
return xyObj;
}
}
export default WhiteBoardApe;