/
script.js
101 lines (83 loc) · 2.62 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
function modContainer(row, col){
gridRowsString = [];
for(i = 0; i < row; i++){
gridRowsString.push("auto");
}
gridRowsString = gridRowsString.join(" ");
cellContainer.style.gridTemplateRows = gridRowsString;
cellContainer.style.gridTemplateColumns = gridRowsString;
}
function createCells(row, col){
for(i = 0; i < row; i++){
for(j = 0; j < col; j++){
let div = document.createElement("div");
div.classList.add("cell");
div.addEventListener("mouseover", colorCellsBlack);
cellContainer.appendChild(div);
}
}
}
function colorCellsBlack(e){
e.target.style.backgroundColor = "black";
}
function colorCellsRainbow(e){
let randomColor = getRandomColor();
e.target.style.backgroundColor = randomColor;
}
function getRandomColor(){
let hexDigits = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
let color = "#"
for(i = 0; i < 6; i++){
color += hexDigits[Math.floor(Math.random() * 16)];
}
return color;
}
function createContainer(){
cellContainer = document.createElement("div");
cellContainer.setAttribute("id", "cellContainer");
overallContainer.appendChild(cellContainer);
}
function removeContainer(){
overallContainer.removeChild(cellContainer);
}
//Initial Set up
let cellContainer = document.createElement("div");
cellContainer.setAttribute("id", "cellContainer");
let overallContainer = document.getElementById("overallContainer");
overallContainer.appendChild(cellContainer);
let isRainbowColors = false;
//Button Stuff
let resetButton = document.getElementById("resetButton");
let rainbowButton = document.getElementById("rainbowButton");
let blackButton = document.getElementById("blackButton");
resetButton.addEventListener("click", function(e){
let rows = Number(prompt("How big should the grid be?"));
if(!Number.isInteger(rows) || rows < 1 || rows > 64){
window.alert("Please Enter a Valid Number between 1 and 64");
return;
}
removeContainer();
createContainer(rows, rows);
modContainer(rows, rows);
createCells(rows, rows);
})
rainbowButton.addEventListener("click", function(e){
let cells = document.querySelectorAll(".cell");
cells.forEach(function(element){
element.removeEventListener("mouseover", colorCellsBlack);
element.addEventListener("mouseover", colorCellsRainbow);
});
})
blackButton.addEventListener("click", function(e){
let cells = document.querySelectorAll(".cell");
cells.forEach(function(element){
element.removeEventListener("mouseover", colorCellsRainbow);
element.addEventListener("mouseover", colorCellsBlack);
})
})
//Initial Grid
let row = 16;
let col = 16;
let gridRowsString = [];
modContainer(row, col);
createCells(row, col);