forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1
More file actions
55 lines (48 loc) · 902 Bytes
/
Copy path1
File metadata and controls
55 lines (48 loc) · 902 Bytes
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
const canvas = document.getElementById("draw");
const ctx = canvas.getContext("2d");
let isDrawing = false;
let radius = 10;
let isRadiusUp = true;
let color = 0;
let isColorUp = true;
function change() {
if (isRadiusUp) {
radius += 1;
} else {
radius -= 1;
}
if (isColorUp) {
color += 1;
} else {
color -= 1;
}
if (radius >= 40) {
isRadiusUp = false;
}
if (radius < 4) {
radius = 10;
isRadiusUp = true;
}
if (color >= 250) {
isColorUp = false;
}
if (color <= 0) {
isColorUp = true;
}
}
document.addEventListener("pointerdown", () => {
isDrawing = true;
});
document.addEventListener("pointerup", () => {
isDrawing = false;
});
addEventListener("pointermove", (event) => {
change();
if (isDrawing) {
const x = event.clientX;
const y = event.clientY;
ctx.arc(x, y, radius, 0, 360, true);
ctx.fillStyle = `hsl(${color}, 50%, 50%)`;
ctx.fill();
}
});