-
Notifications
You must be signed in to change notification settings - Fork 0
/
paint.js
117 lines (95 loc) · 2.64 KB
/
paint.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
109
110
111
112
113
114
115
116
117
let checkPainting = false;
const canvas = document.getElementById("jsCanvas");
const ctx = canvas.getContext("2d");
const colors = document.querySelectorAll(".control__color");
const range = document.getElementById("jsInputRange");
const fill = document.getElementById("jsFill");
const reset = document.getElementById("jsReset");
const save = document.getElementById("jsSave");
canvas.width = 1100;
canvas.height = 1000;
ctx.strokeStyle = "black";
ctx.lineWidth = "4";
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);
const handleMouseMove = (event) => {
const { offsetX, offsetY } = event;
if (!checkPainting) {
ctx.beginPath();
ctx.moveTo(offsetX, offsetY);
} else {
ctx.lineTo(offsetX, offsetY);
ctx.stroke();
}
};
const handleMouseLeave = (event) => {
const { offsetX, offsetY } = event;
checkPainting = false;
};
const handleMouseDown = (event) => {
const { offsetX, offsetY } = event;
checkPainting = true;
};
const handleMouseUp = (event) => {
const { offsetX, offsetY } = event;
checkPainting = false;
};
const handleRightClick = (event) => {
event.preventDefault();
};
const handleColorClick = (event) => {
const currentColor = document.getElementById("jsCurrentColor");
const {
target: {
style: { backgroundColor },
},
} = event;
currentColor.style.backgroundColor = backgroundColor;
ctx.strokeStyle = backgroundColor;
};
const handleInput = (event) => {
const {
target: { value },
} = event;
ctx.lineWidth = value;
};
const handleFillClick = () => {
ctx.fillStyle = ctx.strokeStyle;
ctx.fillRect(0, 0, canvas.width, canvas.height);
};
const handleResetClick = () => {
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);
};
const handleSaveClick = () => {
const canvasDataURL = canvas.toDataURL("image/png");
const aTag = document.createElement("a");
aTag.href = canvasDataURL;
aTag.download = "🎨";
aTag.click();
};
if (canvas) {
canvas.addEventListener("mousemove", handleMouseMove);
canvas.addEventListener("mouseleave", handleMouseLeave);
canvas.addEventListener("mousedown", handleMouseDown);
canvas.addEventListener("mouseup", handleMouseUp);
canvas.addEventListener("contextmenu", handleRightClick);
}
if (colors) {
const colorArray = Array.from(colors);
colorArray.map((color) => {
color.addEventListener("click", handleColorClick);
});
}
if (range) {
range.addEventListener("input", handleInput);
}
if (fill) {
fill.addEventListener("click", handleFillClick);
}
if (reset) {
reset.addEventListener("click", handleResetClick);
}
if (save) {
save.addEventListener("click", handleSaveClick);
}