-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
289 lines (245 loc) · 8.43 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
//HTML elements
const container = document.querySelector(".container");
const penColor = document.querySelector("#pen-color");
const background = document.querySelector("#background-color");
const pen = document.querySelector("#pen-card");
const rainbow = document.querySelector("#rainbow-card");
const random = document.querySelector("#random-card");
const btnClear = document.querySelector(".clear");
const gridTemplate = document.querySelector("#grid-template");
const gridSizeDisplay = document.querySelector("h3");
const penAnimation = document.querySelector("#behind-pen");
const rainbowAnimation = document.querySelector("#behind-rainbow");
const randomAnimation = document.querySelector("#behind-random");
const info = document.querySelector("#info-btn");
const reference = document.querySelector(".reference");
const gridBtn = document.querySelector("#grid-card");
const gridAnimation = document.querySelector("#behind-grid");
const rubber = document.querySelector("#rubber-card");
const rubberAnimation = document.querySelector("#behind-rubber");
const lighten = document.querySelector("#lighten-card");
const lightenAnimation = document.querySelector("#behind-lighten");
//global var
const defaultJSON = JSON.stringify(null);
let gridSize = +JSON.parse(localStorage.getItem("gridSize") ?? defaultJSON) || 16;
let smallSquares;
let color = JSON.parse(localStorage.getItem("color") ?? defaultJSON) || "#000000";
let bkgColor = JSON.parse(localStorage.getItem("bkgColor") ?? defaultJSON) || "#ffffff";
let actionItem = JSON.parse(localStorage.getItem("actionItem") ?? defaultJSON) || "pen";
let mousedown = false;
//active local storage
switchItem([actionItem]);
gridSizeDisplay.innerHTML = `${gridSize}x${gridSize}`;
document.querySelector('input[type="range"]').value = gridSize;
penColor.value = color;
background.value = bkgColor;
container.style.backgroundColor = bkgColor;
createGrid();
randomBkgColor();
penColor.addEventListener("input", changePenColor);
background.addEventListener("input", changeBkgColor);
pen.addEventListener("click", switchItem.bind(this, ["pen"]));
rainbow.addEventListener("click", switchItem.bind(this, ["rainbow"]));
random.addEventListener("click", switchItem.bind(this, ["random"]));
lighten.addEventListener("click", switchItem.bind(this, ["lighten"]));
rubber.addEventListener("click", switchItem.bind(this, ["rubber"]));
gridTemplate.addEventListener("input", changeGridTemplate);
btnClear.addEventListener("click", clear);
info.addEventListener("mouseover", removeClass);
info.addEventListener("mouseleave", addClass);
gridBtn.addEventListener("click", toggleGrid);
function createGrid() {
const squares = JSON.parse(localStorage.getItem("squares")) || [];
container.style.cssText += `grid-template-columns: repeat(${gridSize}, 1fr); grid-template-rows: repeat(${gridSize}, 1fr)`;
for (let i = 0; i < gridSize ** 2; i++) {
const smallSquare = document.createElement("div");
smallSquare.classList.add("small-square");
if (squares[i] && squares[i] !== "#0") {
smallSquare.style.backgroundColor = squares[i];
}
if (
gridAnimation.classList.length > 1 ||
JSON.parse(localStorage.getItem("grid")) === true
) {
gridAnimation.classList.add("option-active");
smallSquare.classList.add("border-top-left");
}
container.appendChild(smallSquare);
smallSquare.addEventListener("mousedown", (e) => {
mousedown = true;
action(e.target);
});
smallSquare.addEventListener("mouseup", () => {
mousedown = false;
});
smallSquare.addEventListener("mouseenter", (e) => {
if (mousedown) {
action(e.target);
}
});
/* smallSquare.setAttribute("ondragstart", "return false"); */
smallSquare.addEventListener("dragstart", () => mousedown = false);
}
smallSquares = [...document.querySelectorAll(".small-square")];
}
function action(smallSquare) {
const squareStyle = smallSquare.style;
switch (actionItem) {
case "pen":
squareStyle.backgroundColor = color;
break;
case "rainbow":
squareStyle.backgroundColor = rainbowColor();
break;
case "random":
squareStyle.backgroundColor = randomRGB();
break;
case "lighten":
squareStyle.backgroundColor = LightenDarkenColor(RGBToHex(squareStyle.backgroundColor), 30);
break;
case "rubber":
squareStyle.backgroundColor = "transparent";
break;
}
changeLocalStorageColors(squareStyle.backgroundColor, smallSquares.indexOf(smallSquare));
}
function randomColor() {
let colors = [];
for (let i = 0; i < 3; i++) {
colors[i] = Math.floor(Math.random() * 255);
}
return colors;
}
function randomRGB() {
const [red, blue, green] = randomColor();
return `rgb(${red}, ${blue}, ${green})`;
}
function rainbowColor() {
const rainbowColors = [
"#ff0000",
"#ffa500",
"#ffff00",
"#00ff00",
"#0000ff",
"#3f0fb7",
"#800080",
];
return rainbowColors[Math.floor(Math.random() * 6)];
}
function randomBkgColor() {
random.children[1].style.cssText = `background: linear-gradient(-45deg, ${randomRGB()}, ${randomRGB()}, ${randomRGB()}, ${randomRGB()}, ${randomRGB()}, ${randomRGB()}, ${randomRGB()});`;
}
function addClass() {
setTimeout(() => {
reference.classList.add("visibility");
}, 300);
}
function removeClass() {
reference.classList.remove("visibility");
}
function RGBToHex(RGB) {
if (!RGB.match("rgb")) return "#0";
if (RGB.match("#")) return RGB;
return RGB.slice(4, RGB.length - 1)
.split(",")
.reduce(
(hex, color) => (hex += Number(color).toString(16).padStart(2, "0")),
"#"
);
}
function squaresToArrOfHex() {
return [...document.querySelectorAll(".small-square")].map((el) =>
RGBToHex(el.style.backgroundColor)
);
}
function LightenDarkenColor(col, amt) {
if(col === "#0") return "transparent";
let usePound = false;
if (col[0] == "#") {
col = col.slice(1);
usePound = true;
}
let num = parseInt(col, 16);
let r = (num >> 16) + amt;
if (r > 255) r = 255;
else if (r < 0) r = 0;
let b = ((num >> 8) & 0x00ff) + amt;
if (b > 255) b = 255;
else if (b < 0) b = 0;
let g = (num & 0x0000ff) + amt;
if (g > 255) g = 255;
else if (g < 0) g = 0;
return (usePound ? "#" : "") + (g | (b << 8) | (r << 16)).toString(16);
}
function switchItem([item]) {
actionItem = item;
localStorage.setItem("actionItem", JSON.stringify(item));
penAnimation.classList.remove("color-active");
rainbowAnimation.classList.remove("color-active");
randomAnimation.classList.remove("color-active");
lightenAnimation.classList.remove("option-active");
rubberAnimation.classList.remove("option-active");
switch (item) {
case "pen":
penAnimation.classList.add("color-active");
break;
case "rainbow":
rainbowAnimation.classList.add("color-active");
break;
case "random":
randomAnimation.classList.add("color-active");
break;
case "lighten":
lightenAnimation.classList.add("option-active");
break;
case "rubber":
rubberAnimation.classList.add("option-active");
break;
default:
console.log("error");
break;
}
}
function clear() {
smallSquares.forEach((smallSquare) => {
smallSquare.style.backgroundColor = "transparent";
});
container.style.backgroundColor = bkgColor;
localStorage.removeItem("squares");
}
function changePenColor(e) {
e.stopImmediatePropagation();
color = e.target.value;
localStorage.setItem("color", JSON.stringify(color));
}
function changeBkgColor(e) {
bkgColor = e.target.value;
container.style.backgroundColor = bkgColor;
localStorage.setItem("bkgColor", JSON.stringify(bkgColor));
}
function changeGridTemplate(e) {
gridSize = e.target.value;
localStorage.setItem("gridSize", JSON.stringify(gridSize));
localStorage.removeItem("squares");
gridSizeDisplay.innerText = `${gridSize}x${gridSize}`;
container.replaceChildren([]);
createGrid();
}
function toggleGrid() {
gridAnimation.classList.toggle("option-active");
smallSquares.forEach((smallSquare) => {
smallSquare.classList.toggle("border-top-left");
});
localStorage.setItem(
"grid",
gridAnimation.classList.contains("option-active")
);
}
function changeLocalStorageColors(color, index) {
if(!localStorage.getItem("squares")) {
localStorage.setItem("squares", JSON.stringify(squaresToArrOfHex()));
}
let arr = JSON.parse(localStorage.getItem("squares"));
arr[index] = RGBToHex(color);
localStorage.setItem("squares", JSON.stringify(arr));
}