-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlights.html
212 lines (198 loc) · 7.26 KB
/
lights.html
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lights</title>
<style>
body {
margin: 0;
height: 100vh;
color: white;
background-color: black;
}
.container {
width: 96%;
height: 96%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: grid;
}
.light {
width: 5vmin;
height: 5vmin;
border-radius: 2.5vmin;
background-color: #4e4e4e;
}
@keyframes blink {
10% { background-color: yellow; }
25% { background-color: yellow; }
100% { background-color: #4e4e4e; }
}
.controls {
position: absolute;
top: 50%;
left: 50%;
width: calc( min( 30rem, 72vw ) );
transform: translate(-50%, -50%);
}
.control {
text-align: center;
width: 100%;
}
.label {
margin-bottom: 1rem;
}
.slider {
width: 100%;
}
</style>
</head>
<body>
<div class="container"></div>
<div class="controls">
<div class="control">
<p class="label rows-amt-label"></p>
<input type="range" min="2" max="16" value="7" class="slider rows-amt">
</div>
<div class="control">
<p class="label columns-amt-label"></p>
<input type="range" min="2" max="16" value="7" class="slider columns-amt">
</div>
<div class="control">
<p class="label speed-label"></p>
<input type="range" min="1" max="100" value="50" class="slider speed">
</div>
<div class="control">
<p class="label stripes-label"></p>
<input type="range" min="1" max="5" value="2" class="slider stripes">
</div>
</div>
<script>
const container = document.querySelector('.container')
const rowsAmtRange = document.querySelector('.rows-amt')
const columnsAmtRange = document.querySelector('.columns-amt')
const speedRange = document.querySelector('.speed')
const stripesRange = document.querySelector('.stripes')
const rowsAmtLabel = document.querySelector('.rows-amt-label')
const columnsAmtLabel = document.querySelector('.columns-amt-label')
const speedLabel = document.querySelector('.speed-label')
const stripesLabel = document.querySelector('.stripes-label')
const renderLights = () => {
const rows = Number(rowsAmtRange.value)
const columns = Number(columnsAmtRange.value)
const animationSeconds = 4 * ((115 - Number(speedRange.value)) / 115)
const stripes = Number(stripesRange.value)
const perimeterCount = (rows * 2) + ((columns - 2) * 2)
const delayInterval = (animationSeconds * 1000 * stripes) / perimeterCount
const maxDelay = delayInterval * (perimeterCount - 1)
container.style.gridTemplateRows = `repeat(${rows}, auto)`
container.style.gridTemplateColumns = `repeat(${columns}, auto)`
container.innerHTML = ''
for (let i = 0; i < perimeterCount; i++) {
const row = (
i < columns
? 0
: i < (columns + rows - 1)
? i - columns + 1
: i < (((columns - 1) * 2) + rows)
? rows - 1
: (rows - 2) - (i - (((columns - 1) * 2) + rows))
)
const column = (
i < columns
? i
: i < (columns + rows - 1)
? columns - 1
: i < (((columns - 1) * 2) + rows)
? (columns - 2) - (i - (columns + rows - 1))
: 0
)
const isInFirstRow = row === 0
const isInLastRow = row === (rows - 1)
const isInFirstColumn = column === 0
const isInLastColumn = column === (columns - 1)
const isCorner = (
(isInFirstRow && isInFirstColumn) ||
(isInFirstRow && isInLastColumn) ||
(isInLastRow && isInFirstColumn) ||
(isInLastRow && isInLastColumn)
)
const light = document.createElement('div')
light.classList.add('light')
light.style.alignSelf = (
isCorner
? 'center'
: isInFirstRow
? 'start'
: isInLastRow
? 'end'
: 'center'
)
light.style.justifySelf = (
isCorner
? 'center'
: isInFirstColumn
? 'start'
: isInLastColumn
? 'end'
: 'center'
)
light.style.gridArea = `${row + 1}/${column + 1}/${row + 1}/${column + 1}`
light.style.animation = `${animationSeconds}s blink infinite`
light.style.animationDelay = `${(delayInterval * i) - maxDelay}ms`
container.appendChild(light)
}
}
const renderLabel = (rangeElement, labelElement, prefix) => {
labelElement.innerText = `${prefix}: ${rangeElement.value}`
}
const renderRowsAmt = () =>
renderLabel(rowsAmtRange, rowsAmtLabel, 'Rows')
const renderColumnsAmt = () =>
renderLabel(columnsAmtRange, columnsAmtLabel, 'Columns')
const renderSpeed = () =>
renderLabel(speedRange, speedLabel, 'Speed')
const renderStripes = () =>
renderLabel(stripesRange, stripesLabel, 'Stripes')
const onRangeChange = (rangeElement, callback) => {
let callbackCalled, currentValue, previousValue
rangeElement.addEventListener('input', e => {
callbackCalled = true
currentValue = e.target.value
if(currentValue !== previousValue) callback(e)
previousValue = currentValue
})
rangeElement.addEventListener('change', e => {
if (!callbackCalled) callback(e)
})
}
onRangeChange(rowsAmtRange, () => {
renderRowsAmt()
renderLights()
})
onRangeChange(columnsAmtRange, () => {
renderColumnsAmt()
renderLights()
})
onRangeChange(speedRange, () => {
renderSpeed()
renderLights()
})
onRangeChange(stripesRange, () => {
renderStripes()
renderLights()
})
window.addEventListener('load', () => {
renderRowsAmt()
renderColumnsAmt()
renderSpeed()
renderStripes()
renderLights()
})
</script>
</body>
</html>