-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
96 lines (82 loc) · 2.47 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const main = document.querySelector('.container');
let drag = false;
let grid = true;
main.addEventListener('dragstart',(e)=>{e.preventDefault()});
// color
let color = document.getElementById('color').value;
const paint = (e) => {
if(drag && e.buttons!=0)
e.target.style.background = color;
}
const ntg = ()=>{};
const toggleBoxEvents = add => {
const boxes = document.querySelectorAll(".innerCont");
if (add) {
boxes.forEach(box => {
box.addEventListener('mousedown',(e)=>{
drag = true;
paint(e);
} );
box.addEventListener('mouseenter',paint);
box.addEventListener('mouseup',()=>{drag=false});
});
}
else {
boxes.forEach(box => {
box.removeEventListener('mouseenter',ntg);
box.removeEventListener('mouseup',ntg);
box.removeEventListener('mousedown',ntg);
drag = false;
});
}
};
const Draw = (size) => {
for (let i = 0; i < size; i++) {
// rowContainer
const rowCont = document.createElement('div');
rowCont.classList.add("rowCont")
for (let j = 0; j < size; j++) {
// innerContainer
let cont = document.createElement('div');
cont.style.cssText =
`width:calc(50vh/${size});
height:calc(50vh/${size})`;
cont.classList.add("innerCont");
grid = true;
rowCont.appendChild(cont);
}
main.appendChild(rowCont);
}
document.querySelector('.currSize').innerHTML = `Size : ${size} x ${size}`;
toggleBoxEvents(true);
}
// default size 5x5
const resize = document.getElementById('slider');
size = resize.value;
Draw(size);
// event listeners
const colorPick = document.getElementById('color');
colorPick.addEventListener('input', (e) => {
color = e.target.value;
})
resize.addEventListener('input', (e) => {
size = e.target.value;
toggleBoxEvents(false);
main.innerHTML = "";
Draw(size);
})
const Grid = ()=>{
boxes = document.querySelectorAll('.innerCont');
if(grid){
boxes.forEach(box=>box.style.border="unset")
grid = false;
}
else{
boxes.forEach(box=>box.style.border="0.5px solid gray")
grid = true;
}
}
const eraseAll = ()=>{
boxes = document.querySelectorAll('.innerCont');
boxes.forEach(box => box.style.background = "unset")
}