-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
133 lines (109 loc) · 3.02 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const canvas = document.getElementById("jsCanvas");
const ctx = canvas.getContext("2d");
const colors = document.getElementsByClassName("jsColor");
const range = document.getElementById("jsRange");
const mode = document.getElementById("jsMode");
const saveBtn = document.getElementById("jsSave");
const paletteChange = document.getElementById("paletteChange");
const p1 = document.getElementById("palette1");
const p2 = document.getElementById("palette2");
const p3 = document.getElementById("palette3");
const palettes = {};
palettes["1"] = "p1";
palettes["2"] = "p2";
palettes["3"] = "p3";
const INITIAL_COLOR = "#2c2c2c";
const CANVAS_SIZE = 700;
const HIDDEN = "hidden";
canvas.width = CANVAS_SIZE;
canvas.height = CANVAS_SIZE;
ctx.fillStyle = "white";
ctx.fillRect(0, 0, CANVAS_SIZE, CANVAS_SIZE);
ctx.strokeStyle = INITIAL_COLOR;
ctx.fillStyle = INITIAL_COLOR;
ctx.lineWidth = 2.5;
let painting = false;
let filling = false;
function startPainting(event) {
painting = true;
}
function stopPainting(event) {
painting = false;
}
function onMouseMove(event) {
const x = event.offsetX;
const y = event.offsetY;
if(!painting) {
ctx.beginPath();
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
ctx.stroke();
}
}
function handleColorClick(event) {
const color = event.target.style.backgroundColor;
ctx.strokeStyle = color;
ctx.fillStyle = color;
}
function handleRangeChange(event) {
const size = event.target.value;
ctx.lineWidth = size;
}
function handleModeClick() {
if(filling === true) {
filling = false;
mode.innerText = "Fill";
} else {
filling = true;
mode.innerText = "Paint";
}
}
function handleCanvasClick() {
if(filling) {
ctx.fillRect(0, 0, CANVAS_SIZE, CANVAS_SIZE);
}
}
function handleCM(event) {
event.preventDefault();
}
function handleSaveClick() {
const image = canvas.toDataURL();
const link = document.createElement("a");
link.href = image;
link.download = "yourPainting[🎨]";
link.click();
}
let n = 1;
function handlePaletteClick() {
p1.classList.toggle(HIDDEN);
p2.classList.toggle(HIDDEN);
/*const p = palettes[n];
p.classList.toggle(HIDDEN);
console.log("show palette" + n);
n++;
if(n == 4) {
n = 1;
}*/
}
if(canvas) {
canvas.addEventListener("mousemove", onMouseMove);
canvas.addEventListener("mousedown", startPainting);
canvas.addEventListener("mouseup", stopPainting);
canvas.addEventListener("mouseleave", stopPainting);
canvas.addEventListener("click", handleCanvasClick);
canvas.addEventListener("contextmenu", handleCM);
}
Array.from(colors).forEach(color => color.addEventListener("click", handleColorClick));
if(range) {
range.addEventListener("input", handleRangeChange);
}
if(mode) {
mode.addEventListener("click", handleModeClick);
}
if(saveBtn) {
saveBtn.addEventListener("click", handleSaveClick);
}
if(paletteChange) {
paletteChange.addEventListener("click", handlePaletteClick);
}