When I press the button, I want to clear the canvas. how can I do that ? can you help me. please. I tried it a few times but I couldn't. template and vuejs codes below. I would be glad if you help me. thank you.
Html
<template>
<div>
<canvas id="myCanvas" width="560" height="360" @mousedown="beginDrawing" @mousemove="keepDrawing" @mouseup="stopDrawing"/>
<button @click="resetcanvas">Reset Canvas</button>
</div>
</template>
Vuejs
<script>
export default {
data: {
x: 0,
y: 0,
isDrawing: false,
canvas: null,
},
methods: {
drawLine(x1, y1, x2, y2) {
let ctx = this.canvas;
ctx.beginPath();
ctx.strokeStyle = 'red';
ctx.lineWidth = 1;
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
ctx.closePath();
},
beginDrawing(e) {
this.x = e.offsetX;
this.y = e.offsetY;
this.isDrawing = true;
},
keepDrawing(e) {
if (this.isDrawing === true) {
this.drawLine(this.x, this.y, e.offsetX, e.offsetY);
this.x = e.offsetX;
this.y = e.offsetY;
}
},
stopDrawing(e) {
if (this.isDrawing === true) {
this.drawLine(this.x, this.y, e.offsetX, e.offsetY);
this.x = 0;
this.y = 0;
this.isDrawing = false;
}
}
},
mounted() {
var c = document.getElementById("myCanvas");
this.canvas = c.getContext('2d');
}
}
</script>