-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
192 lines (166 loc) · 7.05 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
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
const bodyContainer = document.querySelector(".container-body");
const clearButtonContainer = document.querySelector(".container-clear-button");
const gridContainer = document.querySelector(".container-grid");
const header = document.querySelector("header");
const menu = document.querySelector(".menu");
const hamburgerIcon = document.querySelector(".hamburger-button");
const xHamburgerIcon = document.querySelector(".x-hamburger-button");
const menuOptionsContainer = document.querySelector(".container-menu-options");
const colorInputColorWheel = document.querySelector("#input-color");
const colorInputRainbowButton = document.querySelector("#rainbow-button");
const gridLinesButton = document.querySelector(".grid-lines-button");
const gridPieces = document.querySelector(".grid-pieces");
const gridBoxesInput = document.querySelector("#grid-boxes-input");
const clearButton = document.querySelector(".clear-button");
const toggleSwitch = document.querySelector("#toggle-switch");
window.addEventListener("load", makeGrid(16));
let colorChoiceColorWheel = "on";
let colorChoiceRainbow = "off";
colorInputColorWheel.value = "rgb(0,0,0)";
colorInputColorWheel.style.backgroundColor = colorInputColorWheel.value + "";
function selectColorWheel() {
colorInputRainbowButton.style.boxShadow = "none";
colorInputRainbowButton.style.border = "3px solid black";
colorInputRainbowButton.style.color = "black";
colorInputColorWheel.style.boxShadow = "0 0 6px 3px white";
colorInputColorWheel.style.border = "3px solid white";
colorInputColorWheel.style.backgroundColor = colorInputColorWheel.value + "";
colorChoiceColorWheel = "on";
colorChoiceRainbow = "off";
}
function selectRainbowButton() {
colorInputColorWheel.style.boxShadow = "none";
colorInputColorWheel.style.border = "3px solid black";
colorInputRainbowButton.style.boxShadow = "0 0 6px 3px white";
colorInputRainbowButton.style.border = "3px solid white";
colorInputRainbowButton.style.color = "white";
colorChoiceColorWheel = "off";
colorChoiceRainbow = "on";
}
menu.style.height = window.innerHeight + "px";
menuOptionsContainer.style.height = ((window.innerHeight) - (0.1 * window.innerWidth) - (0.165 * window.innerHeight)) + "px";
function colorPixel() {
if (colorChoiceRainbow === "on") {
this.style.backgroundColor = `hsl(${Math.random() * 360}, 100%, 50%)`;
console.log(this.style.backgroundColor);
}
else {
this.style.backgroundColor = colorInputColorWheel.value + "";
console.log(this.style.backgroundColor);
}
}
function resetGrid(parent) {
while (parent.firstChild) {
parent.removeChild(parent.firstChild);
}
}
function makeGrid(gridSize) {
resetGrid(gridContainer);
gridContainer.style.gridTemplateColumns = `repeat(${gridSize}, 1fr)`;
gridContainer.style.gridTemplateRows = `repeat(${gridSize}, 1fr)`;
for (let i = 0; i < (gridSize * gridSize); i++) {
const newGridDiv = document.createElement("div");
newGridDiv.classList = "grid-pieces";
gridContainer.appendChild(newGridDiv);
newGridDiv.style.backgroundColor = "white";
}
}
function changeGridSize() {
let userBoxesNumber = parseInt(gridBoxesInput.value);
if (userBoxesNumber > 100) {
userBoxesNumber = 100;
}
else if (userBoxesNumber < 1) {
userBoxesNumber = 1;
}
makeGrid(userBoxesNumber);
let gridPixels = gridContainer.querySelectorAll('div');
if (toggleSwitch.checked) {
// gridPixels.forEach(gridPixel => gridPixel.removeEventListener('mouseover', colorPixel));
gridPixels.forEach(gridPixel => gridPixel.addEventListener('click', colorPixel));
}
else {
// gridPixels.forEach(gridPixel => gridPixel.removeEventListener('click', colorPixel));
gridPixels.forEach(gridPixel => gridPixel.addEventListener('mouseover', colorPixel));
}
if (gridLinesButton.classList.contains("grid-lines-on")) {
for (let i = 0; i < gridContainer.children.length; i++) {
gridContainer.children[i].style.border = "1px solid rgba(85, 85, 85, 0.685)";
}
}
else {
for (let i = 0; i < gridContainer.children.length; i++) {
gridContainer.children[i].style.border = "none";
}
}
}
function selectGridLines() {
let userBoxesNumber = parseInt(gridBoxesInput.value);
if (userBoxesNumber > 100) {
userBoxesNumber = 100;
gridBoxesInput.value = "100";
}
else if (userBoxesNumber < 1) {
userBoxesNumber = 1;
gridBoxesInput.value = "1";
}
if (gridLinesButton.classList.contains("grid-lines-on")) {
gridLinesButton.classList.remove("grid-lines-on");
gridLinesButton.style.color = "black";
gridLinesButton.style.border = "3px solid black";
gridLinesButton.style.boxShadow = "none";
}
else {
gridLinesButton.classList.add("grid-lines-on");
gridLinesButton.style.color = "white";
gridLinesButton.style.border = "3px solid white";
gridLinesButton.style.boxShadow = "0 0 6px 3px white";
}
if (gridLinesButton.classList.contains("grid-lines-on")) {
for (let i = 0; i < gridContainer.children.length; i++) {
gridContainer.children[i].style.border = "1px solid rgba(85, 85, 85, 0.685)";
}
}
else {
for (let i = 0; i < gridContainer.children.length; i++) {
gridContainer.children[i].style.border = "none";
}
}
}
gridBoxesInput.addEventListener("change", changeGridSize);
gridLinesButton.addEventListener("click", selectGridLines);
// window.addEventListener("load", makeGrid(16));
function displayMenu() {
menu.classList.add("showMenu");
menu.classList.remove("hideMenu");
return;
}
function closeMenu() {
menu.classList.add("hideMenu");
menu.classList.remove("showMenu");
bodyContainer.style.filter = "none";
return;
}
function clearGrid() {
let gridPixels = gridContainer.querySelectorAll('div');
gridPixels.forEach(gridPixel => gridPixel.style.backgroundColor = "white");
}
colorInputColorWheel.addEventListener("input", selectColorWheel);
colorInputRainbowButton.addEventListener("click", selectRainbowButton);
let gridPixels = gridContainer.querySelectorAll('div');
gridPixels.forEach(gridPixel => gridPixel.addEventListener('mouseover', colorPixel));
function hoverToggle() {
let gridPixels2 = gridContainer.querySelectorAll('div');
if (toggleSwitch.checked) {
gridPixels2.forEach(gridPixels2 => gridPixels2.removeEventListener('mouseover', colorPixel));
gridPixels2.forEach(gridPixels2 => gridPixels2.addEventListener('click', colorPixel));
}
else {
gridPixels2.forEach(gridPixels2 => gridPixels2.removeEventListener('click', colorPixel));
gridPixels2.forEach(gridPixels2 => gridPixels2.addEventListener('mouseover', colorPixel));
}
}
toggleSwitch.addEventListener("change", hoverToggle);
clearButton.addEventListener("click", clearGrid);
hamburgerIcon.addEventListener("click", displayMenu);
xHamburgerIcon.addEventListener("click", closeMenu);