feat: loop

This commit is contained in:
cha0s 2021-01-17 11:46:01 -06:00
parent cf05b0b137
commit 9d876ab2c4
2 changed files with 17 additions and 0 deletions

View File

@ -8,6 +8,7 @@ export {
} from './animation-frame';
export {default as AnimationView} from './animation-view';
export {default as Lfo} from './lfo';
export * from './loop';
export {default as Ticker} from './ticker';
export {default as TimedIndex} from './timed-index';
export {default as Transition, TransitionResult} from './transition';

View File

@ -0,0 +1,16 @@
export const createLoop = (ticker, {frequency = 1 / 60, sampler = Date.now} = {}) => {
let lastTime = sampler();
return setInterval(
() => {
const now = sampler();
const elapsed = (now - lastTime) / 1000;
lastTime = now;
ticker(elapsed);
},
1000 * frequency,
);
};
export const destroyLoop = (id) => {
clearInterval(id);
};