@figliolia/ripples
Version:
WebGL ripples based on the clever work of Pim Schreurs
37 lines (36 loc) • 934 B
JavaScript
import { Subscriptable } from "@figliolia/event-emitter";
export class Animations {
static frame = null;
static callstack = new Subscriptable();
static register(callback) {
const ID = this.callstack.register(callback);
void Promise.resolve().then(() => {
this.nextFrame();
});
return ID;
}
static delete(ID) {
return this.callstack.remove(ID);
}
static nextFrame() {
if (this.frame) {
return;
}
this.animate();
}
static animate() {
this.frame = requestAnimationFrame(() => {
if (!this.callstack.length) {
return this.closeLoop();
}
this.callstack.execute();
this.animate();
});
}
static closeLoop() {
if (this.frame) {
cancelAnimationFrame(this.frame);
this.frame = null;
}
}
}