-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
88 lines (50 loc) · 1.66 KB
/
app.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
const containerDiv = document.querySelector('#container');
let divArray = [];
let eraser = false;
const sizeButton = document.querySelector('#grid-size');
modifyDiv(16);
const penButton = document.querySelector('#pen');
const eraserButton = document.querySelector('#eraser');
penButton.onclick = () => {
eraser = false;
}
eraserButton.onclick = () => {
eraser = true;
}
sizeButton.addEventListener('click',() => {
let size;
do {
size = prompt("Enter a size between 0 and 100");
} while (size < 1 || size > 100)
removeDiv();
modifyDiv(size);
})
function createDiv(size) {
const gridDiv = document.createElement('div');
gridDiv.style.width = `${800/size}px`;
gridDiv.style.height = `${800/size}px`;
return gridDiv
}
function modifyDiv(size) {
const noOfGrid = size * size;
for(let i = 0; i < noOfGrid; i++) {
divArray.push(createDiv(size));
containerDiv.appendChild(divArray[i]);
divArray[i].addEventListener('mouseover',() => {
if(eraser == true) {
divArray[i].classList.remove('black');
divArray[i].classList.add('white');
}
else {
divArray[i].classList.remove('white');
divArray[i].classList.add('black');
}
})
}
}
function removeDiv() {
while (containerDiv.firstChild) {
containerDiv.removeChild(containerDiv.firstChild);
}
divArray = [];
}