UNPKG

baseframe-js

Version:

Baseframe JS is a comprehensive suite of modular plugins and utilities designed for front-end development

19 lines (18 loc) 602 B
const animate = (fn, fps = 60) => { let requestAnimate = null; let msPrev = performance.now(); const msPerFrame = 1000 / fps; const cancelAnimation = () => cancelAnimationFrame(requestAnimate); const refresh = (msNow) => { requestAnimate = window.requestAnimationFrame(refresh); const msPassed = msNow - msPrev; if (msPassed < msPerFrame) return; const excessTime = msPassed % msPerFrame; msPrev = msNow - excessTime; fn(cancelAnimation); }; refresh(msPrev); return cancelAnimation; }; export default animate;