forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbits-rain.html
More file actions
233 lines (205 loc) · 6.27 KB
/
Copy pathbits-rain.html
File metadata and controls
233 lines (205 loc) · 6.27 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>bits rain</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
}
#scene {
width: 100%;
height: 500px;
display: flex;
border: 1px solid black;
overflow: hidden;
color: #FF0000;
background-color: #000000;
}
#toolbox {
display: flex;
flex-direction: column;
margin-top: 3rem;
padding: 1rem;
border-radius: 4px;
background-color: #F1F1F1;
}
.tool {
display: flex;
align-items: center;
margin-bottom: 0.5rem;
}
.tool:last-of-type {
margin-bottom: 0;
}
.tool > span {
margin: 0 1rem 0 0;
padding: 0;
flex: 1;
}
.tool > input {
width: 60px;
border-radius: 3px;
}
.tool > input[type="color"]{
border: none;
border-color: transparent;
-webkit-appearance: none;
}
input::-webkit-color-swatch {
border: none;
}
.column {
display: flex;
flex-direction: column;
align-items: center;
flex: 1;
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="toolbox">
<div class="tool">
<span>bit color</span>
<input id="text-color-input" type="color" value="#FF0000" />
</div>
<div class="tool">
<span>background color</span>
<input id="background-color-input" type="color" value="#000000" />
</div>
<div class="tool">
<span>update interval (ms)</span>
<input id="speed-input" type="number" min="0" max="1000" value="50" size="1" />
</div>
<div class="tool">
<span>column count</span>
<input id="column-count-input" type="number" min="0" max="100" value="50" size="4" />
</div>
<div class="tool">
<span>p0</span>
<input id="p0-input" type="range" min="0" max="1" step="0.05" value="0.3" />
</div>
<div class="tool">
<span>p1</span>
<input id="p1-input" type="range" min="0" max="1" step="0.05" value="0.3" />
</div>
</div>
<script>
const isElementOutside = (parent, child) => {
const parentRect = parent.getBoundingClientRect();
const childRect = child.getBoundingClientRect();
return parentRect.top >= childRect.bottom || parentRect.bottom <= childRect.top;
}
const getBit = (p0, p1) => {
const seed = Math.random()
if (seed <= p0) return '0'
if (seed <= p1 + p0) return '1'
return ' '
}
const insertColumn = container => {
const column = document.createElement('div')
column.classList.add('column')
container.appendChild(column)
return column
}
const removeColumn = (container, column) => {
container.removeChild(column)
}
const disposeColumns = (container, count) =>
Array.from(Array(count)).map(() => insertColumn(container))
const patchColumnCount = (columns, container, nextCount) => {
const lastCount = columns.length
if (lastCount === nextCount) return columns
const countDelta = nextCount - lastCount
if (countDelta < 0) {
return columns.filter((column, index) => {
if (index < lastCount + countDelta) return true
removeColumn(container, columns[index])
return false
})
}
const newColumns = disposeColumns(container, countDelta)
return [
...columns,
...newColumns
]
}
const runScene = (scene) => {
const { container, columns, intervalDuration } = scene
scene.interval = setInterval(() => {
columns.forEach((column) => {
const firstBit = column.firstChild
const lastBit = column.lastChild
const bit = document.createElement('div')
bit.innerText = getBit(scene.p0, scene.p1)
if (!firstBit) {
return column.appendChild(bit)
}
column.insertBefore(bit, firstBit)
if (isElementOutside(container, lastBit)) {
column.removeChild(lastBit)
}
})
}, intervalDuration)
}
</script>
<script>
const scene = {
interval: null,
container: null,
intervalDuration: 50,
columns: [],
columnCount: 50,
p0: 0.3,
p1: 0.3
}
document.querySelector('#text-color-input').addEventListener('change', event => {
const { value } = event.target
scene.container.style.color = value
})
document.querySelector('#background-color-input').addEventListener('change', event => {
const { value } = event.target
scene.container.style.backgroundColor = value
})
document.querySelector('#speed-input').addEventListener('change', event => {
const { value } = event.target
clearInterval(scene.interval)
scene.intervalDuration = value
runScene(scene)
})
document.querySelector('#column-count-input').addEventListener('change', event => {
const value = Math.abs(event.target.value) > 100 ? 100 : event.target.value
event.target.value = Math.abs(value)
clearInterval(scene.interval)
setTimeout(() => {
scene.columns = patchColumnCount(scene.columns, scene.container, parseInt(value, 10))
runScene(scene)
}, 0)
})
const p0Input = document.querySelector('#p0-input')
const p1Input = document.querySelector('#p1-input')
const makeUpdateP = (targetName, siblingName, targetInput, siblingInput) => event => {
const value = Math.abs(event.target.value) > 1 ? 1 : event.target.value
const pTarget = Math.abs(value)
targetInput.value = pTarget
scene[targetName] = pTarget
const pTargetDelta = 1 - pTarget
if (pTarget + scene[siblingName] > 1) {
scene[siblingName] = pTargetDelta
siblingInput.value = pTargetDelta
}
clearInterval(scene.interval)
runScene(scene)
}
p0Input.addEventListener('change', makeUpdateP('p0', 'p1', p0Input, p1Input))
p1Input.addEventListener('change', makeUpdateP('p1', 'p0', p1Input, p0Input))
scene.container = document.querySelector('#scene')
scene.columns = disposeColumns(scene.container, scene.columnCount)
runScene(scene)
</script>
</body>
</html>