-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathetch.js
More file actions
57 lines (47 loc) · 1.48 KB
/
etch.js
File metadata and controls
57 lines (47 loc) · 1.48 KB
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
let iDraw=false;
let iRGB=false;
let Rgb = "rgb(255, 0, 0)";
//initial Grid
const container = document.querySelector("#container");
let k = 16;
function makeNew(k){
container.innerHTML = "";
container.style.gridTemplateColumns = `repeat(${k}, 1fr)`;
for (i=0;i<k;i++){
for (j=0;j<k;j++){
const div = document.createElement("div");
div.classList.add("pix");
container.appendChild(div);
}}}
makeNew(k);
//New Grid
const newG = document.querySelector("#newGrid");
newG.addEventListener('click', getNewGrid);
function getNewGrid(){
k=prompt("Inter the size of the Grid: ");
makeNew(k);
return k;
}
//Clear Grid
const clr = document.querySelector("#clear");
clr.addEventListener('click',()=>{ makeNew(k)});
//draw
const canv = document.querySelector("#container");
canv.addEventListener('mousedown', ()=> {iDraw=true});
canv.addEventListener('mouseover', (e)=>{
if(iDraw && !iRGB){
e.target.style.backgroundColor=Rgb}
else if(iDraw && iRGB ){
Rgb = getRGB()
e.target.style.backgroundColor=Rgb}
});
document.addEventListener('mouseup', ()=>{iDraw=false});
function getRGB() {
const r = Math.floor(Math.random() * 256); // 0–255
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
Rgb =`rgb(${r}, ${g}, ${b})`;
return Rgb;
}
const Rain = document.querySelector("#Rainbow");
Rain.addEventListener('click', ()=>{iRGB=!iRGB});