-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (56 loc) · 1.69 KB
/
index.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
let color = "black";
let click = false;
function populateBoard(size) {
let board = document.querySelector(".board");
let squares = board.querySelectorAll("div");
squares.forEach((div) => div.remove());
board.style.gridTemplateColumns = `repeat(${size}, 1fr)`; //this makes a 16x16 column
board.style.gridTemplateRows = `repeat(${size}, 1fr)`;
let amount = size * size;
for (let i = 0; i < amount; i++) {
let square = document.createElement("div");
square.addEventListener("mouseover", colorSquare);
square.style.backgroundColor = "white";
board.insertAdjacentElement("beforeend", square);
}
}
populateBoard(16); // Board is 16x16 by default
let error = document.querySelector(".error");
function changeSize(input) {
if (input >= 2 && input <= 100) {
error.style.display = "none";
populateBoard(input);
} else {
error.style.display = "flex";
error.textContent = "Input must be between 2 and 100";
}
}
function colorSquare() {
if (click) {
if (color === "random") {
this.style.backgroundColor = `hsl(${Math.random() * 360}, 100%,50%)`;
} else {
this.style.backgroundColor = color;
}
}
}
function changeColor(choice) {
color = choice;
}
function resetSquare() {
let board = document.querySelector(".board");
let squares = board.querySelectorAll("div");
squares.forEach((div) => (div.style.backgroundColor = "white"));
}
// Mode Toggle
let mode = document.querySelector(".mode");
document.querySelector(".board").addEventListener("click", (e) => {
if (e.target.tagName != "BUTTON") {
click = !click;
if (click) {
mode.textContent = "Mode: Coloring!";
} else {
mode.textContent = "Mode: Not coloring :(";
}
}
});