-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
108 lines (86 loc) · 3.24 KB
/
app.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const canvas = document.querySelector("canvas");
const context = canvas.getContext("2d");
const range = document.querySelector("#jsRange");
const mode = document.querySelector("#jsMode");
const save = document.querySelector("#jsSave");
/* Option 1: getElementByClassName (HTMLCollection) → Array.from (Array) → forEach */
/* forEach is not available on HTMLCollection */
const colors_html = document.getElementsByClassName("controls_color");
/* Option 2: querySelectorAll (NodeList) → forEach */
/* forEach is available on NodeList and Array */
/* const colors_node = document.querySelectorAll(".controls_color"); */
/* Option 3: getElementByClassName (HTMLCollection) → for */
/* Not Working → Perhaps addEventListner is not available on HTMLCollection */
/* const colors_html = document.getElementsByClassName("controls_color"); */
canvas.width = 700;
canvas.height = 700;
context.fillStyle = "white";
context.fillRect(0, 0, canvas.width, canvas.height);
context.strokeStyle = "#2c2c2c";
context.lineWidth = 2.5;
let onPainting = false;
let onFilling = false;
const startPainting = () => {
onPainting = true;
};
const stopPainting = () => {
onPainting = false;
};
const onMouseMove = (event) => {
const x = event.offsetX;
const y = event.offsetY;
if (!onPainting) {
context.beginPath();
context.moveTo(x, y);
} else {
context.lineTo(x, y);
context.stroke();
}
};
const onClick = () => {
if (onFilling) context.fillRect(0, 0, canvas.width, canvas.height)
};
const onContextMenu = (event) => {
event.preventDefault();
}
const onSave = () => {
const image = canvas.toDataURL("image/png");
const link = document.createElement("a");
link.href = image;
link.download = "image";
link.click();
};
const changeColor = (event) => {
const color = event.target.style.backgroundColor;
context.strokeStyle = color;
context.fillStyle = color;
};
const changeRange = (event) => {
const lineWidth = event.target.value;
context.lineWidth = lineWidth;
}
const changeMode = () => {
if (!onFilling) mode.innerText = "Paint";
else mode.innerText = "Fill";
onFilling = !onFilling;
}
canvas.addEventListener("mousemove", onMouseMove);
canvas.addEventListener("mousedown", startPainting);
canvas.addEventListener("mouseup", stopPainting);
canvas.addEventListener("mouseleave", stopPainting);
canvas.addEventListener("click", onClick);
canvas.addEventListener("contextmenu", onContextMenu);
/* Option 1: getElementByClassName (HTMLCollection) → Array.from (Array) → forEach */
/* forEach is not available on HTMLCollection */
Array.from(colors_html).forEach((color) => addEventListener("click", changeColor));
/* Option 2: querySelectorAll (NodeList) → forEach */
/* forEach is available on NodeList and Array */
/* colors_node.forEach((color) => color.addEventListener("click", changeColor)); */
/* Option 3: getElementByClassName (HTMLCollection) → for */
/* Not Working → Perhaps addEventListner is not available on HTMLCollection */
/* for (let i = 0; i < colors_html.length; i++) {
colors_html[i].addEventListener("click", changeColor);
} */
range.addEventListener("input", changeRange);
mode.addEventListener("click", changeMode);
save.addEventListener("click", onSave);