-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.js
46 lines (37 loc) · 1.03 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
const container = document.querySelector('.container')
const sizeEl = document.querySelector('.size')
let size = sizeEl.value
const color = document.querySelector('.color')
const resetBtn = document.querySelector('.btn')
let draw = false
function populate(size) {
container.style.setProperty('--size', size)
for (let i = 0; i < size * size; i++) {
const div = document.createElement('div')
div.classList.add('pixel')
div.addEventListener('mouseover', function(){
if(!draw) return
div.style.backgroundColor = color.value
})
div.addEventListener('mousedown', function(){
div.style.backgroundColor = color.value
})
container.appendChild(div)
}
}
window.addEventListener("mousedown", function(){
draw = true
})
window.addEventListener("mouseup", function(){
draw = false
})
function reset(){
container.innerHTML = ''
populate(size)
}
resetBtn.addEventListener('click', reset)
sizeEl.addEventListener('keyup', function(){
size = sizeEl.value
reset()
})
populate(size)