-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
53 lines (44 loc) · 1.89 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
function createGrid(gridNumber) {
for (let i = 1; i <= (gridNumber ** 2); i++) {
let item = document.createElement('div');
item.setAttribute('class', 'item');
item.addEventListener('mouseover', function(){changeColour(item)}, false);
container.appendChild(item);
}
container.style.gridTemplateColumns = `repeat(${gridNumber}, 1fr)`;
container.style.gridTemplateRows = `repeat(${gridNumber}, 1fr)`;
}
function resetNewGrid() {
/* prompt */
let newGridNumber = prompt("Enter number of squares per side (1~100): ");
while (isNaN(parseInt(newGridNumber)) || !Number.isInteger(parseFloat(newGridNumber)) ||
parseInt(newGridNumber) < 1 || parseInt(newGridNumber) > 100) {
newGridNumber = prompt("Enter number of squares per side (1~100): ", "1~100");
}
newGridNumber = parseInt(newGridNumber);
/* remove old grid */
const items = document.querySelectorAll('.item');
items.forEach(e => e.remove());
/* create new grid */
createGrid(newGridNumber);
}
function changeColour(item) {
let red = Math.floor(Math.random() * 256);
let green = Math.floor(Math.random() * 256);
let blue = Math.floor(Math.random() * 256);
item.style.backgroundColor = `rgb(${red}, ${green}, ${blue})`;
const currentBrightness = parseFloat(window.getComputedStyle(item).getPropertyValue('filter').match(/\d/g).join('.'));
let newBrightness = currentBrightness;
if (currentBrightness > 0) {
newBrightness = currentBrightness - 0.1;
}
item.style.filter = `brightness(${newBrightness})`;
}
const container = document.querySelector('.container');
const body = document.body;
createGrid(16);
const resetButton = document.createElement('button');
resetButton.classList.add('button');
resetButton.textContent = 'Reset';
body.insertBefore(resetButton, container);
resetButton.addEventListener('click', resetNewGrid, false);