-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
82 lines (57 loc) · 2.04 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
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
80
81
82
document.addEventListener('DOMContentLoaded', () => {
let size = 16, color='#000000'
const buttonSize = document.querySelector('#button-size'),
inputColor = document.querySelector('#input-color'),
buttonRandom = document.querySelector('#button-random'),
buttonClear = document.querySelector('#button-clear'),
container = document.querySelector('.container-grid')
const etchSketch = (isSize=false) =>{
container.innerHTML = ''
buttonRandom.onclick = () => {
color = null
}
inputColor.addEventListener('change', (e) => {
color = e.target.value
})
const prevSize = size
if(isSize){
do {
size = prompt('Enter grid size (max 100):')
}while ( size > 100)
}
size ??= prevSize
const divStyle = `width: calc(100% / ${size});
height: calc(100% / ${size});
border: 1px solid #ddd;
box-sizing: border-box;`,
gridSize = size ** 2
for (let i = 0; i < gridSize; i++) {
const gridSquare = document.createElement('div')
gridSquare.style.cssText = divStyle
const setColor = (color) => {
gridSquare.style.backgroundColor = color
}
gridSquare.addEventListener('mouseover', () => {
if(!gridSquare.style.backgroundColor){
const randomColor = `rgb(${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)})`
color ? setColor(color) : setColor(randomColor)
}
if(gridSquare.style.backgroundColor){
gridSquare.style.opacity = +gridSquare.style.opacity + 0.1
}
})
container.appendChild(gridSquare)
}
buttonClear.addEventListener('click', () => {
const containerArr = Array.from(container.childNodes);
containerArr.forEach(div => {
div.style.cssText = divStyle
})
})
}
etchSketch()
buttonSize.addEventListener('click', () => {
etchSketch(true)
})
}
)