-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
54 lines (41 loc) · 1.7 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
document.addEventListener('DOMContentLoaded', () => {
const container = document.querySelector('.container');
const container_size = document.styleSheets[0].cssRules[0].style.getPropertyValue("width").slice(0,3);
const select = document.querySelector('.grid-size');
const clear = document.querySelector('.clear');
function createGrid() {
let grid_size = select.value;
while (container.hasChildNodes()) {
container.removeChild(container.lastChild);
}
for (let i = 0; i < grid_size; i++) {
let row = document.createElement('div');
row.classList.add('row');
container.appendChild(row);
for (let j = 0; j < grid_size; j++) {
let pixel = document.createElement('div');
pixel.classList.add('pixel-blank');
pixel.style.width = `${container_size / grid_size}px`
pixel.style.height = `${container_size / grid_size}px`
pixel.addEventListener('mouseover', () => {
pixel.classList.remove('pixel-blank');
pixel.classList.add('pixel-fill');
})
row.appendChild(pixel);
}
}
}
createGrid();
function clearGrid() {
const button = document.querySelector('.clear');
button.addEventListener('click', () => {
const pixels = document.querySelectorAll('.pixel-fill');
for (let pixel of pixels) {
pixel.classList.remove('pixel-fill');
pixel.classList.add('pixel-blank');
}
});
}
clearGrid();
select.onchange = createGrid;
});