-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
64 lines (63 loc) · 1.78 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
const grid = document.getElementById('grid');
isClicked = 0;
color = "red";
rainbow = ["rgba(238,130,238,1)","rgba(75,0,130,1)","rgba(0,0,255,1)","rgba(0,128,0,1)","rgba(255,255,0,1)","rgba(255,165,0,1)","rgba(255,0,0,1)"];
grid.addEventListener('mousedown', e =>{
isClicked = 1;
} );
grid.addEventListener('mouseup', e=>{
isClicked = 0;
});
grid.addEventListener('mouseleave', e=>{
isClicked = 0;
});
function initgrid(x){
for ( let step = 0; step < x*x; step++){
const gridelement = document.createElement('div');
gridelement.ondragstart = function() {return false;}
gridelement.addEventListener('mouseenter', active =>{
draw(gridelement);
});
gridelement.addEventListener('mousedown', todraw =>{
isClicked = 1;
draw(gridelement);
});
grid.appendChild(gridelement);
}
}
// function cleargrid(){
// document.getElementById("grid").innerHTML = '';
// setsize();
// }
function draw(elem){
if(isClicked==1){
if(color=='rainbow')
elem.style.backgroundColor = rainbow[Math.floor(Math.random()*7)];
else
elem.style.backgroundColor = color;
if(color=='sketch'){
const style = getComputedStyle(elem);
if(style.opacity > 0 ) elem.style.opacity = style.opacity - 0.1;
}
}
}
function set_color(button, newcolor){
color = newcolor;
document.getElementsByClassName("but-active")[0].className = "";
button.className = "but-active";
}
function setsize(){
size = document.getElementById('size-input');
if( size.value>1){
document.getElementById("grid").innerHTML = '';
document.body.style.setProperty('--size',size.value)
initgrid(size.value);
}
}
function mimicsize(){
sizevalue = document.getElementById('mimic');
size = document.getElementById('size-input');
sizevalue.innerHTML = size.value;
}
initgrid(16);
document.getElementById('size-input').value = '16';