-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (53 loc) · 1.3 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
let isMouseDown = false;
const color = document.querySelector("input[type='color']");
const radius = document.querySelector("input[type='number']");
const eraser = document.querySelector(".eraser");
let rect = canvas.getBoundingClientRect();
let stroke = [];
eraser.addEventListener("click", (e) => {
color.value = "#ffffff";
});
canvas.addEventListener("mousedown", (e) => {
isMouseDown = true;
});
canvas.addEventListener("mouseup", (e) => {
isMouseDown = false;
for (let i = 0; i < stroke.length - 1; i++) {
ctx.beginPath();
ctx.strokeStyle = color.value;
ctx.lineWidth = parseInt(radius.value) * 2;
ctx.moveTo(stroke[i].x, stroke[i].y);
ctx.lineTo(stroke[i + 1].x, stroke[i + 1].y);
ctx.closePath();
ctx.stroke();
}
stroke = [];
});
function draw(x, y) {
ctx.beginPath();
ctx.fillStyle = color.value;
ctx.arc(
x - rect.left,
y - rect.top,
parseInt(radius.value),
0,
2 * Math.PI,
false
);
ctx.fill();
ctx.closePath();
}
canvas.addEventListener("mousemove", (e) => {
let xlim = e.clientX - rect.left;
let ylim = e.clientY - rect.top;
if (!isMouseDown) {
return;
}
draw(e.clientX, e.clientY);
stroke.push({
x: xlim,
y: ylim,
});
});