UNPKG

929 BJavaScriptView Raw
1const t=({stiffness:t=100,damping:e=10,mass:n=1,from:s=0,to:a=1,velocity:r=0,restSpeed:o=2,restDelta:c=.1}={})=>{r=r?-r/1e3:0;const i=((t=100,e=10,n=1)=>e/(2*Math.sqrt(t*n)))(t,e,n),u=a-s,l=Math.sqrt(t/n)/1e3,f=((t,e)=>t*Math.sqrt(1-e*e))(l,i),h={done:!1,value:s,velocity:r};let v;return v=i<1?t=>a-Math.exp(-i*l*t)*((i*l*u-r)/f*Math.sin(f*t)+u*Math.cos(f*t)):t=>a-Math.exp(-l*t)*(u+(r+l*u)*t),{next:t=>{h.value=v(t),h.velocity=0===t?r:function(t,e,n){const s=Math.max(e-5,0);return function(t,e){return e?t*(1e3/e):0}(n-t(s),5)}(v,t,h.value);const e=Math.abs(h.velocity)<=o,n=Math.abs(a-h.value)<=c;return h.done=e&&n,h}}};const e=(e=200,n=15,s=1,a)=>({isKeyframeGenerator:!0,generate:r=>{if(r.length>2)return!1;const o=t({stiffness:e,damping:n,mass:s,velocity:a,from:r[0],to:r[1]});let c=!1,i=0,u=0;for(;!c;){i++;const{value:t,done:e}=o.next(u);r.push(t),u+=10,c=i>200||e}return{keyframes:r,duration:u}}});export{e as spring};