-
Notifications
You must be signed in to change notification settings - Fork 903
/
Copy pathscript.js
47 lines (39 loc) · 1.19 KB
/
script.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
42
43
44
45
46
47
// Get canvas element and drawing context
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// Variables for tracking drawing state
let drawing = false;
let lastX = 0;
let lastY = 0;
// Set line style
ctx.strokeStyle = 'black';
ctx.lineWidth = 2;
// Function to start drawing
function startDrawing(e) {
drawing = true;
[lastX, lastY] = [e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop];
}
// Function to draw a line
function draw(e) {
if (!drawing) return;
ctx.beginPath();
ctx.moveTo(lastX, lastY);
[lastX, lastY] = [e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop];
ctx.lineTo(lastX, lastY);
ctx.stroke();
}
// Function to stop drawing
function stopDrawing() {
drawing = false;
ctx.closePath();
}
// Event listeners for drawing
canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', stopDrawing);
canvas.addEventListener('mouseout', stopDrawing);
// Clear canvas button
const clearButton = document.getElementById('clear-btn');
clearButton.addEventListener('click', () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
});