下面附上画板代码
<!-- 画板 -->
<canvas
class="palette"
id="canvas"
:width="chartWidth"
:height="chartHeight"
>
</canvas>
//点击画板时触发
coverEdit() {
this.canvas = document.getElementById("canvas");
this.ctx = this.canvas.getContext("2d");
this.ctx.fillStyle = "transparent";
this.ctx.globalCompositeOperation="source-over"
//动态获取canvas画布的宽this.canvas.width, 画布的高this.canvas.height
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
this.onoff = false;
this.oldx = 0;
this.oldy = 0;
this.linecolor = "red";
this.linw = 2;
this.canvas.addEventListener("mousemove", this.canvasDraw, true);
this.canvas.addEventListener("mousedown", this.canvasDown, false);
this.canvas.addEventListener("mouseup", this.canvasUp, false);
},
canvasDown(event) {
if (this.huahua == true) {
this.onoff = false;
} else {
this.onoff = true;
this.oldx = event.pageX - this.canvas.getBoundingClientRect().left;
this.oldy = event.pageY - this.canvas.getBoundingClientRect().top;
}
},
//鼠标弹起触发
canvasUp(event) {
this.onoff = false;
console.log('获取',event);
},
//涂抹绘制过程中触发
canvasDraw(event) {
if (this.onoff == true) {
var newx = event.pageX - this.canvas.getBoundingClientRect().left;
var newy = event.pageY - this.canvas.getBoundingClientRect().top;
this.ctx.beginPath();
this.ctx.moveTo(this.oldx, this.oldy);
this.ctx.lineTo(newx, newy);
this.ctx.strokeStyle = this.linecolor;
this.ctx.lineWidth = this.linw;
this.ctx.lineCap = "round";
this.ctx.stroke();
this.oldx = newx;
this.oldy = newy;
}
},
//绘制完成后清除画布内容,也可继续写入自己的代码进行下一步操作
completeMethod() {
if (this.isQzshow == false) {
this.completeaudio = setTimeout(() => {
var canvas = document.getElementById("canvas");
this.ctx = canvas.getContext("2d");
this.ctx.clearRect(0, 0, canvas.width, canvas.height);
}, 500);
}
},
我想添加一个事件,点击那条线就删除那条线,并不是那种橡皮擦涂抹的那种删除效果
https://www.cnblogs.com/fangs...
https://www.hangge.com/blog/c...
这里有提到线擦除,你可以参考下