-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.js
41 lines (36 loc) · 1.14 KB
/
draw.js
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
const drawingCanvas = document.getElementById('drawingCanvas');
const drawingCtx = drawingCanvas.getContext('2d');
let drawing = false;
let drawData = []; // Array to store drawing coordinates
drawingCanvas.addEventListener('mousedown', startDrawing);
drawingCanvas.addEventListener('mousemove', draw);
drawingCanvas.addEventListener('mouseup', stopDrawing);
drawingCanvas.addEventListener('mouseout', stopDrawing);
function startDrawing(e) {
drawing = true;
drawingCtx.beginPath();
const { x, y } = getMousePos(e, drawingCanvas);
drawingCtx.moveTo(x, y);
drawData.push({ x, y, drawing: true });
}
function draw(e) {
if (!drawing) return;
const { x, y } = getMousePos(e, drawingCanvas);
drawingCtx.lineTo(x, y);
drawingCtx.strokeStyle = '#000';
drawingCtx.lineWidth = 2;
drawingCtx.stroke();
drawData.push({ x, y, drawing: true });
}
function stopDrawing() {
drawing = false;
drawingCtx.closePath();
drawData.push({ drawing: false });
console.log(drawData);
}
function getMousePos(e, canvas) {
return {
x: e.clientX - canvas.offsetLeft,
y: e.clientY - canvas.offsetTop
};
}