Skip to content

Instantly share code, notes, and snippets.

@bundle-js
Created March 22, 2023 05:20
Show Gist options
  • Save bundle-js/40fc9e159b09daab991e87abe39ed5bb to your computer and use it in GitHub Desktop.
Save bundle-js/40fc9e159b09daab991e87abe39ed5bb to your computer and use it in GitHub Desktop.
function M(n){let e=parseFloat(n);return typeof e=="number"&&!Number.isNaN(e)}function m(n,e,t){return Math.min(Math.max(n,e),t)}function E(n,e,t){return e+(t-e)*n}function I(n,e){return Math.round(n*10**e)/10**e}function N(n){let e=parseFloat(n);return n.toString().replace(e.toString(),"")}function U(n){return function(e,t,s){return e.map(r=>n(r,t,s))}}var f=(n,[e=1,t=100,s=10,r=0]=[],o)=>{e=m(e,1e-4,1e3),t=m(t,1e-4,1e3),s=m(s,1e-4,1e3),r=m(r,1e-4,1e3);let u=Math.sqrt(t/e),a=s/(2*Math.sqrt(t*e)),g=a<1?u*Math.sqrt(1-a*a):0,c=a<1?(a*u+-r)/g:-r+u,i=o?o*n/1e3:n;return a<1?i=Math.exp(-i*a*u)*(Math.cos(g*i)+c*Math.sin(g*i)):i=(1+c*i)*Math.exp(-i*u),1-i},h=new Map,d=1e5;function v([n,e,t,s]=[]){let r=[n,e,t,s],o=`${r}`;if(h.has(o))return h.get(o);let u=1/6,a=0,g=0;for(;++g<d;){if(Math.abs(1-f(a,r))<.001){let i=a,y=1;for(;++g<d&&(a+=u,!(Math.abs(1-f(a,r))>=.001));)if(y++,y===16){let F=i*1e3;return h.set(o,[F,g]),[F,g]}}a+=u}let c=a*1e3;return h.set(o,[c,g]),[c,g]}function x(n){return(e,t=[],s)=>1-n(1-e,t,s)}function k(n){return function(e,t=[],s){return e<.5?n(e*2,t,s)/2:1-n(e*-2+2,t,s)/2}}function A(n){return function(e,t=[],s){return e<.5?(1-n(1-e*2,t,s))/2:(n(e*2-1,t,s)+1)/2}}var P=f,$=x(f),q=k(f),T=A(f);function S(n,e,t=3){let s=e.length-1,r=m(Math.floor(n*s),0,s-1),o=e[r],u=e[r+1],a=(n-r/s)*s;return I(E(a,o,u),t)}function b(n,e){let t=e.length-1;n=m(n,0,1);let s=Math.round(n*t);return e[s]}var _=b;function C(n,e,t=3){let s="";return M(e[0])&&(s=N(e[0])),S(n,e.map(r=>typeof r=="number"?r:parseFloat(r)),t)+s}function L(n,e,t=3){return e.every(s=>typeof s=="number")?S(n,e,t):e.every(s=>M(s))?C(n,e,t):b(n,e)}var p={spring:f,"spring-in":P,"spring-out":$,"spring-in-out":q,"spring-out-in":T},O=Object.keys(p);function B(n,e){p={...p,[n]:e},O=Object.keys(p)}function G(n){p={...p,...n},O=Object.keys(p)}function j(n){let e=/(\(|\s)([^)]+)\)?/.exec(n.toString());return e?e[2].split(",").map(t=>{let s=parseFloat(t);return Number.isNaN(s)?t.trim():s}):[]}function w(n={}){let e=typeof n=="string"||Array.isArray(n)&&typeof n[0]=="function",{easing:t=[f,1,100,10,0],numPoints:s=38,decimal:r=3}=e?{easing:n}:n;if(typeof t=="string"){let o=p[t.replace(/(\(|\s).+/,"").toLowerCase().trim()],u=j(t);t=[o,...u]}return{easing:t,numPoints:s,decimal:r}}var l=new Map;function D(n={}){let{easing:e,numPoints:t}=w(n);if(Array.isArray(e)){if(typeof e[0]!="function")throw new Error("[spring-easing] A frame function is required as the first element in the easing array, e.g. [SpringFrame, ...]");e.length>1&&e.length<5&&console.warn(`[spring-easing] Be careful of only setting some of the spring parameters, you've only set ${5-e.length} spring parameter(s). The easing works best in the format:
* "spring-out(mass, stiffness, damping, velocity)" or
* [SpringOutFrame, mass, stiffness, damping, velocity].`),e.length>5&&console.warn(`[spring-easing] You entered ${5-e.length} more spring parameter(s) than necessary. The easing needs to be in the format:
* "spring-out(mass, stiffness, damping, velocity)" or
* [SpringOutFrame, mass, stiffness, damping, velocity].`)}else throw new Error(`[spring-easing] The easing needs to be in the format:
* "spring-out(mass, stiffness, damping, velocity)" or
* [SpringOutFrame, mass, stiffness, damping, velocity], the easing recieved is "${e}", [spring-easing] doesn't really know what to do with that.`);let[s,...r]=e,[o,u=38]=v(r);t||(t=u);let a=`${r},${t}`;if(l.has(a)){let i=l.get(a);if(i.has(s))return i.get(s)}let g=[];for(let i=0;i<t;i++)g[i]=s(i/(t-1),r,o);let c=l.has(a)?l.get(a):new WeakMap;return c.set(s,[g,o]),l.set(a,c),[g,o]}function K(n,e={},t=L){let s=w(e),[r,o]=D(s);return[r.map(u=>t(u,n,s.decimal)),o]}export{k as EaseInOut,x as EaseOut,A as EaseOutIn,h as EasingDurationCache,O as EasingFunctionKeys,p as EasingFunctions,w as EasingOptions,l as FramePtsCache,D as GenerateSpringFrames,d as INFINITE_LOOP_LIMIT,K as SpringEasing,f as SpringFrame,P as SpringInFrame,q as SpringInOutFrame,$ as SpringOutFrame,T as SpringOutInFrame,v as getSpringDuration,N as getUnit,L as interpolateComplex,S as interpolateNumber,b as interpolateSequence,C as interpolateString,_ as interpolateUsingIndex,M as isNumberLike,m as limit,j as parseEasingParameters,B as registerEasingFunction,G as registerEasingFunctions,E as scale,U as toAnimationFrames,I as toFixed};
/*!
* Spring solver inspired by Webkit Copyright © 2016 Apple Inc. All rights reserved. https://webkit.org/demos/spring/spring.js
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment