@fancyapps/ui
Version:
Robust JavaScript UI Component Library
1 lines • 3.92 kB
JavaScript
!/*! 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)},t=e=>"object"==typeof e&&null!==e&&e.constructor===Object&&"[object Object]"===Object.prototype.toString.call(e);function i(e){return t(e)||Array.isArray(e)}function r(e,n){let t=Object.keys(e),o=Object.keys(n);return t.length===o.length&&t.every(t=>{let o=e[t],u=n[t];return"function"==typeof o?`${o}`==`${u}`:i(o)&&i(u)?r(o,u):o===u})}let o=1/60*3e3,u=function(e){for(let n of g)n.getState()===a.Running&&n.tick(m?e-m:0);m=e,d=window.requestAnimationFrame(u)},a=((s={})[s.Initializing=0]="Initializing",s[s.Running=1]="Running",s[s.Paused=2]="Paused",s[s.Completed=3]="Completed",s[s.Destroyed=4]="Destroyed",s);var s,f,l,c=((f=c||{})[f.Spring=0]="Spring",f[f.Ease=1]="Ease",f);let p=((l={})[l.Loop=0]="Loop",l[l.Reverse=1]="Reverse",l),g=new Set,d=null,m=0;function y(){let e=a.Initializing,t=c.Ease,i=0,s=0,f=y.Easings.Linear,l=500,m=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=p.Loop,A=y.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=a.Running,z("start",v,C)}function V(){if(e=a.Completed,L={},z("end",v),e===a.Completed)if(i<O){if(i++,x===p.Reverse){let e={...j};j={...C},C=e}D(I)}else i=0;return F}let F={getState:function(){return e},easing:function(e){return f=e,t=c.Ease,L={},F},duration:function(e){return l=e,F},spring:function(e={}){t=c.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 m=n,b=i,h=r,w=o,M=u,S=a,R=s,E=f,L={},F},isRunning:function(){return e===a.Running},isSpring:function(){return t===c.Spring},from:function(e){return v={...e},F},to:function(e){return C=e,F},repeat:function(e,n=0,t=p.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=a.Initializing,r(j={...v},C)||(g.add(this),d||(d=window.requestAnimationFrame(u)),D(n)),F},pause:function(){return T&&(clearTimeout(T),T=null),e===a.Running&&(e=a.Paused,z("pause",v)),F},end:V,tick:function(i){i>o&&(i=o),s+=i;let u=0,g=!1;if(e!==a.Running)return F;if(t===c.Ease){g=1===(u=n(0,s/l,1));let e=x===p.Reverse?A:f;for(let n in v)v[n]=j[n]+(C[n]-j[n])*e(u)}if(t===c.Spring){let e=.001*i,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 m&&"number"==typeof m[i]?L[i]=m[i]:"number"==typeof m?L[i]=m: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}g=!t}let d={...C};return z("step",v,j,C,u),g&&e===a.Running&&r(C,d)&&(e=a.Completed,V()),F},getStartValues:function(){return j},getCurrentValues:function(){return v},getCurrentVelocities:function(){return L},getEndValues:function(){return C},destroy:function(){e=a.Destroyed,T&&(clearTimeout(T),T=null),j=v=C={},g.delete(this)}};return F}y.destroy=()=>{for(let e of g)e.destroy();d&&(cancelAnimationFrame(d),d=null)},y.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=y,e.TweenRepeatType=p,e.TweenState=a});