@infinityfx/lively
Version:
Feature complete, lightweight react animation library.
3 lines (2 loc) • 815 B
JavaScript
import{useRef as r}from"react";import t from"./use-link.js";function e(e,{stiffness:n=2,damping:a=.1,mass:s=1,restThreshold:u=.01}={}){const c=r({t:0,value:e,velocity:new Array(Array.isArray(e)?e.length:1).fill(0)}),o=t(e),i=r(o((r=>r)));function l(){const r=Date.now(),t=Math.min((r-c.current.t)/1e3,1/12),e=o(),i=c.current.velocity,m=Array.isArray(e);let f=0,h=0;const y=m?e:[e],A=Array.isArray(c.current.value)?c.current.value:[c.current.value];for(let r=0;r<i.length;r++){const e=A[r]-y[r];i[r]+=(n*e-a*i[r])/s,y[r]+=i[r]*t,f+=Math.abs(e),h+=Math.abs(i[r])}f/=i.length,h/=i.length,o.set(m?y:y[0]),c.current.t=r,(f>u||h>u)&&requestAnimationFrame(l)}return i.current.set=r=>{c.current.value=r,c.current.t=Date.now(),requestAnimationFrame(l)},i.current}export{e as default};
//# sourceMappingURL=use-spring.js.map