-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEtch-a-sketch.js
70 lines (56 loc) · 2.17 KB
/
Etch-a-sketch.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
const container = document.querySelector(".container");
const section = document.querySelector(".section");
const grayBtn = document.createElement("button");
const resetBtn = document.createElement("button");
const rainbowBtn = document.createElement("button");
const btnContainer = document.querySelector(".btn-container");
rainbowBtn.innerText = "Rainbow";
grayBtn.innerText = "Gray";
resetBtn.innerText = "Reset";
btnContainer.appendChild(resetBtn);
btnContainer.appendChild(grayBtn);
btnContainer.appendChild(rainbowBtn);
function createGrid(cols, rows) {
for (let i = 0; i < (cols * rows); i++) {
let box = document.createElement("div");
// box.classList.add("box");
container.appendChild(box).classList.add("box");
box.style.border = "1px solid black";
container.style.gridTemplateColumns = `repeat(${cols}, 1fr)`;
container.style.gridTemplateRows = `repeat(${rows}, 1fr)`;
}
}
function reset() {
const boxes = document.querySelectorAll(".box");
boxes.forEach(box => box.remove());
const userInput = prompt("Enter the number of grids, maximum: 100", 100);
if (userInput === null || userInput < 1) {
createGrid(16, 16);
grayColor();
} else {
createGrid(userInput, userInput);
grayColor();
}
}
function grayColor() {
const boxes = document.querySelectorAll(".box");
boxes.forEach(box => box.addEventListener("mouseover", () => {
const random = Math.floor(Math.random() * 256);
box.style.backgroundColor = `rgb(${random},${random},${random})`;
}))
}
function rainbowColor() {
const boxes = document.querySelectorAll(".box");
boxes.forEach(box => box.addEventListener("mouseover", () => {
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
box.style.backgroundColor = `rgb(${r},${g},${b})`;
}))
}
createGrid(16, 16);
grayColor();
rainbowBtn.addEventListener("click", rainbowColor);
grayBtn.addEventListener("click", grayColor);
resetBtn.addEventListener("click", reset);
// boxes.forEach(box => box.addEventListener("mouseover", changeColor))