-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (58 loc) · 2.08 KB
/
index.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
const EAS = (() => {
let container = document.querySelector("#container");
/* grid creation logic */
setGrid();
paintCell();
function setGrid(gridSize = 16) {
clearGrid();
container.style.setProperty("--cell", gridSize);
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
container.appendChild(document.createElement("div"));
}
}
paintCell();
}
function resetGrid() {
clearGrid();
setGrid();
}
function clearGrid() {
container.innerHTML = '';
}
/* bound logic to buttons */
(function () {
const setGridBtn = document.querySelector("#setgrid");
const resetGridBtn = document.querySelector("#resetgrid");
setGridBtn.addEventListener("click", () => {
let gridSize = prompt("Please, enter a value between 0 and 60 (excluding)");
while (gridSize < 1 || gridSize > 60) gridSize = prompt("PLEASE A VALUE IN RANGE 1 TO 60");
setGrid(gridSize);
})
resetGridBtn.addEventListener("click", () => resetGrid());
})();
/* paint a cell on mouseenter */
function getColor() {
let hue = Math.floor(Math.random() * 255);
let saturation = Math.floor(Math.random() * 100);
let lightness = Math.floor(Math.random() * 100);
return { hue, saturation, lightness }
/* let r = Math.floor(Math.random() * 255);
let g = Math.floor(Math.random() * 255);
let b = Math.floor(Math.random() * 255);
return { r, g, b }; */
}
function paintCell() {
let cells = document.querySelectorAll("#container div");
cells.forEach(cell => {
let cellColor = getColor();
cell.addEventListener("mouseenter", () => {
/* cell.style.backgroundColor = `rgb(${cellColor.r}, ${cellColor.g}, ${cellColor.b})`;
cellColor = { r: cellColor.r - 50, g: cellColor.g - 50, b: cellColor.g - 50} */
cell.style.backgroundColor = `hsl(${cellColor.hue}, ${cellColor.saturation}%, ${cellColor.lightness}%)`;
cellColor.lightness -= 10;
})
})
};
return { setGrid, resetGrid, getColor }
})()