-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
87 lines (77 loc) · 2.47 KB
/
app.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
const sketchArea = document.querySelector('#sketchBox');
const clearButton = document.querySelector('#clear');
const colorSelect = document.querySelector('#colorSelect');
const slider = document.querySelector('#slider');
const sketchSize = document.querySelector('#sketchSize');
const eraser = document.querySelector('#eraser');
const colorMode = document.querySelector('#color');
const rainbowMode = document.querySelector('#rainbow');
const modes = document.querySelectorAll('.mode');
let mode = 'color';
let drawColor = '#333';
let mouseDown = false;
document.body.onmousedown = () => (mouseDown = true);
document.body.onmouseup = () => (mouseDown = false);
function colorCell(e){
if(e.type === 'mouseover' && !mouseDown) return;
if(mode === 'color'){
this.style.backgroundColor = drawColor;
}
else if(mode === 'eraser'){
this.style.backgroundColor = '#fefefe';
}
else{
this.style.backgroundColor = randomColor();
}
}
function clearGraph(){
const cells = document.querySelectorAll('.cell');
cells.forEach(cell => cell.style.backgroundColor = '#fefefe');
}
function drawGraph(sideLength){
sketchBox.innerHTML = '';
for (let i = 0; i < sideLength; i++) {
const column = document.createElement('div');
column.classList.add('col');
for(let j = 0; j < sideLength; j++){
const cell = document.createElement('div')
cell.classList.add('cell');
cell.addEventListener('mouseover', colorCell);
cell.addEventListener('mousedown', colorCell);
column.append(cell);
}
sketchArea.append(column);
}
}
function randomColor(){
return '#' + Math.floor(Math.random()*16777215).toString(16);
}
function changeActiveButton(){
colorMode.classList.toggle('active');
rainbowMode.classList.toggle('active');
eraser.classList.toggle('active');
}
function selectMode(e){
let oldButton = document.querySelector(`#${mode}`)
let newButton = document.querySelector(`#${e.target.id}`);
newButton.classList.add('active');
oldButton.classList.remove('active');
mode = e.target.id;
}
slider.addEventListener('input', (e) => {
let sideLength = parseInt(e.target.value);
sketchSize.innerText = `${sideLength} x ${sideLength}`;
drawGraph(sideLength);
});
clearButton.addEventListener('click', clearGraph);
colorSelect.addEventListener('input', (e) => {
drawColor = e.target.value;
});
for (let modeButton of modes){
modeButton.addEventListener('click', (e) => {
if(e.target.id !== mode){
selectMode(e);
}
});
}
drawGraph(16);