-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
123 lines (94 loc) · 3.27 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const DEFAULT_COLOR = "#333"
const DEFAULT_MODE = "color"
const DEFAULT_SIZE = 16
let currentColor = DEFAULT_COLOR
let currentMode = DEFAULT_MODE
let currentSize = DEFAULT_SIZE
const colorPicker = document.querySelector("#colorPicker")
const colorBtn = document.querySelector("#colorBtn")
const rainbowBtn = document.querySelector("#rainbowBtn")
const eraserBtn = document.querySelector("#eraserBtn")
const clearBtn = document.querySelector("#clearBtn")
const sizeValue = document.querySelector("#sizeValue")
const container = document.querySelector(".grid-container")
const sizeDisplay = document.querySelector(".sizeDisplay")
function clearGrid() {
container.innerHTML = ""
}
function setCurrentColor(color) {
currentColor = color
}
function setCurrentSize(size) {
currentSize = size
}
function setCurrentMode(mode) {
activateButton(mode)
currentMode = mode
}
function changeSize(value) {
setCurrentSize(value)
updateSizeValue(value)
reloadGrid()
sizeDisplay.innerHTML = `${value}x${value}`
sizeValue.value = ""
}
function reloadGrid() {
clearGrid()
createGrid(currentSize)
}
function updateSizeValue(value) {
sizeValue.innerHTML = `${value} x ${value}`
}
let mouseDown = false
document.body.onmousedown = () => (mouseDown = true)
document.body.onmouseup = () => (mouseDown = false)
function createGrid(size) {
container.style.gridTemplateColumns = `repeat(${size}, 1fr)`
container.style.gridTemplateRows = `repeat(${size}, 1fr)`
for (let i = 0; i < size * size; i++) {
const gridElement = document.createElement('div')
gridElement.classList.add('grid-element')
gridElement.addEventListener('mouseover', changeColor)
gridElement.addEventListener('mousedown', changeColor)
container.appendChild(gridElement)
}
}
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 activateButton(newMode) {
if (currentMode === 'rainbow') {
rainbowBtn.classList.remove('active')
} else if (currentMode === 'color') {
colorBtn.classList.remove('active')
} else if (currentMode === 'eraser') {
eraserBtn.classList.remove('active')
}
if (newMode === 'rainbow') {
rainbowBtn.classList.add('active')
} else if (newMode === 'color') {
colorBtn.classList.add('active')
} else if (newMode === 'eraser') {
eraserBtn.classList.add('active')
}
}
colorPicker.oninput = (e) => setCurrentColor(e.target.value)
colorBtn.onclick = () => setCurrentMode('color')
rainbowBtn.onclick = () => setCurrentMode('rainbow')
eraserBtn.onclick = () => setCurrentMode('eraser')
clearBtn.onclick = () => reloadGrid()
sizeBtn.onclick = () => changeSize(sizeValue.value)
window.onload = () => {
createGrid(DEFAULT_SIZE)
activateButton(DEFAULT_MODE)
}