-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.js
35 lines (30 loc) · 1.1 KB
/
javascript.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
const boardSize = 1000;
const genBtn = document.querySelector('#generate');
function generateBoard() {
genBtn.textContent = 'Clear Board';
genBtn.removeEventListener('click', generateBoard);
genBtn.addEventListener('click', clearBoard);
let gridSize = parseInt(prompt('Enter a value between 1 and 100'));
if (gridSize < 1 || gridSize > 100)
gridSize = 100;
let pixelSize = boardSize / gridSize;
for (let i = 0; i < gridSize * gridSize; i++) {
let pixel = document.createElement('div');
pixel.className = 'pixel';
pixel.style.width = `${pixelSize}px`;
pixel.style.height = `${pixelSize}px`;
pixel.addEventListener('mouseover', () => {
pixel.style.backgroundColor = 'black';
})
if (i % gridSize == 0)
pixel.style.className = 'new-row'
document.querySelector('.board').append(pixel);
}
}
function clearBoard() {
genBtn.textContent = 'Generate Board';
document.querySelector('.board').innerHTML = '';
genBtn.removeEventListener('click', clearBoard);
genBtn.addEventListener('click', generateBoard);
}
genBtn.addEventListener('click', generateBoard)