-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
94 lines (84 loc) · 2.74 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
//Create global variables for DOM manipulation
const grid = document.querySelector("#grid");
const black = document.querySelector("#draw_black");
const erase = document.querySelector("#erase");
const clear = document.querySelector("#clear");
const rainbow = document.querySelector("#rainbow");
const range = document.querySelector('#range');
let label = document.querySelector('label');
let defaultGrid = 16;
let cellsArray = []; //Used to manipulate dom through pushing grid cells to it
let gridWidth = grid.clientWidth;
let gridHeight = grid.clientHeight;
label.textContent = `Size = ${range.value}`
function randomColor() {
//function the returns a random hex color
let colors = "0123456789ABCDEF";
let hex = "#";
for (let i = 0; i < 6; i++) {
hex += colors[Math.floor(Math.random() * colors.length)];
}
return hex;
}
function createGrid(size) {
//Function to create a grid
//Instead of creating all 256 divs in one for loop create a nested for loop
for (let i = 0; i < size; i++) {
//columns
let div_column = document.createElement("div");
grid.appendChild(div_column);
cellsArray.push(div_column);
div_column.style.width = gridWidth / size + "px";
for (let j = 0; j < size; j++) {
//rows
let div_row = document.createElement("div");
grid.appendChild(div_row);
div_row.style.width = gridWidth / size + "px";
cellsArray.push(div_row);
}
}
}
createGrid(defaultGrid)
//remove function to change grid size and add input range with event listener to update grid based on range input
range.addEventListener('change', (e) => {
cellsArray.length = 0;
while(grid.lastElementChild) {
grid.removeChild(grid.lastElementChild)
}
let val = e.target.value;
label.textContent = `Size = ${val}`
createGrid(+val);
})
//Function to change background color of each cell on mouse over
function changeBackgroundToBlack() {
for (const cell of cellsArray) {
cell.addEventListener("mouseover", () => {
cell.style.backgroundColor = "black";
});
}
}
//function to reverse background color changes to none (acting as an erase)
function eraseGrid() {
cellsArray.forEach((cell) => {
cell.addEventListener("mouseover", () => {
cell.style.background = "";
});
});
}
//function to change background color to random color
function rainbowStyle() {
for (const cell of cellsArray) {
cell.addEventListener("mouseover", () => {
cell.style.backgroundColor = randomColor();
});
}
}
//Event listeners on the buttons
black.addEventListener("click", changeBackgroundToBlack);
erase.addEventListener("click", eraseGrid);
rainbow.addEventListener("click", rainbowStyle);
clear.addEventListener("click", () => {
cellsArray.forEach((cell) => {
cell.style.background = "";
});
});