-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
322 lines (271 loc) · 10.2 KB
/
script.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/// CREATE HTML ELEMENTS
const body = document.querySelector("body");
const header = document.getElementById("header");
const canvas = document.getElementById("canvas");
const buttons = document.getElementById("buttons");
const footer = document.getElementById("footer");
const title = document.createElement("h1")
title.textContent = "Etch-a-Sketch"
const subTitle = document.createElement("p")
subTitle.textContent = "Create your amazing pixel art!"
const colorBtn = document.createElement("input");
colorBtn.type = "color";
colorBtn.value = "#494752";
const blackBtn = document.createElement("button");
blackBtn.textContent = "Black Pen";
const randomBtn = document.createElement("button");
randomBtn.textContent = "Random Effect";
const shadowBtn = document.createElement("button");
shadowBtn.textContent = "Shadow Effect";
const eraserBtn = document.createElement("button");
eraserBtn.textContent = "Eraser";
const clearBtn = document.createElement("button");
clearBtn.textContent = "Clear";
const sizeSelector = document.createElement("input");
sizeSelector.type = "range";
sizeSelector.min = 2;
sizeSelector.max = 100;
sizeSelector.defaultValue = 16;
let sizeNum = document.createElement("p");
sizeNum.textContent = sizeSelector.value + " X " + sizeSelector.value;
sizeNum.style.color = "white";
const footText = document.createElement("p");
footText.textContent = "Created by";
const githubIcon = document.createElement("i");
githubIcon.classList.add("devicon-github-original");
githubIcon.textContent = " araujodanield";
const github = document.createElement("a")
github.setAttribute("href", "https://github.com/araujodanield");
github.setAttribute("target", "_blank");
github.appendChild (githubIcon);
header.appendChild(title).classList.add("title");
header.appendChild(subTitle).classList.add("sub-title");
buttons.appendChild(colorBtn).classList.add("btn", "color-window");
buttons.appendChild(blackBtn).classList.add("btn");
buttons.appendChild(randomBtn).classList.add("btn");
buttons.appendChild(shadowBtn).classList.add("btn");
buttons.appendChild(eraserBtn).classList.add("btn");
buttons.appendChild(clearBtn).classList.add("btn", "clear-btn");
buttons.appendChild(sizeSelector).classList.add("size-slider");
buttons.appendChild(sizeNum).classList.add("size-text");
footer.appendChild(footText);
footer.appendChild(github);
/// CREATE CANVAS
// Add a pixel amount to the canvas with CSS Grid to give all pixels the same size.
function createPixels(size) {
canvas.style.gridTemplateColumns = `repeat(${size} , 1fr)`;
canvas.style.gridTemplateRows = `repeat(${size} , 1fr)`;
for (let i = 0; i < size * size; i++) {
let px = document.createElement("div");
px.style.border = "1px solid rgba(0, 0, 0, 0.1)";
canvas.appendChild(px).classList.add("pixel");
}
};
// Default size when the page is loaded: 16x16.
createPixels(sizeSelector.value);
/// DRAWING TOOLS
// Select all the divs created by createPixels function.
let pixels = document.querySelectorAll(".pixel");
// Applied on the canvas. With this, the user can only draw when the left mouse button is pressed.
let mouseIsClicked;
window.addEventListener("mousedown", () => {
mouseIsClicked = true;
});
window.addEventListener("mouseup", () => {
mouseIsClicked = false;
});
// Applied on buttons. Remove the "pressed" style from other buttons when the user press a new one.
function removePreviousPressed() {
if (colorBtn) {
blackBtn.classList.remove("black-btn");
randomBtn.classList.remove("random-btn");
shadowBtn.classList.remove("shadow-btn");
eraserBtn.classList.remove("eraser-btn");
};
if (blackBtn) {
colorBtn.classList.remove("color-btn");
randomBtn.classList.remove("random-btn");
shadowBtn.classList.remove("shadow-btn");
eraserBtn.classList.remove("eraser-btn");
};
if (randomBtn) {
colorBtn.classList.remove("color-btn");
blackBtn.classList.remove("black-btn");
shadowBtn.classList.remove("shadow-btn");
eraserBtn.classList.remove("eraser-btn");
};
if (shadowBtn) {
colorBtn.classList.remove("color-btn");
blackBtn.classList.remove("black-btn");
randomBtn.classList.remove("random-btn");
eraserBtn.classList.remove("eraser-btn");
};
if (eraserBtn) {
colorBtn.classList.remove("color-btn");
blackBtn.classList.remove("black-btn");
randomBtn.classList.remove("random-btn");
shadowBtn.classList.remove("shadow-btn");
};
};
// Default pen when the page is loaded.
function defaultPen() {
pixels.forEach (pixel => {
pixel.addEventListener("mousedown", () => {
pixel.style.backgroundColor = colorBtn.value;
});
pixel.addEventListener("mouseover", () => {
if (mouseIsClicked) {
pixel.style.backgroundColor = colorBtn.value;
};
});
});
};
// Allow the user to select a custom color or copy any color on screen.
function colorPicker() {
colorBtn.addEventListener("click", () => {
removePreviousPressed();
colorBtn.classList.add("color-btn");
pixels.forEach (pixel => {
pixel.addEventListener("mousedown", () => {
pixel.style.backgroundColor = colorBtn.value;
});
pixel.addEventListener("mouseover", () => {
if (mouseIsClicked) {
pixel.style.backgroundColor = colorBtn.value;
};
});
});
});
};
// A black pen.
function blackPen() {
blackBtn.addEventListener("click", () => {
removePreviousPressed();
blackBtn.classList.add("black-btn");
pixels.forEach (pixel => {
pixel.addEventListener("mousedown", () => {
pixel.style.backgroundColor = "black";
});
pixel.addEventListener("mouseover", () => {
if (mouseIsClicked) {
pixel.style.backgroundColor = "black";
};
});
});
});
};
// Add a random color for each pixel on interaction.
function randomPen() {
randomBtn.addEventListener("click", () => {
removePreviousPressed();
randomBtn.classList.add("random-btn");
pixels.forEach (pixel => {
pixel.addEventListener("mousedown", () => {
pixel.style.backgroundColor = "none";
});
pixel.addEventListener("mouseover", () => {
let r = Math.floor(Math.random() * 255);
let g = Math.floor(Math.random() * 255);
let b = Math.floor(Math.random() * 255);
if (mouseIsClicked) {
pixel.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;
};
});
});
});
};
// Adds black color with 0.1 opacity to the pixel and increases its value on each interaction until reach 1.
// OBS: There are many ways to do this logic with less code, but to make this the most visual and "beginner friendly" possible, I opted for an 'if else' statement.
function shadowPen() {
shadowBtn.addEventListener("click", () => {
removePreviousPressed();
shadowBtn.classList.add("shadow-btn");
pixels.forEach (pixel => {
let opacity = 0.1;
let changeOpacity = () => {
if (opacity === 0.1) {
opacity = 0.2;
} else if (opacity === 0.2) {
opacity = 0.3;
} else if (opacity === 0.3) {
opacity = 0.4;
} else if (opacity === 0.4) {
opacity = 0.5;
} else if (opacity === 0.5) {
opacity = 0.6;
} else if (opacity === 0.6) {
opacity = 0.7;
} else if (opacity === 0.7) {
opacity = 0.8;
} else if (opacity === 0.8) {
opacity = 0.9;
} else if (opacity === 0.9) {
opacity = 1;
};
};
pixel.addEventListener("mousedown", () => {
pixel.style.backgroundColor = `rgba(0, 0, 0, ${opacity})`;
changeOpacity();
});
pixel.addEventListener("mouseover", () => {
if (mouseIsClicked) {
pixel.style.backgroundColor = `rgba(0, 0, 0, ${opacity})`;
changeOpacity();
};
});
});
});
};
// Allow the user to delete pixel color individually.
function pixelEraser() {
eraserBtn.addEventListener("click", () => {
removePreviousPressed();
eraserBtn.classList.add("eraser-btn");
pixels.forEach (pixel => {
pixel.addEventListener("mousedown", () => {
pixel.style.backgroundColor = "";
});
pixel.addEventListener("mouseover", () => {
if (mouseIsClicked) {
pixel.style.backgroundColor = "";
};
});
});
});
};
// Clear the canvas by removing the color of all pixels.
function clearCanvas() {
clearBtn.addEventListener("click", () => {
pixels.forEach (pixel => {
pixel.style.backgroundColor = "";
});
});
};
/// CHANGE THE PIXEL AMOUNT IN THE CANVAS
// Allow the user to select a new size for the canvas, delete all the current pixel divs and then call the updateCanvas function.
function sizeSelect() {
sizeSelector.addEventListener("mousemove", () => {
sizeNum.textContent = sizeSelector.value + " X " + sizeSelector.value;
});
sizeSelector.addEventListener("change", () => {
const pixelAmount = body.querySelectorAll(".pixel")
pixelAmount.forEach (currentPx => currentPx.remove());
updateCanvas();
});
};
// Apply the selection from the size slider and re-select the class of the new pixels to allow the drawing tools to work.
function updateCanvas() {
createPixels(sizeSelector.value);
pixels = document.querySelectorAll(".pixel");
defaultPen();
removePreviousPressed();
};
/// Call all the tools functions.
defaultPen();
colorPicker();
blackPen();
randomPen();
shadowPen();
pixelEraser();
clearCanvas();
sizeSelect();