-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
94 lines (74 loc) · 2.77 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
const GRID_LENGTH = 500;
let mouseDown = false;
let mouseEnter = false;
let selectedColor = "white";
const grid = document.querySelector(".grid");
function allowColorChange(square) {
// Multiple events to allow user to color while dragging their clicked mouse
square.addEventListener("mousedown", (event) => {
mouseDown = true;
if (mouseEnter && mouseDown) {
event.target.style.backgroundColor = selectedColor;
}
});
square.addEventListener("mouseenter", (event) => {
mouseEnter = true;
if (mouseEnter && mouseDown) {
event.target.style.backgroundColor = selectedColor;
}
});
}
function populateGrid(squaresPerRow=16) {
const gridSquares = grid.childNodes;
// Remove previous squares
if (gridSquares.length > 0) {
while (grid.firstChild) {
grid.removeChild(grid.firstChild);
}
}
const squareLength = GRID_LENGTH / squaresPerRow;
const totalSquares = squaresPerRow ** 2
// Add new squares
for (let i = 1; i <= totalSquares; i++) {
const newSquare = document.createElement("div");
newSquare.classList.add("square");
newSquare.style.width = squareLength + "px";
newSquare.style.height = squareLength + "px";
grid.appendChild(newSquare);
}
for (square of grid.children) allowColorChange(square);
const gridSizeHeading = document.querySelector(".grid-size-heading");
gridSizeHeading.innerText = `Grid Size: ${squaresPerRow} × ${squaresPerRow}`;
}
// Add event listeners to buttons
const colorBoxes = document.querySelectorAll(".color-box");
for (colorBox of colorBoxes) {
colorBox.addEventListener("click", (event) => {
const previouslySelected = document.querySelector(".selected");
previouslySelected.classList.remove("selected");
event.target.classList.add("selected");
selectedColor = event.target.getAttribute("id");
});
}
const resetButton = document.querySelector(".reset-button");
resetButton.addEventListener("click", () => {
// Keeps the same size but removes color
populateGrid(Math.sqrt(grid.children.length));
});
const changeGridSizeButton = document.querySelector(".change-grid-size-button");
changeGridSizeButton.addEventListener("click", () => {
let newGridLength = "";
while (typeof(newGridLength) !== "number" || newGridLength < 1 || newGridLength > 50) {
newGridLength = parseInt(prompt(
"Enter a number between 1 and 50 to change the number of squares on each side of the grid."
)) | "";
}
populateGrid(newGridLength);
})
// Prevents coloring by hovering mouse
document.body.addEventListener("mouseup", () => {
mouseDown = false;
mouseEnter = false;
});
// Make initial grid
populateGrid();