forked from gSchool/pixel-art-maker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
65 lines (55 loc) · 1.93 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
document.addEventListener("DOMContentLoaded", function () {
let grid = document.getElementById("grid")
let container = document.getElementById("container")
let pixels = 24
let colorNames = ["red", "orange", "yellow", "green", "blue", "purple", "black", "white", "gray", "SaddleBrown"]
let currentColor = ""
let cell, row, col, colorCell, indicator
// pixels * width of each pixel (20px) + (pixels + 1) * border (1px) = total grid size
grid.style.width = (pixels * 21 + 1) + "px"
grid.style.height = (pixels * 21 + 1) + "px"
container.style.width = (pixels * 21 + 50) + "px"
function changeColor(event) {
if (event.target.className === "cell") {
event.target.style.backgroundColor = currentColor
}
}
function selectColor(event) {
currentColor = event.target.style.backgroundColor
indicator.style.backgroundColor = currentColor
}
function makeGrid() {
for (let rows = 0; rows < pixels; rows++) {
row = document.createElement("div")
row.className = "row"
for (let cols = 0; cols < pixels; cols++) {
cell = document.createElement("div")
cell.className = "cell"
if (rows === 0) {
cell.style.borderTop = "1px solid black"
}
if (cols === 0) {
cell.style.borderLeft = "1px solid black"
}
row.appendChild(cell)
}
grid.appendChild(row)
}
grid.addEventListener("click", changeColor)
}
function makePalette(colors) {
for (let i = 0; i < colors.length; i++) {
palette = document.getElementById("palette")
colorCell = document.createElement("div")
colorCell.className = "color"
colorCell.style.backgroundColor = colors[i]
colorCell.addEventListener("click", selectColor)
palette.appendChild(colorCell)
}
}
makeGrid()
makePalette(colorNames)
indicator = document.createElement("div")
indicator.className = "indicator"
palette.appendChild(indicator)
})