Skip to content

Commit cdf793b

Browse files
committed
feat: draw delete mode sync (#23)
1 parent 9f5d912 commit cdf793b

File tree

1 file changed

+57
-16
lines changed

1 file changed

+57
-16
lines changed

svr/res/js/draw.js

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ const draw = new Vue({
2020
lineCap : "round",
2121
lineJoin : "round",
2222
strokeStyle: "#000000", // 画笔颜色
23+
2324
//line: 线条, circle: 圆形, rectangle: 矩形, text: 文字, delete: 擦除
2425
drawMode: "line", // 画笔模式
26+
2527
circleFill : false, //填充圆模式
2628
rectangleFill : false, //填充矩形模式
2729
triangleFill : false, //填充三角形
@@ -120,7 +122,7 @@ const draw = new Vue({
120122
return
121123
}
122124

123-
let {
125+
let {
124126
width : remoteWidth, height : remoteHeight, lineWidth,
125127
curPoint, prePoint, starStartPoint, circleStartPoint, triangleStartPoint, rectangleStartPoint
126128
} = options.remote;
@@ -162,8 +164,15 @@ const draw = new Vue({
162164
starStartPoint.y = starStartPoint.y * ratioHeight;
163165
options.remote.starStartPoint = starStartPoint;
164166
this.drawStar(options);
165-
} else {
166-
console.log("收到远程未知的绘制模式")
167+
} else if(drawMode === 'delete'){
168+
prePoint.x = prePoint.x * ratioWidth;
169+
prePoint.y = prePoint.y * ratioHeight;
170+
options.remote.prePoint = prePoint;
171+
this.drawDelete(options);
172+
} else if(drawMode === 'image'){
173+
this.drawImage(options);
174+
} else {
175+
console.log("收到远程未知的绘制模式, ",options)
167176
}
168177
},
169178
// 打开/关闭本地画笔
@@ -423,9 +432,8 @@ const draw = new Vue({
423432
}
424433

425434
document.getElementById("drawImage").onclick = function () {
426-
that.drawImage({ local : {
427-
canvas, context, localDrawCallback
428-
} })
435+
that.drawMode = "image";
436+
that.drawImage({ drawMode: that.drawMode, local : { canvas, context, localDrawCallback } })
429437
}
430438

431439
document.getElementById("drawDonwload").onclick = function () {
@@ -736,11 +744,16 @@ const draw = new Vue({
736744
}
737745
},
738746
// 画笔擦除
739-
drawDelete: function ({ event, drawMode, local : {
740-
canvas, context, lineWidth, curPoint, prePoint, lineCap,
741-
width, height, devicePixelRatio,
742-
lineJoin, strokeStyle, fillStyle
743-
}}) {
747+
drawDelete: function ({ event, drawMode, fromRemote, local, remote}) {
748+
if(fromRemote){
749+
local = remote;
750+
}
751+
let {
752+
canvas, context, lineWidth, curPoint, prePoint, lineCap,
753+
width, height, devicePixelRatio,
754+
lineJoin, strokeStyle, fillStyle, localDrawCallback
755+
} = local;
756+
744757
context.lineWidth = lineWidth;
745758
context.lineCap = lineCap;
746759
context.lineJoin = lineJoin;
@@ -762,13 +775,31 @@ const draw = new Vue({
762775
context.clearRect(x - 20, y - 20, lineWidth, lineWidth);
763776
i++;
764777
}
778+
779+
//如果是本地擦除,完成擦除后数据回调给远端
780+
if (!fromRemote) {
781+
localDrawCallback && localDrawCallback({ event, drawMode, fromRemote : true, remote : {
782+
lineWidth, curPoint, prePoint, lineCap, width, height, devicePixelRatio,
783+
lineJoin, strokeStyle, fillStyle,
784+
}});
785+
}
765786
},
766787
// 图片渲染处理
767-
drawImage: function ({ event, drawMode, local : {
768-
canvas, context, lineWidth, curPoint, prePoint, lineCap,
769-
width, height, devicePixelRatio,
770-
lineJoin, strokeStyle, fillStyle, localDrawCallback
771-
}}) {
788+
drawImage: function ({ event, drawMode, fromRemote, local, remote}) {
789+
if(fromRemote){
790+
local = remote;
791+
}
792+
let {
793+
canvas, context, lineWidth, curPoint = {x:0,y:0}, prePoint, lineCap,
794+
width, height, devicePixelRatio, imgResult, imgWidth, imgHeight,
795+
lineJoin, strokeStyle, fillStyle, localDrawCallback
796+
} = local;
797+
798+
if(fromRemote){
799+
context.drawImage(imgResult, 0, 0, imgWidth, imgHeight, 0, 0, canvas.width, canvas.height);
800+
return;
801+
}
802+
772803
let that = this;
773804
let input = document.createElement("input");
774805
input.setAttribute("type", "file");
@@ -786,6 +817,16 @@ const draw = new Vue({
786817
//画完图片,保存操作记录
787818
that.drawHistoryList.push(canvas.toDataURL());
788819
that.drawRollbackPoint = that.drawHistoryList.length - 1;
820+
821+
//如果是本地绘制,完成绘制后数据回调给远端
822+
//图片暂时存在问题,因为图片大小可能超过通道限制,需要分批发送
823+
//所以暂时不支持图片的远端同步
824+
// if (!fromRemote) {
825+
// localDrawCallback && localDrawCallback({ event, drawMode, fromRemote : true, remote : {
826+
// lineWidth, curPoint, prePoint, lineCap, width, height, devicePixelRatio,
827+
// lineJoin, strokeStyle, fillStyle, imgResult : img.src, imgWidth: img.width, imgHeight: img.height
828+
// }});
829+
// }
789830
}
790831
}
791832
}

0 commit comments

Comments
 (0)