-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
90 lines (73 loc) · 2.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
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
const board = document.querySelector('.board');
const colorPanel = document.querySelector('.color-panel');
const colorBtn = document.querySelector('.color-btn');
const rainbowBtn = document.querySelector('.rainbow-btn');
const eraserBtn = document.querySelector('.eraser-btn');
const clearBtn = document.querySelector('.clear-btn');
const sizeValue = document.querySelector('.size-value');
const sizeSlider = document.querySelector('.size-slider');
let currentColor = '#333333';
let currentMode = 'color';
let currentSize = 16;
window.onload = () => {
setupBoard(currentSize);
colorBtn.classList.add('colored-button');
}
function setupBoard(size) {
board.style.gridTemplateColumns = `repeat(${size}, 1fr)`;
board.style.gridTemplateRows = `repeat(${size}, 1fr)`;
for (let i = 0; i < (size * size); i++) {
const square = document.createElement('div');
square.classList.add('square');
board.appendChild(square);
square.addEventListener('mouseover', changeColor);
square.addEventListener('mousedown', changeColor);
}
}
let mouseDown = false;
document.body.onmousedown = () => (mouseDown = true);
document.body.onmouseup = () => (mouseDown = false);
function changeColor(e) {
if (e.type === 'mouseover' && !mouseDown) return
if (currentMode === 'rainbow') {
const randomR = Math.floor(Math.random() * 256)
const randomG = Math.floor(Math.random() * 256)
const randomB = Math.floor(Math.random() * 256)
e.target.style.backgroundColor = `rgb(${randomR}, ${randomG}, ${randomB})`
} else if (currentMode === 'color') {
e.target.style.backgroundColor = currentColor
} else if (currentMode === 'eraser') {
e.target.style.backgroundColor = '#fefefe'
}
}
function reloadBoard() {
board.innerHTML = '';
setupBoard(currentSize);
}
function colorButton(coloredButton, firstToRemove, secondToRemove) {
coloredButton.classList.add('colored-button');
firstToRemove.classList.remove('colored-button');
secondToRemove.classList.remove('colored-button');
}
colorPanel.addEventListener('input', () => {currentColor = `${colorPanel.value}`})
colorBtn.addEventListener('click', () => {
colorButton(colorBtn, rainbowBtn, eraserBtn);
currentMode = 'color'
});
rainbowBtn.addEventListener('click', () => {
colorButton(rainbowBtn, colorBtn, eraserBtn);
currentMode = 'rainbow'
});
eraserBtn.addEventListener('click', () => {
colorButton(eraserBtn, colorBtn, rainbowBtn);
currentMode = 'eraser'
});
sizeSlider.addEventListener('input', () => {
sizeValue.textContent = `${sizeSlider.value} x ${sizeSlider.value}`;
currentSize = sizeSlider.value;
});
sizeSlider.addEventListener('mouseup', () => {
setupBoard(sizeSlider.value);
reloadBoard()
})
clearBtn.addEventListener('click', reloadBoard);