UNPKG

@omnimedia/omnitool

Version:

open source video processing tools

64 lines 1.73 kB
import { pub } from "@e280/stz"; import { ms } from "../../../units/ms.js"; import { fps } from "../../../units/fps.js"; export const realtime = () => { let playing = false; let frameRate = fps(60); let frameDuration = 1000 / frameRate; let lastNow = 0; let lastComposite = 0; const onTick = pub(); let resolveTick = null; const loop = (now) => { requestAnimationFrame(loop); if (!playing) return; lastNow = now; while (now - lastComposite >= frameDuration) { lastComposite += frameDuration; resolveTick?.(); resolveTick = null; onTick(); } }; async function* ticks() { lastNow = performance.now(); lastComposite = lastNow; requestAnimationFrame(loop); while (true) { await new Promise(r => resolveTick = r); yield; } } return { play() { if (playing) return; playing = true; lastNow = performance.now(); lastComposite = lastNow; }, pause() { playing = false; }, setFPS(v) { frameRate = v; frameDuration = 1000 / frameRate; }, isPlaying() { return playing; }, ticks, onTick }; }; export const fixedStep = async (opts, onFrame) => { const dt = ms(1000 / opts.fps); const durationInSeconds = opts.duration / 1000; const total = Math.ceil(durationInSeconds * opts.fps); for (let i = 0; i < total; i++) { const t = ms(i * dt); await onFrame(t, i); } }; //# sourceMappingURL=schedulers.js.map