-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
112 lines (94 loc) · 2.92 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
const gridDetails = {
gridSize: "16",
bw: "#333333",
rainbow: "",
eraser: "#ffffff",
picked: "",
};
const grid = document.querySelector(".grid");
const bwBtn = document.querySelector(".button--bw");
const rainbowBtn = document.querySelector(".button--rainbow");
const eraserBtn = document.querySelector(".button--eraser");
const clearBtn = document.querySelector(".button--clear");
const colorPicker = document.querySelector(".color-picker");
const sizePicker = document.querySelector(".size-picker__control");
const sizePickerCounter = document.querySelector(".size-picker__counter");
function init() {
bwBtn.addEventListener("click", paintBw);
rainbowBtn.addEventListener("click", paintRainbow);
eraserBtn.addEventListener("click", erase);
clearBtn.addEventListener("click", clear);
colorPicker.addEventListener("input", pickColor);
sizePicker.addEventListener("input", pickSize);
loadGrid();
paintBw();
}
function loadGrid() {
const size = gridDetails.gridSize ** 2;
for (let i = 0; i < size; i++) {
const element = document.createElement("div");
element.classList.add("grid__element");
grid.appendChild(element);
}
}
function paintRainbow() {
grid.querySelectorAll(".grid__element").forEach((element) => {
element.addEventListener("mouseenter", () => {
element.classList.add("grid__element--painted");
generateRandomColor();
element.style.backgroundColor = gridDetails.rainbow;
});
});
}
function paintBw() {
grid.querySelectorAll(".grid__element").forEach((element) => {
element.addEventListener("mouseenter", () => {
element.classList.add("grid__element--painted");
element.style.backgroundColor = gridDetails.bw;
});
});
}
function paintOwnColor() {
grid.querySelectorAll(".grid__element").forEach((element) => {
element.addEventListener("mouseenter", () => {
element.classList.add("grid__element--painted");
element.style.backgroundColor = gridDetails.picked;
});
});
}
function erase() {
grid.querySelectorAll(".grid__element").forEach((element) => {
element.addEventListener("mouseenter", () => {
element.classList.remove("grid__element--painted");
element.style.backgroundColor = gridDetails.eraser;
});
});
}
function clear() {
grid.textContent = "";
init();
}
function generateRandomColor() {
const red = getRandomNumber();
const green = getRandomNumber();
const blue = getRandomNumber();
gridDetails.rainbow = `rgb(${red}, ${green}, ${blue})`;
}
function getRandomNumber() {
return Math.floor(Math.random() * 256);
}
function pickColor() {
gridDetails.picked = this.value;
paintOwnColor();
}
function pickSize() {
sizePickerCounter.textContent = this.value;
gridDetails.gridSize = this.value;
setGridSize(this.value);
clear();
}
function setGridSize(size) {
grid.style.gridTemplateColumns = `repeat(${size}, 1fr)`;
grid.style.gridTemplateRows = `repeat(${size}, 1fr)`;
}
init();