-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
41 lines (34 loc) · 1.13 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
const canvas = document.getElementById('drawing-canvas');
const ctx = canvas.getContext('2d');
let isDrawing = false;
let color = document.getElementById('color').value;
let brushSize = document.getElementById('brush-size').value;
canvas.addEventListener('mousedown', (e) => {
isDrawing = true;
ctx.beginPath();
ctx.moveTo(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop);
});
canvas.addEventListener('mousemove', (e) => {
if (isDrawing) {
ctx.lineTo(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop);
ctx.strokeStyle = color;
ctx.lineWidth = brushSize;
ctx.stroke();
}
});
canvas.addEventListener('mouseup', () => {
isDrawing = false;
ctx.closePath();
});
const clearButton = document.getElementById('clear-button');
clearButton.addEventListener('click', () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
});
const colorPicker = document.getElementById('color');
colorPicker.addEventListener('change', (e) => {
color = e.target.value;
});
const brushSizeInput = document.getElementById('brush-size');
brushSizeInput.addEventListener('change', (e) => {
brushSize = e.target.value;
});