-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
49 lines (40 loc) · 1.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
let resetButton = document.querySelector("#reset");
let container = document.getElementById("container");
let squaresPerSide = 0;
const blackRate = 255*0.1;
resetButton.addEventListener("click", (e)=>{
squaresPerSide= window.prompt("How many spares per side?");
resetGrid();
creategrid(squaresPerSide);
})
function changeSquareColor(e){
let colors = e.target.style.backgroundColor;
if (colors == "")
e.target.style.backgroundColor="#"+Math.floor((Math.random() * 0xffffff)).toString(16);
else{
colors=colors.split(",");
let red = parseInt(colors[0].slice(4),10);
let green = parseInt(colors[1],10);
let blue = parseInt(colors[2].slice(0,-1),10);
e.target.style.backgroundColor = "rgb( "+(red-blackRate)+","+(green-blackRate)+","+(blue-blackRate)+")";
}
}
function resetGrid(){
container.innerHTML = "";
}
function creategrid(squaresNumber){
let square;
for(let i = 0;i < squaresNumber;i++)
{
for(let j = 0;j < squaresNumber;j++)
{
square = document.createElement('div');
square.className= "grid_square";
square.addEventListener("mouseover", changeSquareColor);
container.appendChild(square);
}
}
container.style.gridTemplateRows = "repeat("+squaresNumber.toString()+", 1fr)";
container.style.gridTemplateColumns = "repeat("+squaresNumber.toString()+", 1fr)";
}
creategrid(16);