-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
330 lines (295 loc) · 9 KB
/
index.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!DOCTYPE html>
<head>
<title>PNGTuber</title>
</head>
<div id="imgdiv">
<img id="img" class="img">
<img id="simg" class="img">
</div>
<div id="controls">
<img id="logo" src="logo.png" />
<p>Volume threshold</p>
<input type="range" id="volume-range" min="1" max="100" oninput="onVolumeRange(this.value)" />
<input type="number" id="volume-number" min="1" max="100" oninput="onVolumeNumber(this.value)" />
<p>Idle brightness</p>
<input type="range" id="brightness-range" min="1" max="100" oninput="onBrightnessRange(this.value)" />
<input type="number" id="brightness-number" min="1" max="100" oninput="onBrightnessNumber(this.value)" />
<p>Animation transition time</p>
<input type="range" id="time-range" min="0" max="1" step="0.01" oninput="onTimeRange(this.value)" />
<input type="number" id="time-number" min="0" max="1" step="0.01" oninput="onTimeNumber(this.value)" />
<p>Silence transition delay</p>
<input type="range" id="delay-range" min="0" max="1" step="0.1" oninput="onDelayRange(this.value)" />
<input type="number" id="delay-number" min="0" max="1" step="0.1" oninput="onDelayNumber(this.value)" />
<p>Jump height</p>
<input type="range" id="height-range" min="0" max="100" oninput="onHeightRange(this.value)" />
<input type="number" id="height-number" min="0" max="100" oninput="onHeightNumber(this.value)" />
<p>Idle image</p>
<input type="file" accept="image/*" oninput="setIdle(this.files[0])" />
<p>Talking image</p>
<input type="file" accept="image/*" oninput="setTalking(this.files[0])" />
</div>
<style>
* {
font-size: 4vmin;
font-family: Arial, Helvetica, sans-serif;
margin: 0px;
}
.img {
position: absolute;
bottom: 0;
margin-left: auto;
margin-right: auto;
max-width: 100vw;
max-height: 100vh;
}
#img {
opacity: 0;
transition-property: top;
}
#imgdiv {
display: flex;
justify-content: center;
align-items: center;
}
#controls:hover {
opacity: 1;
}
#controls {
width: 99vw;
height: 99vh;
position: absolute;
top: 0px;
background-color: rgba(127, 127, 127, 0.5);
opacity: 0;
}
#logo {
max-width: 100vw;
max-height: 25vh;
}
input[type=range] {
width: 75vw;
-webkit-appearance: none;
appearance: none;
}
input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 1vw;
height: 3vh;
background-color: black;
}
input[type=range]::-webkit-slider-runnable-track {
-webkit-appearance: none;
appearance: none;
width: 75vw;
height: 3vh;
background-color: white;
}
#volume-range {
--volume: 0%;
position: relative;
width: 75vw;
height: 3vh;
background-color: white;
}
#volume-range::before {
content: "";
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: var(--volume);
background-color: rgba(0, 255, 0, 0.5);
transition: width 100ms linear;
}
</style>
<script src="https://localforage.github.io/localForage/localforage.min.js"></script>
<script>
const VOLUME = "volume";
const VOLUME_RANGE = document.getElementById("volume-range");
const VOLUME_NUMBER = document.getElementById("volume-number");
const BRIGHTNESS = "brightness";
const BRIGHTNESS_RANGE = document.getElementById("brightness-range");
const BRIGHTNESS_NUMBER = document.getElementById("brightness-number");
const TIME="time";
const TIME_RANGE=document.getElementById("time-range");
const TIME_NUMBER=document.getElementById("time-number");
const DELAY="delay"
const DELAY_RANGE=document.getElementById("delay-range");
const DELAY_NUMBER=document.getElementById("delay-number");
const HEIGHT="height";
const HEIGHT_RANGE=document.getElementById("height-range");
const HEIGHT_NUMBER=document.getElementById("height-number");
const IDLE_IMAGE = "idle-image";
const TALKING_IMAGE = "talking-image";
const IMAGE = document.getElementById("img");
const SIMAGE = document.getElementById("simg");
const DEFAULT_VOLUME = 50;
const DEFAULT_BRIGHTNESS = 90;
const DEFAULT_TIME=0.2;
const DEFAULT_HEIGHT=5;
const DEFAULT_IDLE = "";
const DEFAULT_TALKING = "";
const DEFAULT_DELAY=0.5;
const LEGACY_BRIGHTNESS = "darkness";
const LEGACY_IDLE = "closed";
const LEGACY_TALKING = "open";
const storage = localforage;
let volume;
let brightness;
let time;
let idle;
let talking;
let height;
let delay;
function setHeight(input){
height=input;
IMAGE.style.maxHeight=(100-height)+"vh";
IMAGE.style.top=height+"vh";
SIMAGE.style.maxHeight=(100-height)+"vh";
SIMAGE.style.top=height+"vh";
}
function setTime (input) {
document.getElementById("img").style.transitionDuration=input+'s';
time=input;
}
function setIdle(input) {
const reader = new FileReader();
reader.addEventListener("load", function () {
SIMAGE.src = reader.result;
storage.setItem(IDLE_IMAGE, reader.result);
}, false);
reader.readAsDataURL(input);
}
function setTalking(input) {
const reader = new FileReader();
reader.addEventListener("load", function () {
IMAGE.src = reader.result;
storage.setItem(TALKING_IMAGE, reader.result);
}, false);
reader.readAsDataURL(input);
}
function setBrightness(input){
brightness = input;
SIMAGE.style.setProperty('filter', 'brightness(' + brightness + '%)');
}
function onVolumeRange(input) {
volume = input;
VOLUME_NUMBER.value = input;
storage.setItem(VOLUME, input);
}
function onVolumeNumber(input) {
volume = input;
VOLUME_RANGE.value = input;
storage.setItem(VOLUME, input);
}
function onBrightnessRange(input) {
setBrightness(input)
BRIGHTNESS_NUMBER.value = input;
storage.setItem(BRIGHTNESS, input);
}
function onBrightnessNumber(input) {
setBrightness(input)
BRIGHTNESS_RANGE.value = input;
storage.setItem(BRIGHTNESS, input);
}
function onTimeRange(input) {
setTime(input);
TIME_NUMBER.value=input;
storage.setItem(TIME, input);
}
function onTimeNumber(input){
setTime(input);
TIME_RANGE.value=input;
storage.setItem(TIME, input);
}
function onHeightRange(input){
setHeight(input);
HEIGHT_NUMBER.value=input;
storage.setItem(HEIGHT,input);
}
function onHeightNumber(input){
setHeight(input);
HEIGHT_RANGE.value=input;
storage.setItem(HEIGHT,input);
}
function onDelayRange(input){
delay=input;
DELAY_NUMBER.value=input;
storage.setItem(DELAY,input);
}
function onDelayNumber(input){
delay=input;
DELAY_RANGE.value=input;
storage.setItem(DELAY,input);
}
window.onload = async function () {
try {
volume = await storage.getItem(VOLUME) || DEFAULT_VOLUME;
brightness = await storage.getItem(BRIGHTNESS) || DEFAULT_BRIGHTNESS;
idle = await storage.getItem(IDLE_IMAGE) || DEFAULT_IDLE;
talking = await storage.getItem(TALKING_IMAGE) || DEFAULT_TALKING;
delay = await storage.getItem(DELAY) || DEFAULT_DELAY;
setHeight(await storage.getItem(HEIGHT) || DEFAULT_HEIGHT);
setTime(await storage.getItem(TIME) || DEFAULT_TIME);
VOLUME_RANGE.value = volume;
VOLUME_NUMBER.value = volume;
BRIGHTNESS_RANGE.value = brightness;
BRIGHTNESS_NUMBER.value = brightness;
TIME_RANGE.value=time;
TIME_NUMBER.value=time;
HEIGHT_NUMBER.value=height;
HEIGHT_RANGE.value=height;
DELAY_RANGE.value = delay;
DELAY_NUMBER.value = delay;
IMAGE.src = talking;
SIMAGE.src=idle;
SIMAGE.style.setProperty('filter', 'brightness(' + brightness + '%)');
const volumeVisualizer = document.getElementById("volume-range");
let backoff=null
const volumeCallback = function(averageVolume) {
averageVolume=Math.min(parseInt(averageVolume.data),100)
volumeVisualizer.style.setProperty(
"--volume",
averageVolume + "%"
);
if (averageVolume <= volume) {
if(backoff==null && IMAGE.style.opacity==1){
SIMAGE.style.opacity=1
IMAGE.style.opacity=0
}
} else {
if(backoff!=null){
clearTimeout(backoff)
}
backoff=setTimeout(()=>backoff=null,Math.max(time*2000,delay*1000))
if (IMAGE.style.opacity==0) {
IMAGE.style.opacity=1
SIMAGE.style.opacity=0
IMAGE.style.transitionTimingFunction='ease-out'
IMAGE.style.top="0vh";
setTimeout(()=>{
IMAGE.style.top=height+"vh";
IMAGE.style.transitionTimingFunction='ease-in'
},time*1000);
}
}
};
const startAudio=function (event=null){
if(event){
console.log(event)
}
let ev=new EventSource('http://localhost:5000/audio')
ev.onmessage=volumeCallback;
ev.onerror=startAudio;
window.onbeforeunload=function(event=null){
ev.close()
}
}
startAudio();
} catch (e) {
window.alert(e.message);
throw e;
}
}
</script>