-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
68 lines (51 loc) · 1.79 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
const container = document.querySelector(".container");
const containerLength = 600;
const normalBtn = document.querySelector("#normal");
const rainbowBtn = document.querySelector("#rainbow");
const eraseBtn = document.querySelector('#erase');
const changeSizeBtn = document.querySelector('#change-size');
let paintMode = "normal";
function makeSquareGrid(gridSize) {
container.textContent = "";
// cell dimensions
const cellLength = containerLength / gridSize;
for (let i = 0; i < (gridSize * gridSize); i++){
const cell = document.createElement("div");
cell.classList.add("cell");
cell.style.outline = "1px solid gray";
cell.style.height = `${cellLength}px`;
cell.style.width = `${cellLength}px`;
container.appendChild(cell);
cell.addEventListener("mouseover", () => {
if (paintMode === "normal"){
cell.style.backgroundColor = "black";
} else if (paintMode === "erase"){
cell.style.backgroundColor = "";
} else if (paintMode === "rainbow"){
const randR = Math.floor(Math.random()*256);
const randG = Math.floor(Math.random()*256);
const randB = Math.floor(Math.random()*256);
const randomColour = `rgb(${randR},${randG},${randB})`;
cell.style.backgroundColor = randomColour;
}
} );
}
}
normalBtn.addEventListener("click", () => {
paintMode = "normal";
})
rainbowBtn.addEventListener("click", () => {
paintMode = "rainbow";
})
eraseBtn.addEventListener("click", () => {
paintMode = "erase";
})
changeSizeBtn.addEventListener("click", () => {
let userSizeChoice = prompt("Enter a new grid size (1-100): ");
if (userSizeChoice > 100 || userSizeChoice < 1){
alert("Grid size must be between 1 & 100!");
} else {
makeSquareGrid(userSizeChoice);
}
})
makeSquareGrid(16);