-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
175 lines (157 loc) · 4.32 KB
/
Copy pathapp.js
File metadata and controls
175 lines (157 loc) · 4.32 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const settings = {
gridsize: 16,
color: '#000',
}
const elements = {
appContainer: document.querySelector('#app'),
etchContainer: document.createElement('div'),
buttonsContainer: document.createElement('div'),
buttons: {},
}
const state = {
mouse: '',
color: 'DEFAULT',
}
document.addEventListener(
'mouseup',
() => {
state.mouse = 'UP'
},
false
)
document.addEventListener(
'mousedown',
() => {
state.mouse = 'DOWN'
},
false
)
const clearTheBoard = () => {
const pixels = document.querySelectorAll('.pixelgrid > div')
pixels.forEach((pixel) => {
pixel.style.backgroundColor = ''
})
}
const resetBoardSize = () => {
clearTheBoard()
settings.gridsize = parseInt(prompt('Enter new boardsize (max. 100)')) || settings.gridsize
elements.etchContainer.childNodes.forEach((child) => {
child.remove()
})
elements.appContainer.appendChild(createPixelGrid(elements.etchContainer, settings.gridsize))
}
const getRandomColor = () => {
const letters = '0123456789ABCDEF'
let color = '#'
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)]
}
return color
}
// The best solution is problably this one: https://stackoverflow.com/questions/5560248/programmatically-lighten-or-darken-a-hex-color-or-rgb-and-blend-colors
// but I try it myself and simpler
const getDarkenRGBColor = (shadingValue, currentBackgroundColorStyle) => {
const colorRegEx = /rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/
// value at index 0 is the complete string, then the capture groups are following
const [_, r, g, b] = currentBackgroundColorStyle.match(colorRegEx) || ['', 255, 255, 255]
return `rgb(${parseInt(r) + shadingValue}, ${parseInt(g) + shadingValue}, ${
parseInt(b) + shadingValue
})`
}
const createCustomElement = (
tagname,
type = '',
text = '',
id = '',
cssClasses = [],
onclickFn
) => {
const element = document.createElement(tagname)
element.type = type
element.id = id
element.classList.add(...cssClasses)
element.textContent = text
element.onclick = onclickFn
return element
}
const changePixelBackgroundColor = (e) => {
if (state.mouse === 'DOWN') {
if (state.color === 'DEFAULT') e.target.style.backgroundColor = settings.color
if (state.color === 'RANDOM') e.target.style.backgroundColor = getRandomColor()
if (state.color === 'FADE') e.target.style.backgroundColor = getDarkenRGBColor(-25.5, e.target.style.backgroundColor)
}
}
const createPixelGrid = (etchContainerElement, gridSize = settings.gridsize) => {
gridSize = gridSize > 100 ? 100 : gridSize
const cssGridSize = `repeat(${gridSize}, 1fr)`
etchContainerElement.style.gridTemplateColumns = cssGridSize
etchContainerElement.style.gridTemplateRows = cssGridSize
for (let i = 0; i < gridSize * gridSize; i++) {
const pixel = document.createElement('div')
pixel.addEventListener(
'mouseover',
(e) => {
changePixelBackgroundColor(e)
},
false
)
etchContainerElement.appendChild(pixel)
}
return etchContainerElement
}
const init = (gridSize = settings.gridsize) => {
elements.buttons.clearButton = createCustomElement(
'button',
'button',
'Clear',
undefined,
['nes-btn'],
clearTheBoard
)
elements.buttons.resetButton = createCustomElement(
'button',
'button',
'New Gridsize',
undefined,
['nes-btn'],
resetBoardSize
)
elements.buttons.rainbowColorButton = createCustomElement(
'button',
'button',
'Color Rainbow',
undefined,
['nes-btn'],
() => {
state.color = 'RANDOM'
}
)
elements.buttons.resetColor = createCustomElement(
'button',
'button',
'Color Black',
undefined,
['nes-btn'],
() => {
state.color = 'DEFAULT'
}
)
elements.buttons.fadeColorDark = createCustomElement(
'button',
'button',
'Color Fade Darker',
undefined,
['nes-btn'],
() => {
state.color = 'FADE'
}
)
elements.etchContainer.classList.add('pixelgrid')
elements.buttonsContainer.classList.add('buttons')
Object.entries(elements.buttons).forEach((button) => {
elements.buttonsContainer.appendChild(button[1]) // HTMLElement is on index 1 in array
})
elements.appContainer.appendChild(createPixelGrid(elements.etchContainer, gridSize))
elements.appContainer.before(elements.buttonsContainer)
}
init()