UNPKG

animare

Version:

Advanced animation library for modern JavaScript.

9 lines (5 loc) 6.78 kB
function B(e,n,t){let o=t?.forcePlay??!0,r=!1,a=new IntersectionObserver(i=>{if(e)for(let u=0;u<i.length;u++){let c=i[u].isIntersecting;if(t?.onVisibilityChange?.(c),!e){console.error("[autoPause] The timeline is not defined.");return}if(c){if(e.timelineInfo.isPaused&&r){r=!1,e.resume();return}o&&!e.timelineInfo.isPaused&&e.play();return}e.timelineInfo.isPlaying&&(r=!0,e.pause())}},t);return n&&a.observe(n),()=>a.disconnect()}var j=(e,n)=>1-3*n+3*e,I=(e,n)=>3*n-6*e,E=e=>3*e,A=(e,n,t)=>((j(n,t)*e+I(n,t))*e+E(n))*e,F=(e,n,t)=>3*j(n,t)*e*e+2*I(n,t)*e+E(n);function W(e,n,t,o,r){let a,i,u=0;do i=n+(t-n)/2,a=A(i,o,r)-e,a>0?t=i:n=i;while(Math.abs(a)>1e-7&&++u<10);return i}function H(e,n,t,o){for(let r=0;r<4;++r){let a=F(n,t,o);if(a===0)return n;let i=A(n,t,o)-e;n-=i/a}return n}var U=e=>e;function T(e,n,t,o){if(!(0<=e&&e<=1&&0<=t&&t<=1))throw new Error(`/n/n\u26D4 [animare] \u27A1\uFE0F [ease] \u27A1\uFE0F [cubicBezier] : bezier x values must be in [0, 1] range. !! `);if(e===n&&t===o)return U;let r=typeof Float32Array=="function"?new Float32Array(11):new Array(11);for(let i=0;i<11;++i)r[i]=A(i*.1,e,t);function a(i){let u=0,s=1,c=10;for(;s!==c&&r[s]<=i;++s)u+=.1;--s;let l=(i-r[s])/(r[s+1]-r[s]),m=u+l*.1,p=F(m,e,t);return p>=.001?H(i,m,e,t):p===0?m:W(i,u,u+.1,e,t)}return i=>i===0||i===1?i:A(a(i),n,o)}function G(e){let n=e.match(/-?[0-9.]+/g)?.map(parseFloat),t=[];if(!n)return t;t.push([n[0],n[1]]);for(let o=2;o<n.length;o+=6)t.push([n[o],n[o+1],n[o+2],n[o+3],n[o+4],n[o+5]]);return t}function D(e){let n=[],t=0,o=0;for(let r=0;r<e.length;r++){let a=e[r],i=a[0],u=1-a[1];if(!r){t=i,o=u;continue}let s=a[2],c=1-a[3],l=a[4],m=1-a[5];n.push([t,o,i,u,s,c,l,m]),t=l,o=m}return n}function k(e){let n=G(e),t=D(n),o=1e-6;return r=>{if(r===0)return t[0][1];if(r===1)return t[t.length-1][7];let a=0;for(let i=0;i<t.length;i++){let[u,s,c,l,m,p,O,M]=t[i];if(r<a||r>O)continue;a=O;let f=0,h=1,b=(f+h)/2,K=0;for(;b>=f&&b<=1;){let V=1-b,g=V*V,S=g*V,w=u*S+c*3*g*b+m*3*V*b*b+O*b**3,P=s*S+l*3*g*b+p*3*V*b*b+M*b**3;if(++K>50||Math.abs(w-r)<=o)return P;w>=r?h=b:f=b,b=(f+h)/2}return 0}return 0}}function N({mass:e=1,stiffness:n=100,damping:t=10,velocity:o=0,duration:r=1e3}={}){let a=(i,u,s)=>Math.min(Math.max(i,u),s);return i=>{if(i===0||i===1)return i;e=a(e,.1,1e3),n=a(n,.1,1e3),t=a(t,.1,1e3),o=a(o,.1,1e3);let u=Math.sqrt(n/e),s=t/(2*Math.sqrt(n*e)),c=s<1?u*Math.sqrt(1-s*s):0,l=1,m=s<1?(s*u+-o)/c:-o+u,p=r?r*i/1e3:i;return p=s<1?Math.exp(-p*s*u)*(l*Math.cos(c*p)+m*Math.sin(c*p)):(l+m*p)*Math.exp(-p*u),1-p}}function _(e=10,n=!0){let t=(r,a,i)=>Math.min(Math.max(r,a),i),o=n?Math.ceil:Math.floor;return r=>o(t(r,0,1)*e)/e}function z(e){if(!(e instanceof Float32Array)&&!Array.isArray(e))throw new Error(` \u26D4 [animare] \u27A1\uFE0F [ease] \u27A1\uFE0F [fromPoints] : first param must be an Array or Float32Array. !! `);let n=e.length;return t=>e[Math.floor(t*n)]??e[n-1]}function d(e=1){let n=e*Math.PI;return t=>1-Math.pow(Math.cos(t*Math.PI/2),3)*Math.cos(t*n)}function y(e){return e<1/2.75?7.5625*e*e:e<2/2.75?7.5625*(e-=1.5/2.75)*e+.75:e<2.5/2.75?7.5625*(e-=2.25/2.75)*e+.9375:7.5625*(e-=2.625/2.75)*e+.984375}function C(e){return e}var J={back:(e=1.70158)=>n=>(e+1)*n*n*n-e*n*n,bounce:e=>1-y(1-e),circ:e=>1-Math.sqrt(1-Math.pow(e,2)),cubic:e=>e*e*e,elastic:e=>{let n=2*Math.PI/3;return e===0?0:e===1?1:-Math.pow(2,10*e-10)*Math.sin((e*10-10.75)*n)},expo:e=>e===0?0:Math.pow(2,10*e-10),sine:e=>1-Math.cos(e*Math.PI/2),quad:e=>e*e,quart:e=>e*e*e*e,quint:e=>e*e*e*e*e,poly:e=>n=>Math.pow(n,e),wobble:d},$=J;var Q={back:(e=1.70158)=>n=>{let t=e*1.525;return n<.5?Math.pow(2*n,2)*((t+1)*2*n-t)/2:(Math.pow(2*n-2,2)*((t+1)*(n*2-2)+t)+2)/2},bounce:e=>e<.5?(1-y(1-2*e))/2:(1+y(2*e-1))/2,circ:e=>e<.5?(1-Math.sqrt(1-Math.pow(2*e,2)))/2:(Math.sqrt(1-Math.pow(-2*e+2,2))+1)/2,cubic:e=>e<.5?4*e*e*e:1-Math.pow(-2*e+2,3)/2,elastic:e=>{let n=2*Math.PI/4.5;return e===0?0:e===1?1:e<.5?-(Math.pow(2,20*e-10)*Math.sin((20*e-11.125)*n))/2:Math.pow(2,-20*e+10)*Math.sin((20*e-11.125)*n)/2+1},expo:e=>e===0?0:e===1?1:e<.5?Math.pow(2,20*e-10)/2:(2-Math.pow(2,-20*e+10))/2,sine:e=>-(Math.cos(Math.PI*e)-1)/2,quad:e=>e<.5?2*e*e:1-Math.pow(-2*e+2,2)/2,quart:e=>e<.5?8*e*e*e*e:1-Math.pow(-2*e+2,4)/2,quint:e=>e<.5?16*e*e*e*e*e:1-Math.pow(-2*e+2,5)/2,poly:e=>n=>n<.5?Math.pow(2,e-1)*Math.pow(n,e):1-Math.pow(-2*n+2,e)/2,wobble(e=1){return Z(d(e))}};function Z(e){return n=>n<.5?e(n*2)/2:1-e((1-n)*2)/2}var v=Q;var Y={back:(e=1.70158)=>n=>1+(e+1)*Math.pow(n-1,3)+e*Math.pow(n-1,2),bounce:y,circ:e=>Math.sqrt(1-Math.pow(e-1,2)),cubic:e=>1-Math.pow(1-e,3),elastic:e=>{let n=2*Math.PI/3;return e===0?0:e===1?1:Math.pow(2,-10*e)*Math.sin((e*10-.75)*n)+1},expo:e=>e===1?1:1-Math.pow(2,-10*e),sine:e=>Math.sin(e*Math.PI/2),quad:e=>1-(1-e)*(1-e),quart:e=>1-Math.pow(1-e,4),quint:e=>1-Math.pow(1-e,5),poly:e=>n=>1-Math.pow(1-n,e),wobble:(e=1)=>X(d(e))};function X(e){return n=>1-e(1-n)}var R=Y;var ee={in:$,out:R,inOut:v,linear:C,cubicBezier:T,custom:k,fromPoints:z,steps:_,spring:N};function x(e){return typeof e=="object"&&!Array.isArray(e)}function L(e){return e<0?0:e>1?1:e}function ne(e,n,t){if(typeof e=="number"&&typeof n=="number")return e+t*(n-e);if(Array.isArray(e)&&Array.isArray(n))return e.map((o,r)=>o+t*(n[r]-o));if(x(e)&&x(n)){let o=Object.assign({});for(let r in e)if(r in e&&r in n){let a=r;o[a]=e[a]+t*(n[a]-e[a])}return o}throw new Error("Invalid input types for lerp function")}function te(e){let n=e.root??document,t=()=>re(e);return n.addEventListener("scroll",t,{passive:!0}),()=>n.removeEventListener("scroll",t)}function re({timeline:e,element:n,root:t=document.documentElement,axis:o="y",start:r="top",end:a="bottom",startOffset:i=0,endOffset:u=0}){let s=o==="y",c=s?t.clientHeight:t.clientWidth,l=s?t.scrollTop:t.scrollLeft,m=q(n,t,r)+i,p=q(n,t,a)+u,O=m<=l+c,M=p<l,f=0;if(!O)f=0;else if(M)f=1;else{let h=c-(m-p);f=L((l+c-m)/h)}e.seek(e.timelineInfo.duration*f),e.timelineInfo.isPlaying||e.playOneFrame()}function q(e,n,t){return t==="top"?e.offsetTop-n.offsetTop:t==="bottom"?e.offsetTop-n.offsetTop+e.offsetHeight:t==="left"?e.offsetLeft-n.offsetLeft:t==="right"?e.offsetLeft-n.offsetLeft+e.offsetWidth:e.offsetTop}function oe(e){let n=x(e),t=n?e.x:e[0],o=n?e.y:e[1],r=n?e.z:e[2],a=n?"w"in e?e.w:void 0:e[3];return typeof a=="number"?`rgb(${Math.round(t)} ${Math.round(o)} ${Math.round(r)} / ${Math.round(a*100)}%)`:`rgb(${Math.round(t)} ${Math.round(o)} ${Math.round(r)})`}function ie(e){let n=x(e),t=n?e.x:e[0],o=n?e.y:e[1],r=n?e.z:e[2],a=n?"w"in e?e.w:void 0:e[3];return typeof a=="number"?`hsl(${Math.round(t)}deg ${Math.round(o)}% ${Math.round(r)}% / ${Math.round(a*100)}%)`:`hsl(${Math.round(t)}deg ${Math.round(o)}% ${Math.round(r)}%)`}export{B as autoPause,ee as ease,ne as lerp,te as scrollAnimation,ie as vecToHSL,oe as vecToRGB}; //# sourceMappingURL=index.js.map