drawableCanvas = () => {
const canvas = DOM.canvas(300, 300);
const context = canvas.getContext("2d");
let mouseDown = false;
let lastX;
let lastY;
function drawLine(x, y, lastX, lastY) {
context.strokeStyle = "#000000";
context.lineWidth = 12;
context.lineJoin = "round";
context.beginPath();
context.moveTo(lastX, lastY);
context.lineTo(x, y);
context.closePath();
context.stroke();
return [x, y];
}
function handleMousedown() {
mouseDown = true;
};
function handleMouseup() {
mouseDown = false;
[lastX, lastY] = [undefined, undefined];
};
function handleMousemove(e) {
const rect = e.target.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
if (mouseDown) {
[lastX, lastY] = drawLine(x, y, lastX, lastY);
}
};
canvas.addEventListener("mousedown", handleMousedown);
canvas.addEventListener("mouseup", handleMouseup);
canvas.addEventListener("mousemove", handleMousemove);
context.fillStyle = "#ffffff";
context.fillRect(0, 0, 300, 300);
return canvas;
}