-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
307 lines (273 loc) · 7.44 KB
/
Copy pathapp.js
File metadata and controls
307 lines (273 loc) · 7.44 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
let COLOR = "#000000";
let ERASE = false;
// 1 is grayscale, 2 is lighten, 3 is rainbow
let COLORMODE = ["off", "grayscale", "lighten", "rainbow"];
let CURRENTMODE = 0; // current coloring mode
let GRID = 32;
let touchscreen = "ontouchstart" in window || navigator.msMaxTouchPoints > 0;
// Makes the grid
function makeGrid() {
const sketchContainer = document.querySelector(".sketch");
sketchContainer.style.gridTemplateColumns = `repeat(${GRID}, 1fr)`;
sketchContainer.style.gridTemplateRows = `repeat(${GRID}, 1fr)`;
if (sketchContainer.hasChildNodes()) {
sketchContainer.replaceChildren();
}
for (let x = 0; x < GRID * GRID; x++) {
const pixel = document.createElement("div");
pixel.classList.add("pixel");
sketchContainer.appendChild(pixel);
}
draw(document.querySelectorAll(".pixel"));
}
makeGrid();
//
// drawing logic
function draw(pixel) {
mouseDown = false;
window.addEventListener("mousedown", () => (mouseDown = true));
window.addEventListener("mouseup", () => (mouseDown = false));
if (!touchscreen) {
pixel.forEach((pixel) => {
pixel.addEventListener("mouseover", (e) => {
if (ERASE && mouseDown) {
eraseMode(e.target);
} else if (mouseDown && CURRENTMODE != 0) {
// 1 is grayscale, 2 is lighten, 3 is rainbow
switch (CURRENTMODE) {
case 1:
grayscaleMode(e.target);
break;
case 2:
ligthenMode(e.target);
break;
case 3:
rainbowMode(e.target);
}
} else if (mouseDown) {
normalDraw(e.target);
}
});
//
// to color the grid on first click
pixel.addEventListener("mousedown", (e) => {
// 1 is grayscale, 2 is lighten, 3 is rainbow
if (ERASE) {
eraseMode(e.target);
} else if (CURRENTMODE != 0) {
switch (CURRENTMODE) {
case 1:
grayscaleMode(e.target);
break;
case 2:
ligthenMode(e.target);
break;
case 3:
rainbowMode(e.target);
}
} else {
normalDraw(e.target);
}
});
});
} else {
//
// drawing logic for mobile
window.ontouchmove = (e) => {
let mPixel = document.elementFromPoint(
e.touches[0].clientX,
e.touches[0].clientY
);
if (mPixel.classList[0] === "pixel") {
if (ERASE) {
eraseMode(mPixel);
} else if (CURRENTMODE != 0) {
// 1 is grayscale, 2 is lighten, 3 is rainbow
switch (CURRENTMODE) {
case 1:
grayscaleMode(mPixel);
break;
case 2:
ligthenMode(mPixel);
break;
case 3:
rainbowMode(mPixel);
}
} else {
normalDraw(mPixel);
}
}
};
//
// to color the grid on first click
window.ontouchstart = (e) => {
let mPixel = document.elementFromPoint(
e.touches[0].clientX,
e.touches[0].clientY
);
if (mPixel.classList[0] === "pixel") {
if (ERASE) {
eraseMode(mPixel);
} else if (CURRENTMODE != 0) {
// 1 is grayscale, 2 is lighten, 3 is rainbow
switch (CURRENTMODE) {
case 1:
grayscaleMode(mPixel);
break;
case 2:
ligthenMode(mPixel);
break;
case 3:
rainbowMode(mPixel);
}
} else {
normalDraw(mPixel);
}
}
};
}
}
//
// Pick new color
function setColor(pickedColor) {
COLOR = pickedColor;
}
const colorPicker = document.getElementById("color-picker");
colorPicker.oninput = (e) => setColor(e.target.value);
//
// Erase button
const eraser = document.getElementById("eraser");
eraser.addEventListener("click", (e) => {
e.target.classList.toggle("btn-on");
ERASE ? (ERASE = false) : (ERASE = true);
});
//
// Clear button
const clear = document.getElementById("clear");
clear.addEventListener("click", () => {
const pixel = document.querySelectorAll(".pixel");
pixel.forEach((pixel) => {
pixel.style.backgroundColor = "#ffffff";
});
});
// COLOR MODES
//
// Grayscale button - 1
const grayscaler = document.getElementById("grayscale");
grayscaler.addEventListener("click", (e) => {
if (COLORMODE[CURRENTMODE] === "grayscale") {
CURRENTMODE = 0;
} else {
CURRENTMODE = 1;
}
toggleButton();
});
//
// lighten button - 2
const lighten = document.getElementById("lighten");
lighten.addEventListener("click", (e) => {
if (COLORMODE[CURRENTMODE] === "lighten") {
CURRENTMODE = 0;
} else {
CURRENTMODE = 2;
}
toggleButton();
});
//
// rainbow button - 3
const rainbow = document.getElementById("rainbow");
rainbow.addEventListener("click", (e) => {
if (COLORMODE[CURRENTMODE] === "rainbow") {
CURRENTMODE = 0;
} else {
CURRENTMODE = 3;
}
toggleButton();
});
// Turns off other color mode when one is activated
function toggleButton() {
const buttons = document.querySelectorAll(".color-mode");
buttons.forEach((b) => {
if (COLORMODE[CURRENTMODE] === b.getAttribute("id")) {
b.classList.add("btn-on");
} else {
b.classList.remove("btn-on");
}
});
}
//
// Sliding bar for the grid size
const gridSlider = document.getElementById("grid-slider");
const gridValue = document.getElementById("grid-value");
if (!touchscreen) {
gridSlider.oninput = (e) => {
gridValue.innerHTML = `Grid size: ${e.target.value}x${e.target.value}`;
};
gridSlider.onmouseup = (e) => {
GRID = e.target.value;
makeGrid();
};
} else {
gridSlider.oninput = (e) => {
gridValue.innerHTML = `Grid size: ${e.target.value}x${e.target.value}`;
};
gridSlider.ontouchend = (e) => {
GRID = e.target.value;
makeGrid();
};
}
/* console.log(
"ontouchstart" in window,
navigator.maxTouchPoints > 0,
navigator.msMaxTouchPoints > 0
); */
//
// normal drawing mode
function normalDraw(e) {
e.style.backgroundColor = COLOR;
}
// erase mode
function eraseMode(e) {
if (e.style.backgroundColor !== undefined) {
e.style.backgroundColor = "#ffffff";
}
}
// grayscale mode
function grayscaleMode(e) {
// element doesnt initially have backgroundcolor propert even when I set it
// so i have to resort to this mess, kill me
if (e.style.backgroundColor !== undefined && e.style.backgroundColor !== "") {
let color = e.style.backgroundColor;
let regex = color.replace(/[^,0-9]/g, "").split(",");
let rgb = regex.map((c) => {
return parseInt(c);
});
let r = rgb[0] - 25.5;
let g = rgb[1] - 25.5;
let b = rgb[2] - 25.5;
e.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;
} else {
e.style.backgroundColor = "rgb(229.5,229.5,229.5)";
}
}
// ligthen mode
function ligthenMode(e) {
if (e.style.backgroundColor !== undefined && e.style.backgroundColor !== "") {
let color = e.style.backgroundColor;
let regex = color.replace(/[^,0-9]/g, "").split(",");
let rgb = regex.map((c) => {
return parseInt(c);
});
let r = rgb[0] + 25.5;
let g = rgb[1] + 25.5;
let b = rgb[2] + 25.5;
e.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;
}
}
// rainbow mode
function rainbowMode(e) {
let r = Math.floor(Math.random() * 256);
let g = Math.floor(Math.random() * 256);
let b = Math.floor(Math.random() * 256);
e.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;
}