UNPKG

@fancyapps/ui

Version:

Robust JavaScript UI Component Library

3 lines (2 loc) • 3.48 kB
/*! License details at fancyapps.com/license */ import{clamp as n}from"../utils/clamp.js";import{isEqual as t}from"../utils/isEqual.js";const e=function(n){for(const t of u)t.getState()===i.Running&&t.tick(a?n-a:0);a=n,s=window.requestAnimationFrame(e)};var i,o,r;!function(n){n[n.Initializing=0]="Initializing",n[n.Running=1]="Running",n[n.Paused=2]="Paused",n[n.Completed=3]="Completed",n[n.Destroyed=4]="Destroyed"}(i||(i={})),function(n){n[n.Spring=0]="Spring",n[n.Ease=1]="Ease"}(o||(o={})),function(n){n[n.Loop=0]="Loop",n[n.Reverse=1]="Reverse"}(r||(r={}));const u=new Set;let s=null,a=0;function c(){let a=i.Initializing,f=o.Ease,l=0,p=0,g=c.Easings.Linear,m=500,d=0,S=0,h=0,y=0,E=1/0,R=.01,b=.01,M=!1,w={},v=null,C={},L={},I={},D=0,z=0,N=r.Loop,V=c.Easings.Linear;const j=new Map;function q(n,...t){for(const e of j.get(n)||[])e(...t)}function x(n){return p=0,n?v=setTimeout(()=>{A()},n):A(),P}function A(){a=i.Running,q("start",C,L)}function F(){if(a=i.Completed,I={},q("end",C),a===i.Completed)if(l<D){if(l++,N===r.Reverse){const n={...w};w={...L},L=n}x(z)}else l=0;return P}const P={getState:function(){return a},easing:function(n){return g=n,f=o.Ease,I={},P},duration:function(n){return m=n,P},spring:function(n={}){f=o.Spring;const t={velocity:0,mass:1,tension:170,friction:26,restDelta:.1,restSpeed:.1,maxSpeed:1/0,clamp:!0},{velocity:e,mass:i,tension:r,friction:u,restDelta:s,restSpeed:a,maxSpeed:c,clamp:l}={...t,...n};return d=e,S=i,h=r,y=u,b=s,R=a,E=c,M=l,I={},P},isRunning:function(){return a===i.Running},isSpring:function(){return f===o.Spring},from:function(n){return C={...n},P},to:function(n){return L=n,P},repeat:function(n,t=0,e=r.Loop,i){return D=n,z=t,N=e,V=i||g,P},on:function(n,t){var e,i;return e=n,i=t,j.set(e,[...j.get(e)||[],i]),P},off:function(n,t){var e,i;return e=n,i=t,j.has(e)&&j.set(e,j.get(e).filter(n=>n!==i)),P},start:function(n){return t(C,L)||(a=i.Initializing,w={...C},u.add(this),s||(s=window.requestAnimationFrame(e)),x(n)),P},pause:function(){return v&&(clearTimeout(v),v=null),a===i.Running&&(a=i.Paused,q("pause",C)),P},end:F,tick:function(e){e>50&&(e=50),p+=e;let u=0,s=!1;if(a!==i.Running)return P;if(f===o.Ease){u=n(0,p/m,1),s=1===u;const t=N===r.Reverse?V:g;for(const n in C)C[n]=w[n]+(L[n]-w[n])*t(u)}if(f===o.Spring){const t=.001*e;let i=0;for(const e in C){const o=L[e];let r=C[e];if("number"!=typeof o||isNaN(o)||"number"!=typeof r||isNaN(r))continue;if(Math.abs(o-r)<=b){C[e]=o,I[e]=0;continue}I[e]||("object"==typeof d&&"number"==typeof d[e]?I[e]=d[e]:I[e]="number"==typeof d?d:0);let u=I[e];u=n(-1*Math.abs(E),u,Math.abs(E));const s=u*S*y;u+=((r>o?-1:1)*(Math.abs(o-r)*h)-s)/S*t,r+=u*t;const a=C[e]>o?r<o:r>o;let c=Math.abs(u)<R&&Math.abs(o-r)<=b;M&&a&&(c=!0),c?(r=o,u=0):i++,C[e]=r,I[e]=u}s=!i}const c={...L};return q("step",C,w,L,u),s&&a===i.Running&&t(L,c)&&(a=i.Completed,F()),P},getStartValues:function(){return w},getCurrentValues:function(){return C},getCurrentVelocities:function(){return I},getEndValues:function(){return L},destroy:function(){a=i.Destroyed,v&&(clearTimeout(v),v=null),w=C=L={},u.delete(this)}};return P}c.destroy=()=>{for(const n of u)n.destroy();s&&(cancelAnimationFrame(s),s=null)},c.Easings={Linear:function(n){return n},EaseIn:function(n){return 0===n?0:Math.pow(2,10*n-10)},EaseOut:function(n){return 1===n?1:1-Math.pow(2,-10*n)},EaseInOut:function(n){return 0===n?0:1===n?1:n<.5?Math.pow(2,20*n-10)/2:(2-Math.pow(2,-20*n+10))/2}};export{c as Tween,r as TweenRepeatType,i as TweenState};