-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
88 lines (78 loc) · 2.97 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
const container = document.querySelector(".inner-container");
const generateGridBtn = document.getElementById("generateGrid");
const userInput = document.getElementById("pixels_per_side");
const clearGridBtn = document.getElementById("clear_grid");
const eraserBtn = document.getElementById("eraser");
const gridWidth = 960;
const gridHeight = 660;
let eraserOn = false;
createGrid(16); //default grid is 16 x 16
generateGridBtn.addEventListener("click", () => {
let userInputVal = userInput.value;
if (!Number.isInteger((+userInputVal))//must convert argument to a number for Number.isInteger to work
|| userInputVal < 1
|| userInputVal > 100
|| isNaN(userInputVal)) {
alert("Please enter a whole number between 1 and 100 in the input field");
userInput.value = "";
} else {
container.innerHTML = "";
createGrid(userInputVal);
}
})
clearGridBtn.addEventListener("click", () => {
const pixels = document.getElementsByClassName("pixel");
for (let i = 0; i < pixels.length; i++) {
pixels[i].style.backgroundColor = `white`;
}
})
eraserBtn.addEventListener("click", () => {
eraserOn = !eraserOn;
if (eraserOn) {
eraserBtn.textContent = "Eraser: On"
} else {
eraserBtn.textContent = "Eraser: Off"
}
enableOrDisableColoring();
})
function createGrid(userInputVal) {
createGridRows(userInputVal);
createGridColumns(userInputVal);
enableOrDisableColoring();
}
function createGridRows(userInputVal) {
for (let i = 1; i <= userInputVal; i++) {
container.innerHTML += `<div class="row-style" style="
box-sizing:border-box;border: 1px solid black; width: 100%;height: ${gridHeight / userInputVal}px;display: flex;"></div>`
}
}
function createGridColumns(userInputVal) {
const rows = document.getElementsByClassName("row-style");
for (let i = 0; i < userInputVal; i++) {
rows[i].innerHTML += rows[i].innerHTML + createGridPixels(userInputVal);//adding #(userInputVal) divs with class "pixel" to each row
}
}
function createGridPixels(userInputVal) {//create individual grids
let pixelDivs = ``
for (let i = 1; i <= userInputVal; i++) {
pixelDivs += `<div class="pixel" style="width: ${gridWidth / userInputVal}px; height:${gridHeight / userInputVal}px;
box-sizing: border-box;border: 1px solid black; "></div>`;
}
return pixelDivs;
}
function enableOrDisableColoring() {
const pixels = document.getElementsByClassName("pixel");
for (let i = 0; i < pixels.length; i++) {
pixels[i].addEventListener("mouseover", () => {
if (!eraserOn) {
pixels[i].style.backgroundColor = `rgb(${getRGBValue()} ${getRGBValue()} ${getRGBValue()})`;
} else {
pixels[i].style.backgroundColor = `white`;
}
})
}
}
function getRGBValue() {
//random number from 0 (inclusive) to 256 (inclusive)
return (Math.floor(Math.random() * 256)).toString();
}