-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
89 lines (60 loc) · 2.02 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
const container = document.querySelector(".grids");
for (let i = 0; i < 30; i++){
const row = document.createElement('div');
for (let j = 0; j < 30; j++){
const col = document.createElement('div');
row.appendChild(col);
col.style.cssText = 'height: 20px; width: 20px; background: white;';
col.setAttribute('class', 'box');
}
container.appendChild(row);
row.style.cssText = 'display: flex;';
}
const boxes = document.querySelectorAll(".box");
const clear = document.querySelector("#clear");
const erase = document.querySelector('#eraser');
const draw = document.querySelector('#draw');
const color = document.querySelector('#colour');
function boxClicked(){
if (isErase && isMouseDown) {
this.style.cssText = 'background: white; height: 20px; width: 20px;'
} else if (isMouseDown){
if (randomcolor){
const allcolors = ['violet', 'indigo', 'blue', 'green', 'yellow', 'orange', 'red']
const choose = Math.floor((Math.random()*7));
const bgcolor = allcolors[choose];
this.style.cssText = `background: ${bgcolor}; height: 20px; width: 20px;`;
} else {
this.style.cssText = "background: black; height: 20px; width: 20px;";
}
}
}
function gridCleared(){
boxes.forEach(box => {
box.style.cssText = 'background: white; height: 20px; width: 20px;'
});
isErase = false;
}
boxes.forEach(box => {
box.addEventListener('mousedown', () => {
isMouseDown = true;
});
box.addEventListener('mouseup', () => {
isMouseDown = false;
});
box.addEventListener('mousemove', boxClicked);
});
clear.addEventListener('click', gridCleared);
let isErase = false;
let randomcolor = false;
erase.addEventListener('click', () => {
isErase = true;
});
draw.addEventListener('click', () => {
isErase = false;
randomcolor = false;
});
color.addEventListener('click', () => {
isErase = false;
randomcolor = true;
});