UNPKG

@ssgoi/core

Version:

Core animation engine for SSGOI - Native app-like page transitions with spring physics

2 lines (1 loc) 3.67 kB
"use strict";var L=Object.defineProperty;var _=(o,e,t)=>e in o?L(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var n=(o,e,t)=>_(o,typeof e!="symbol"?e+"":e,t);class u{constructor(e){n(this,"omega");n(this,"zeta");n(this,"restDelta");n(this,"restSpeed");const{stiffness:t,damping:s}=e,i=1;this.omega=Math.sqrt(t/i),this.zeta=s/(2*Math.sqrt(t*i)),this.restDelta=e.restDelta??.01,this.restSpeed=e.restSpeed??.01}step(e,t,s){const i=Math.min(s,.033),{omega:r,zeta:p}=this,{position:c,velocity:d}=e,h=-2*i*p*r*d,a=i*r*r*(t-c),l=d+h+a;return{position:c+i*l,velocity:l}}isSettled(e,t){const s=Math.abs(t-e.position),i=Math.abs(e.velocity);return s<this.restDelta&&i<this.restSpeed}}class E{constructor(e){n(this,"leader");n(this,"follower");n(this,"restDelta");n(this,"restSpeed");this.restDelta=e.restDelta??.01,this.restSpeed=e.restSpeed??.01,this.leader=new u({stiffness:e.stiffness,damping:e.damping,restDelta:this.restDelta,restSpeed:this.restSpeed});let t,s,i,r;typeof e.follower=="number"?(t=e.stiffness*e.follower,s=e.damping):e.follower?(t=e.follower.stiffness,s=e.follower.damping,i=e.follower.restDelta,r=e.follower.restSpeed):(t=e.stiffness,s=e.damping),this.follower=new u({stiffness:t,damping:s,restDelta:i??this.restDelta,restSpeed:r??this.restSpeed})}step(e,t,s){const r=e._leader??{position:e.position,velocity:e.velocity},p=this.leader.step(r,t,s),c=this.follower.step(e,p.position,s);return{position:c.position,velocity:c.velocity,_leader:p}}isSettled(e,t){const s=e,i=Math.abs(t-e.position)<this.restDelta&&Math.abs(e.velocity)<this.restSpeed;return s._leader?Math.abs(t-s._leader.position)<this.restDelta&&Math.abs(s._leader.velocity)<this.restSpeed&&i:i}}const v=.01,M=500,R=10;class O{constructor(e){n(this,"acceleration");n(this,"resistance");n(this,"resistanceType");n(this,"min");n(this,"max");n(this,"bounceStiffness");n(this,"bounceDamping");n(this,"restDelta");this.acceleration=e.acceleration,this.resistance=e.resistance,this.resistanceType=e.resistanceType??"quadratic",this.min=e.min,this.max=e.max,this.bounceStiffness=e.bounceStiffness??M,this.bounceDamping=e.bounceDamping??R,this.restDelta=e.restDelta??v}step(e,t,s){const i=Math.min(s,.033),{acceleration:r,resistance:p,resistanceType:c,min:d,max:h}=this,{position:a,velocity:l}=e,f=d!==void 0&&a<d,w=h!==void 0&&a>h;let b;if(f||w){const D=a-(f?d:h),I=-this.bounceStiffness*D,H=-this.bounceDamping*l;b=I+H}else{const m=t>a?1:-1;let D;c==="linear"?D=p*l:D=p*l*Math.abs(l),b=m*r-D}const y=l+b*i;let S=a+y*i;if(!f&&!w){const m=t>a?1:-1;(m>0&&S>t||m<0&&S<t)&&(S=t)}return{position:S,velocity:S===t?0:y}}isSettled(e,t){return Math.abs(t-e.position)<this.restDelta}}class T{static from(e){if(e.spring&&e.inertia)throw new Error("Cannot use both 'spring' and 'inertia' together. Choose one physics type.");if(e.inertia)return new O({acceleration:e.inertia.acceleration,resistance:e.inertia.resistance,resistanceType:e.inertia.resistanceType,min:e.inertia.min,max:e.inertia.max,bounceStiffness:e.inertia.bounceStiffness,bounceDamping:e.inertia.bounceDamping,restDelta:e.inertia.restDelta});const t=e.spring??{stiffness:300,damping:30};if(t.doubleSpring){const s=t.doubleSpring;let i;return typeof s=="number"?i=s:typeof s=="object"&&(i={stiffness:s.stiffness,damping:s.damping}),new E({stiffness:t.stiffness,damping:t.damping,follower:i,restDelta:t.restDelta,restSpeed:t.restSpeed})}return new u({stiffness:t.stiffness,damping:t.damping,restDelta:t.restDelta,restSpeed:t.restSpeed})}static fromSpring(e){return T.from({spring:e})}}exports.DoubleSpringIntegrator=E;exports.InertiaIntegrator=O;exports.IntegratorProvider=T;exports.SETTLE_THRESHOLD=.05;exports.SpringIntegrator=u;