mirror of
https://codeberg.org/vlw/vlw.se.git
synced 2025-09-13 21:13:40 +02:00
This PR adds basic support for the upcoming release Vegvisir 3.1 Reviewed-on: https://codeberg.org/vlw/vlw.se/pulls/11 Co-authored-by: Victor Westerlund <victor.vesterlund@gmail.com> Co-committed-by: Victor Westerlund <victor.vesterlund@gmail.com>
54 lines
No EOL
1.2 KiB
JavaScript
54 lines
No EOL
1.2 KiB
JavaScript
importScripts("./Generator.mjs");
|
|
|
|
class GlitchWorker extends Generator {
|
|
constructor() {
|
|
super();
|
|
|
|
// Delay between these values
|
|
this.config = {
|
|
glitch: { min: 500, max: 2500 },
|
|
randBg: { min: 5000, max: 5000 }
|
|
}
|
|
|
|
this._timers = {};
|
|
|
|
self.addEventListener("message", event => this.message(event));
|
|
self.postMessage("READY");
|
|
}
|
|
|
|
// Run a scoped function on a random interval between
|
|
queue(func) {
|
|
clearTimeout(this._timers[func]);
|
|
const next = Generator.randInt(this.config[func].min, this.config[func].max);
|
|
this._timers[func] = setTimeout(() => this.queue(func), next);
|
|
|
|
this[func]?.();
|
|
}
|
|
|
|
// Set background by id and stop randBg animation
|
|
async forceBg(id) {
|
|
clearTimeout(this._timers.randBg);
|
|
|
|
const image = await this.fetchBg(id);
|
|
this.bg.current = image;
|
|
|
|
this.setBg(image);
|
|
}
|
|
|
|
// Event handler for messages from parent thread
|
|
message(event) {
|
|
const data = typeof event.data === "object" ? event.data : [event.data];
|
|
|
|
switch(data[0]) {
|
|
case "START":
|
|
this.bg.dir = data[1];
|
|
this.randBg();
|
|
for(const func of Object.keys(this.config)) {
|
|
this.queue(func);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
self.glitch = new GlitchWorker(); |