-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
233 lines (212 loc) · 7.53 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
const container = document.querySelector(".container");
let mode;
let containerSize = calculateContainerSize();
let colorPicker = document.querySelector(".color-picker");
let color = colorPicker.value;
let isMousePressed = false;
let label = document.querySelector("label");
let slider = document.getElementById("vol");
let dimensions = slider.value;
let updateTimeout;
const buttons = document.querySelectorAll("button");
//adjusts grid on the basis of slider value by throttling
slider.addEventListener("input", (e) => {
label.innerText = `Grid-Size(${e.target.value} x ${e.target.value})`;
dimensions = e.target.value; //new dimensions set on event change
if (updateTimeout) {
//updateTimeout is used to decrease rendering pressure on browser by rendering the grid after 0.3s
clearTimeout(updateTimeout);
}
updateTimeout = setTimeout(() => {
container.innerHTML = ""; // Clear existing grid items
createGridItems(); // Recreate grid items with new size
updateTimeout = null;
}, 300); // Throttle the update
});
//picks color from colorpicker input
colorPicker.addEventListener("input", (e) => {
if (e.target.value == "#00000") color = "#0a0a0a";
else color = e.target.value;
console.log(color);
});
// Calculate and set the container size
function calculateContainerSize() {
const viewportWidth = window.innerWidth;
const headerText = document.querySelector(".header-text");
console.log(viewportWidth);
if (viewportWidth < 640) {
headerText.style.fontSize = "35px";
return 350;
} else if (viewportWidth < 1140) {
headerText.style.fontSize = "50px";
return 400;
} else {
return 0.35 * viewportWidth;
}
}
//toggles buttons and also changes ui on turning off and on handles clear mode separately
buttons.forEach((button) => {
button.addEventListener("click", handleButtonClick);
});
function handleButtonClick(e) {
const button = e.target;
// Untoggle all buttons
buttons.forEach((otherButton) => {
if (otherButton !== button) {
otherButton.value = "OFF";
otherButton.style.backgroundColor = "white";
otherButton.style.color = "black";
}
});
if (button.value == "OFF") {
mode = button.className;
//clear mode handled separately
if (mode == "clear") {
button.style.backgroundColor = "black";
button.style.color = "white";
setTimeout(() => {
button.style.backgroundColor = "white";
button.style.color = "black";
mode = "";
clearing();
}, 500);
} else {
button.value = "ON";
button.style.backgroundColor = "black";
button.style.color = "white";
}
} else if (button.value == "ON") {
mode = "";
button.value = "OFF";
button.style.backgroundColor = "white";
button.style.color = "black";
}
}
//creates grid acc to the container size and dimensions specified
function createGridItems() {
containerSize = calculateContainerSize();
container.style.width = `${containerSize}px`;
container.style.height = `${containerSize}px`;
for (let i = 0; i < dimensions * dimensions; i++) {
const gridItem = document.createElement("div");
gridItem.classList.add("grid-item");
gridItem.draggable = false;
// gridItem.style.border = "1px solid #F5F5F5"; //this can be used to add grid but omitting for now
gridItem.style.width = `${containerSize / dimensions}px`;
gridItem.style.height = `${containerSize / dimensions}px`;
gridItem.style.filter = "brightness(100%)";
container.appendChild(gridItem);
}
}
createGridItems();
// Update container size on window resize
window.addEventListener("resize", () => {
const newSize = calculateContainerSize();
if (newSize !== containerSize) {
if (newSize !== containerSize) {
container.innerHTML = ""; // Clear existing grid items
createGridItems(); // Recreate grid items with new size
}
}
});
// Add a mousedown event listener on the container to handle stopping the actions
container.addEventListener("mousedown", () => {
isMousePressed = true;
container.addEventListener("mouseover", handleMouseOver);
});
// Add a mouseup event listener on the window to handle stopping the actions
window.addEventListener("mouseup", () => {
isMousePressed = false;
container.removeEventListener("mouseover", handleMouseOver);
});
//for hnadling mobile touches//////////////////////////////////////////////
container.addEventListener("touchstart", () => {
isMousePressed = true;
container.addEventListener("touchmove", handleTouchMove);
});
// Add a touchend event listener on the window to handle stopping the actions
window.addEventListener("touchend", () => {
isMousePressed = false;
container.removeEventListener("touchmove", handleTouchMove);
});
function handleTouchMove(e) {
// Touch event might have multiple touches, handle each one
for (const touch of e.changedTouches) {
const target = document.elementFromPoint(touch.clientX, touch.clientY);
if (target && target.classList.contains("grid-item")) {
handleMouseOver({ target });
}
}
}
//////////////////////////////////////////////////////////////////////////
//adds mouseover event works when mouse is pressed else returns
function handleMouseOver(e) {
if (!isMousePressed) return; // Stop if mouse is not pressed
if (mode === "color-mode") {
coloring(e);
} else if (mode === "rainbow-mode") {
rainbow(e);
} else if (mode === "darken-mode") {
darken(e);
} else if (mode === "lighten-mode") {
lighten(e);
} else if (mode === "eraser") {
erasing(e);
}
}
//function for coloring the grids with selected color
function coloring(e) {
if (isMousePressed && e.target.classList.contains("grid-item")) {
e.target.style.filter = `brightness(100%)`;
e.target.style.backgroundColor = color;
}
}
//function for erasing the colored griditems
function erasing(e) {
if (isMousePressed && e.target.classList.contains("grid-item")) {
e.target.style.filter = `brightness(100%)`;
e.target.style.backgroundColor = container.style.backgroundColor;
}
}
//function for coloring the grids with random color
function rainbow(e) {
if (isMousePressed && e.target.classList.contains("grid-item")) {
let letters = "0123456789ABCDEF";
let randoColor = "#";
for (let i = 0; i < 6; i++) {
randoColor += letters[Math.floor(Math.random() * 16)]; //returns random hex value
}
e.target.style.filter = `brightness(100%)`;
e.target.style.backgroundColor = randoColor;
}
}
//function for clearing the whole grid at once
function clearing() {
const gridItems = document.querySelectorAll(".grid-item");
gridItems.forEach((gridItem) => {
gridItem.style.backgroundColor = null;
gridItem.style.filter = `brightness(100%)`;
});
}
//function for darkening the color
function darken(e) {
if (isMousePressed && e.target.classList.contains("grid-item")) {
const currentBrightness = parseFloat(
e.target.style.filter.replace(/[^\d.]/g, "")
);
const newBrightness = Math.max(currentBrightness - 10, 0);
e.target.style.filter = `brightness(${newBrightness}%)`;
//darkens 10% each time mouse is hovered min value 0;
}
}
function lighten(e) {
if (isMousePressed && e.target.classList.contains("grid-item")) {
const currentBrightness = parseFloat(
e.target.style.filter.replace(/[^\d.]/g, "")
);
// const newBrightness = Math.min(currentBrightness + 10, 500);
const newBrightness = currentBrightness + 40;
e.target.style.filter = `brightness(${newBrightness}%)`;
//lightens 40% each time mouse is hovered no max value but when added won't make the color disappear(white);
}
}