vlw.se/assets/js/modules/glitch/GlitchWorker.js
Victor Westerlund 140132fa72
feat: release 1.0.0 (#1)
* wip: 2024-02-13T12:59:17+0100 (1707825557)

* wip: 2024-02-21T03:16:48+0100 (1708481808)

* wip: 2024-02-21T20:50:20+0100 (1708545020)

* wip: 2024-02-21T20:50:20+0100 (1708545020)

* wip: 2024-03-01T13:17:58+0100 (1709295478)

* wip: 2024-03-06T12:06:58+0100 (1709723218)

* wip: 2024-03-07T15:07:57+0100 (1709820477)

* wip: 2024-03-09T01:36:44+0100 (1709944604)

* wip: 2024-03-14T23:24:12+0100 (1710455052)

* wip: 2024-03-28T18:27:40+0100 (1711646860)

* wip: 2024-03-28T18:27:40+0100 (1711646860)

* feat: create README

* wip: 2024-04-01T12:21:45+0200 (1711966905)
2024-04-01 10:22:25 +00:00

54 lines
No EOL
1.2 KiB
JavaScript
Executable file

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();