-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
100 lines (85 loc) · 2.91 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
// size is the number of horizontal or vertical squares
const CONTAINER_WIDTH = 500;
const CONTAINER_HEIGHT = 500;
const DEFAULT_SQUARES_NUMBER = 32;
const DEFAULT_SQUARES_COLOR = "black";
const DEFAULT_SQUARES_BORDER = "1px solid black";
const container = document.querySelector(".container");
const toggleColorBtn = document.querySelector("#toggle-color");
const toggleBorderBtn = document.querySelector("#toggle-border");
const toggleBtns = document.querySelectorAll(".toggle");
const sizeBtn = document.querySelector("#size");
const clearBtn = document.querySelector("#clear");
let currentSize = DEFAULT_SQUARES_NUMBER;
let getCurrentColorMode = getDefaultColor;
let currentBorder = DEFAULT_SQUARES_BORDER;
function getCurrentSquaresWidthAndHeight() {
return CONTAINER_WIDTH / currentSize;
}
function createGrid() {
const squareWidthAndHeight = getCurrentSquaresWidthAndHeight();
for (let i = 0; i < currentSize**2; i++) {
const square = document.createElement("div");
square.style.width = `${squareWidthAndHeight}px`;
square.style.height = `${squareWidthAndHeight}px`;
square.style.border = currentBorder;
container.appendChild(square);
}
}
function getDefaultColor() {
return DEFAULT_SQUARES_COLOR;
}
function getRainbowColor() {
return `rgb(${Math.floor(Math.random()*256)},
${Math.floor(Math.random()*256)},
${Math.floor(Math.random()*256)})`;
}
container.addEventListener("mouseover", e => {
if (e.target.className === "container") return; // ignore hovering on the parent container
e.target.style.backgroundColor = getCurrentColorMode();
});
toggleColorBtn.addEventListener("click", e => {
getCurrentColorMode = (getCurrentColorMode === getDefaultColor)? getRainbowColor : getDefaultColor;
});
toggleBorderBtn.addEventListener("click", e => {
const squares = container.children;
if (squares[0].style.border === DEFAULT_SQUARES_BORDER) {
for (const square of squares) {
square.style.border = "";
}
currentBorder = "";
}
else {
for (const square of squares) {
square.style.border = DEFAULT_SQUARES_BORDER;
}
currentBorder = DEFAULT_SQUARES_BORDER;
}
});
toggleBtns.forEach(button => {
button.addEventListener("click", e => {
button.classList.toggle("toggle-active");
});
});
sizeBtn.addEventListener("click", e => {
let size;
while(true) {
size = prompt("Enter number of square line (1 : 100)");
if (size === null || size === "") return;
size = +size;
if (Number.isInteger(size) && (size >= 1 && size <= 100)) {
break;
}
else {
alert("Enter a valid number");
}
}
currentSize = size;
container.innerHTML = "";
createGrid();
});
clearBtn.addEventListener("click", e => {
container.innerHTML = "";
createGrid();
});
createGrid();