Skip to content

Instantly share code, notes, and snippets.

@bundle-js
Created October 31, 2023 18:52
Show Gist options
  • Save bundle-js/be7db7e5cf6282c6072f30aff305226f to your computer and use it in GitHub Desktop.
Save bundle-js/be7db7e5cf6282c6072f30aff305226f to your computer and use it in GitHub Desktop.
function S(n){let t=parseFloat(n);return typeof t=="number"&&!Number.isNaN(t)}function m(n,t,e){return Math.min(Math.max(n,t),e)}function w(n,t,e){return t+(e-t)*n}function F(n,t){return Math.round(n*10**t)/10**t}function q(n){let t=parseFloat(n);return n.toString().replace(t.toString(),"")}function j(n,t,e=3){let r=t.length-1,i=[],o=0,s=0,a=n.length;for(;s<a;s++){o=n[s];let l=m(Math.floor(o*r),0,r-1),u=t[l],c=t[l+1],f=(o-l/r)*r;i.push(F(w(f,u,c),e))}return i}function D(n,t){let e=t.length-1,r=[],i=0,o=0,s=n.length;for(;o<s;o++){i=m(n[o],0,1);let a=Math.round(i*e);r.push(t[a])}return r}var v=D;function A(n,t,e=3){let r="";return S(t[0])&&(r=q(t[0])),j(n,t.map(i=>typeof i=="number"?i:parseFloat(i)),e).map(i=>i+r)}function C(n,t,e=3){let r=!0,i=!0,o=0,s,a=t.length;for(;o<a;o++)s=t[o],r&&(r=typeof s=="number"),i&&(i=S(s));return r?j(n,t,e):i?A(n,t,e):D(n,t)}function tt(n,t={},e=C){let r=M(t),[i,o]=O(r);return[e?.(i,n,r.decimal),o]}function nt(n){return(t,e,r)=>t.map(i=>n(i,e,r))}function $(n,t,e){let[r,i]=t,o=e[0]-r,s=e[1]-i;if(o!==0||s!==0){let a=((n[0]-r)*o+(n[1]-i)*s)/(o*o+s*s);a>1?(r=e[0],i=e[1]):a>0&&(r+=o*a,i+=s*a)}return o=n[0]-r,s=n[1]-i,o*o+s*s}function U(n,t){let e=t*t;if(n.length<3)return n;let r=[n[0]],i=[[0,n.length-1]];for(;i.length>0;){let[o,s]=i.pop(),a=0,l=0;for(let u=o+1;u<s;u++){let c=$(n[u],n[o],n[s]);c>a&&(l=u,a=c)}a>e?(i.push([o,l]),i.push([l,s])):r.push(n[s])}return r.sort((o,s)=>o[0]-s[0])}function T(n,t,e){if(!n)return null;let r=Math.max(e,2);return U(n,t).map(([i,o])=>[F(i,r),F(o,e)])}function z(n,t){if(!n)return[];let e=new Intl.NumberFormat("en-US",{maximumFractionDigits:Math.max(t-2,0)}),r=new Intl.NumberFormat("en-US",{maximumFractionDigits:t}),i=n,o=new Set,s=1/10**t;for(let[u,c]of i.entries()){let[f]=c;if(u===0){f===0&&o.add(c);continue}if(u===i.length-1){let N=i[u-1][0];f===1&&N<=1&&o.add(c);continue}let h=i[u-1][0],x=(i[u+1][0]-h)/2+h;Math.abs(f-x)<s&&o.add(c)}let a=[[i[0]]];for(let u of i.slice(1))u[1]===a.at(-1)[0][1]?a.at(-1).push(u):a.push([u]);return a.map(u=>{let c=r.format(u[0][1]),f=u.map(d=>{let[N]=d,k=c;return o.has(d)||(k+=" "+e.format(N*100)+"%"),k}).join(", ");if(u.length===1)return f;let I=[u[0][0],u.at(-1)[0]].map(d=>e.format(d*100)+"%").join(" "),x=`${c} ${I}`;return x.length>f.length?f:x})}function et(n={}){let t=M(n),[e,r]=O(t),i=m(t.quality??.85,0,1),o=w(1-i,0,.025),s=e.length,a=e.map((u,c)=>[c/(s-1),u]),l=T(a,o,t.decimal);return[z(l,t.decimal).join(", "),r]}var g=(n,[t=1,e=100,r=10,i=0]=[],o)=>{t=m(t,1e-4,1e3),e=m(e,1e-4,1e3),r=m(r,1e-4,1e3),i=m(i,1e-4,1e3);let s=Math.sqrt(e/t),a=r/(2*Math.sqrt(e*t)),l=a<1?s*Math.sqrt(1-a*a):0,u=a<1?(a*s+-i)/l:-i+s,c=o?o*n/1e3:n;return a<1?c=Math.exp(-c*a*s)*(Math.cos(l*c)+u*Math.sin(l*c)):c=(1+u*c)*Math.exp(-c*s),1-c},y=new Map,E=1e5;function R([n,t,e,r]=[]){let i=[n,t,e,r],o=`${i}`;if(y.has(o))return y.get(o);let s=1/6,a=0,l=0;for(;++l<E;){if(Math.abs(1-g(a,i))<.001){let c=a,f=1;for(;++l<E&&(a+=s,!(Math.abs(1-g(a,i))>=.001));)if(f++,f===16){let h=c*1e3;return y.set(o,[h,l]),[h,l]}}a+=s}let u=a*1e3;return y.set(o,[u,l]),[u,l]}function W(n){return(t,e=[],r)=>1-n(1-t,e,r)}function B(n){return function(t,e=[],r){return t<.5?n(t*2,e,r)/2:1-n(t*-2+2,e,r)/2}}function G(n){return function(t,e=[],r){return t<.5?(1-n(1-t*2,e,r))/2:(n(t*2-1,e,r)+1)/2}}var K=g,X=W(g),H=B(g),J=G(g);function P(n,t,e=3){let r=t.length-1,i=m(Math.floor(n*r),0,r-1),o=t[i],s=t[i+1],a=(n-i/r)*r;return F(w(a,o,s),e)}function V(n,t){let e=t.length-1;n=m(n,0,1);let r=Math.round(n*e);return t[r]}var rt=V;function Q(n,t,e=3){let r="";return S(t[0])&&(r=q(t[0])),P(n,t.map(i=>typeof i=="number"?i:parseFloat(i)),e)+r}function Y(n,t,e=3){return t.every(o=>typeof o=="number")?P(n,t,e):t.every(o=>S(o))?Q(n,t,e):V(n,t)}var p={spring:g,"spring-in":K,"spring-out":X,"spring-in-out":H,"spring-out-in":J},L=Object.keys(p);function it(n,t){p={...p,[n]:t},L=Object.keys(p)}function ot(n){p={...p,...n},L=Object.keys(p)}function Z(n){let t=/(\(|\s)([^)]+)\)?/.exec(n.toString());return t?t[2].split(",").map(e=>{let r=parseFloat(e);return Number.isNaN(r)?e.trim():r}):[]}function M(n={}){let t=typeof n=="string"||Array.isArray(n)&&typeof n[0]=="function",{easing:e=[g,1,100,10,0],numPoints:r=38,decimal:i=3,...o}=t?{easing:n}:n;if(typeof e=="string"){let s=p[e.replace(/(\(|\s).+/,"").toLowerCase().trim()],a=Z(e);e=[s,...a]}return{easing:e,numPoints:r,decimal:i,...o}}var b=new Map;function O(n={}){let{easing:t,numPoints:e}=M(n);if(Array.isArray(t)){if(typeof t[0]!="function")throw new Error("[spring-easing] A frame function is required as the first element in the easing array, e.g. [SpringFrame, ...]")}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 "${t}", [spring-easing] doesn't really know what to do with that.`);let[r,...i]=t,[o,s=38]=R(i);e||(e=s);let a=`${i},${e}`;if(b.has(a)){let c=b.get(a);if(c.has(r))return c.get(r)}let l=[];for(let c=0;c<e;c++)l[c]=r(c/(e-1),i,o);let u=b.has(a)?b.get(a):new WeakMap;return u.set(r,[l,o]),b.set(a,u),[l,o]}function _(n,t={},e=Y){let r=M(t),[i,o]=O(r);return[i.map(s=>e(s,n,r.decimal)),o]}export{tt as BatchSpringEasing,et as CSSSpringEasing,B as EaseInOut,W as EaseOut,G as EaseOutIn,y as EasingDurationCache,L as EasingFunctionKeys,p as EasingFunctions,M as EasingOptions,b as FramePtsCache,O as GenerateSpringFrames,E as INFINITE_LOOP_LIMIT,_ as SpringEasing,g as SpringFrame,K as SpringInFrame,H as SpringInOutFrame,X as SpringOutFrame,J as SpringOutInFrame,C as batchInterpolateComplex,j as batchInterpolateNumber,D as batchInterpolateSequence,A as batchInterpolateString,v as batchInterpolateUsingIndex,_ as default,z as getLinearSyntax,T as getOptimizedPoints,R as getSpringDuration,q as getUnit,Y as interpolateComplex,P as interpolateNumber,V as interpolateSequence,Q as interpolateString,rt as interpolateUsingIndex,S as isNumberLike,m as limit,Z as parseEasingParameters,U as ramerDouglasPeucker,it as registerEasingFunction,ot as registerEasingFunctions,w as scale,$ as squaredSegmentDistance,nt as toAnimationFrames,F as toFixed};
/*!
* Based off of https://github.com/jakearchibald/linear-easing-generator
*
* Changes:
* - Added comments and docs top explain logic
* - Switched to iterative approach for the `ramerDouglasPeucker` algorithim
* - Renamed functions, parameters and variables to improve readability and to better match a library usecase
*
* Copyright 2023 Jake Archibald [@jakearchibald](https://github.com/jakearchibald)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*!
* 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