UNPKG

@fancyapps/ui

Version:

Robust JavaScript UI Component Library

1 lines 3.9 kB
!/*! License details at fancyapps.com/license */function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).window=e.window||{})}(this,function(e){"use strict";let n=function(e=0,n=0,t=0){return Math.max(Math.min(n,t),e)};function t(e){return"object"==typeof e&&null!==e&&e.constructor===Object&&"[object Object]"===Object.prototype.toString.call(e)||Array.isArray(e)}function i(e,n){let r=Object.keys(e),o=Object.keys(n);return r.length===o.length&&r.every(r=>{let o=e[r],u=n[r];return"function"==typeof o?`${o}`==`${u}`:t(o)&&t(u)?i(o,u):o===u})}let r=1/60*3e3,o=function(e){for(let n of p)n.getState()===u.Running&&n.tick(d?e-d:0);d=e,g=window.requestAnimationFrame(o)},u=((a={})[a.Initializing=0]="Initializing",a[a.Running=1]="Running",a[a.Paused=2]="Paused",a[a.Completed=3]="Completed",a[a.Destroyed=4]="Destroyed",a);var a,s,f,l=((s=l||{})[s.Spring=0]="Spring",s[s.Ease=1]="Ease",s);let c=((f={})[f.Loop=0]="Loop",f[f.Reverse=1]="Reverse",f),p=new Set,g=null,d=0;function m(){let e=u.Initializing,t=l.Ease,a=0,s=0,f=m.Easings.Linear,d=500,y=0,b=0,h=0,w=0,R=1/0,S=.01,M=.01,E=!1,j={},T=null,v={},C={},L={},O=0,I=0,x=c.Loop,A=m.Easings.Linear,k=new Map;function z(e,...n){for(let t of k.get(e)||[])t(...n)}function D(e){return s=0,e?T=setTimeout(()=>{N()},e):N(),F}function N(){e=u.Running,z("start",v,C)}function V(){if(e=u.Completed,L={},z("end",v),e===u.Completed)if(a<O){if(a++,x===c.Reverse){let e={...j};j={...C},C=e}D(I)}else a=0;return F}let F={getState:function(){return e},easing:function(e){return f=e,t=l.Ease,L={},F},duration:function(e){return d=e,F},spring:function(e={}){t=l.Spring;let{velocity:n,mass:i,tension:r,friction:o,restDelta:u,restSpeed:a,maxSpeed:s,clamp:f}={velocity:0,mass:1,tension:170,friction:26,restDelta:.1,restSpeed:.1,maxSpeed:1/0,clamp:!0,...e};return y=n,b=i,h=r,w=o,M=u,S=a,R=s,E=f,L={},F},isRunning:function(){return e===u.Running},isSpring:function(){return t===l.Spring},from:function(e){return v={...e},F},to:function(e){return C=e,F},repeat:function(e,n=0,t=c.Loop,i){return O=e,I=n,x=t,A=i||f,F},on:function(e,n){return k.set(e,[...k.get(e)||[],n]),F},off:function(e,n){return k.has(e)&&k.set(e,k.get(e).filter(e=>e!==n)),F},start:function(n){return e=u.Initializing,i(j={...v},C)||(p.add(this),g||(g=window.requestAnimationFrame(o)),D(n)),F},pause:function(){return T&&(clearTimeout(T),T=null),e===u.Running&&(e=u.Paused,z("pause",v)),F},end:V,tick:function(o){o>r&&(o=r),s+=o;let a=0,p=!1;if(e!==u.Running)return F;if(t===l.Ease){p=1===(a=n(0,s/d,1));let e=x===c.Reverse?A:f;for(let n in v)v[n]=j[n]+(C[n]-j[n])*e(a)}if(t===l.Spring){let e=.001*o,t=0;for(let i in v){let r=C[i],o=v[i];if(!("number"==typeof r&&!isNaN(r))||!("number"==typeof o&&!isNaN(o)))continue;if(Math.abs(r-o)<=M){v[i]=r,L[i]=0;continue}L[i]||("object"==typeof y&&"number"==typeof y[i]?L[i]=y[i]:"number"==typeof y?L[i]=y:L[i]=0);let u=L[i],a=(u=n(-1*Math.abs(R),u,Math.abs(R)))*b*w,s=Math.abs(r-o)*h;u+=((o>r?-1:1)*s-a)/b*e,o+=u*e;let f=v[i]>r?o<r:o>r,l=Math.abs(u)<S&&Math.abs(r-o)<=M;E&&f&&(l=!0),l?(o=r,u=0):t++,v[i]=o,L[i]=u}p=!t}let g={...C};return z("step",v,j,C,a),p&&e===u.Running&&i(C,g)&&(e=u.Completed,V()),F},getStartValues:function(){return j},getCurrentValues:function(){return v},getCurrentVelocities:function(){return L},getEndValues:function(){return C},destroy:function(){e=u.Destroyed,T&&(clearTimeout(T),T=null),j=v=C={},p.delete(this)}};return F}m.destroy=()=>{for(let e of p)e.destroy();g&&(cancelAnimationFrame(g),g=null)},m.Easings={Linear:function(e){return e},EaseIn:function(e){return 0===e?0:Math.pow(2,10*e-10)},EaseOut:function(e){return 1===e?1:1-Math.pow(2,-10*e)},EaseInOut:function(e){return 0===e?0:1===e?1:e<.5?Math.pow(2,20*e-10)/2:(2-Math.pow(2,-20*e+10))/2}},e.Tween=m,e.TweenRepeatType=c,e.TweenState=u});