-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
56 lines (48 loc) · 1.41 KB
/
main.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
const grid = document.querySelector("#grid")
let size = 16
window.onload = function() {
grid.style.gridTemplateColumns = 'repeat(' + String(size) + ', 1fr)'
for(let i = 0; i < size*size; i++)
{
let d = document.createElement("div")
d.classList = "grid-block"
d.style = "background-color: white;"
d.addEventListener("mouseover", changeColour)
grid.appendChild(d)
}
}
function changeColour(e)
{
let r = Math.random() * 255
let g = Math.random() * 255
let b = Math.random() * 255
e.target.style.background = "rgb(" + String(r) + "," + String(g) + "," + String(b) + ")"
}
function changeSize()
{
let newSize = prompt("Enter new size (10 - 100)", "20")
newSize = parseInt(newSize)
if(!isNaN(newSize))
{
if(newSize >= 10 && newSize <= 100) {
grid.innerHTML = '';
grid.style.gridTemplateColumns = 'repeat(' + String(newSize) + ', 1fr)'
for(let i = 0; i < newSize*newSize; i++)
{
let d = document.createElement("div")
d.classList = "grid-block"
d.style = "background-color: white;"
d.addEventListener("mouseover", changeColour)
grid.appendChild(d)
}
size = newSize
}
}
}
function clearGrid()
{
for(e of grid.children)
{
e.style.background = "white"
}
}