-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
56 lines (47 loc) · 1.65 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
const container = document.querySelector('.grid-container');
document.addEventListener('mousedown', () => mouseClicked = true);
document.addEventListener('mouseup', () => mouseClicked = false);
const slider = document.querySelector('.slider');
const sliderNumber = document.querySelector('.slider-number');
slider.addEventListener('mouseup', initGrid);
sliderNumber.textContent = slider.value;
slider.oninput = () => sliderNumber.textContent = slider.value;
const colors = document.querySelectorAll('input.color');
for(const color of colors){
color.addEventListener('click', () => {
for (const color of colors) {
if(color.checked) {
selectedColor = color.value;
break;
}
}
})
}
let mouseClicked = false;
let selectedColor = 'blue';
function initGrid(){
container.innerHTML = '';
let size = slider.value;
//TODO set size limit to 100;
for(let row = 0; row < size; row++){
const rowDiv = document.createElement('div');
rowDiv.classList.add("row");
container.appendChild(rowDiv);
for(let col = 0; col < size; col++){
const div = document.createElement('div');
div.classList.add('square');
rowDiv.appendChild(div);
div.addEventListener('mouseover', () => {
if(mouseClicked){
div.id = selectedColor;
div.classList.add('transition');
}
});
div.addEventListener('click', () => {
div.id = selectedColor;
div.classList.add('transition');
});
}
}
}
initGrid();