-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJS.js
63 lines (57 loc) · 1.72 KB
/
JS.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
function makeGrid(dimension) {
let container = document.createElement("div");
container.classList.add("container", "grid");
container.addEventListener("mousedown", changeColor);
container.addEventListener("mouseover", hovering);
container.addEventListener("mouseover", changeColor);
container.addEventListener("mouseleave", hovering);
for (let i = 0; i < dimension ** 2; i++) {
let div = document.createElement("div");
div.setAttribute("draggable", "false");
div.addEventListener("mouseleave", hovering);
container.appendChild(div).className = "cell";
}
container.addEventListener("contextmenu", (e) => {
e.preventDefault();
});
container.style.setProperty("--grid-rows", dimension);
container.style.setProperty("--grid-cols", dimension);
document.body.appendChild(container);
updateDisplay(dimension);
}
function hovering(e) {
e.target.classList.toggle("hovering");
}
function changeDimension() {
let dimension;
do {
dimension = prompt("How many rows/columns would you like? (Max 100)");
dimension = parseInt(dimension);
} while (dimension > 100);
if (!dimension) {
return;
}
container = document.querySelector(".container");
document.body.removeChild(container);
makeGrid(dimension);
}
function reset() {
cells = document.querySelectorAll(".cell");
cells.forEach((cell) => {
cell.classList.remove("colored");
});
}
function changeColor(e) {
e.preventDefault();
if (e.buttons == 1) {
e.target.classList.add("colored");
}
if (e.buttons == 2) {
e.target.classList.remove("colored");
}
}
function updateDisplay(dimension) {
display = document.getElementById("dimensionDisplay");
display.textContent = `${dimension} x ${dimension}`;
}
makeGrid(10);