UNPKG

motion-dom

Version:
3 lines (2 loc) • 21.7 kB
const t=(t,e,n)=>n>e?e:n<t?t:n,e={},n=t=>"object"==typeof t&&null!==t;class s{constructor(){this.subscriptions=[]}add(t){var e,n;return e=this.subscriptions,n=t,-1===e.indexOf(n)&&e.push(n),()=>this.remove(t)}remove(t){!function(t,e){const n=t.indexOf(e);n>-1&&t.splice(n,1)}(this.subscriptions,t)}notify(t,e,n){const s=this.subscriptions.length;if(s)if(1===s)this.subscriptions[0](t,e,n);else for(let r=0;r<s;r++){const s=this.subscriptions[r];s&&s(t,e,n)}}getSize(){return this.subscriptions.length}clear(){this.subscriptions.length=0}}const r=t=>1e3*t,i=t=>t/1e3,a=2e4;function o(t,e=50,n=2e4,s){let r=0,i=t.next(r);for(;!i.done&&r<n;)r+=e,i=t.next(r);return r>=n?1/0:r}const c=t=>t<0?1/Math.max(1+t,.05):Math.max(1-t,.05);function h(t,e){if(!(t>1))return 1;const n=Math.sqrt(t*t-1),s=t-n,r=t+n,i=2*n*Math.exp(-e)*(1+e);return l(t=>r*Math.exp(-s*t)-s*Math.exp(-r*t)-i,t=>Math.exp(-r*t)-Math.exp(-s*t),e)/e}function u(t,e){return t*Math.sqrt(1-e*e)}const d=12;function l(t,e,n){let s=n;for(let n=1;n<d;n++)s-=t(s)/e(s);return s}const p=.001;const f=(t,e)=>(e?t>=0:t>0)&&t<1/0;function g(t,e){if(f(t,e))return t}function m(e){const n=g(e.stiffness),s=g(e.damping,!0),a=g(e.mass),o={...e,stiffness:n??100,damping:s??10,mass:a??1,isResolvedFromDuration:!1,isTimeDefined:void 0===(n??s??a)&&(void 0!==e.duration||void 0!==e.bounce)};if(o.isTimeDefined){if(e.visualDuration){const t=c(e.bounce||0),n=2*Math.PI/(1.2*e.visualDuration)*h(t,2*Math.PI/1.2);o.stiffness=n*n,o.damping=2*t*Math.sqrt(o.stiffness)}else Object.assign(o,function({duration:e=800,bounce:n=.3}){let s,a;const o=c(n);e=t(.01,10,i(e)),o<1?(s=t=>{const n=t*o,s=n*e,r=u(t,o),i=Math.exp(-s);return p-n/r*i},a=t=>{const n=t*o*e,r=o*o*t*t*e,i=Math.exp(-n),a=u(t*t,o);return(-s(t)+p>0?-1:1)*-r*i/a}):(s=t=>Math.exp(-t*e)*(t*e+1)-.001,a=t=>Math.exp(-t*e)*(e*e*-t));const d=l(s,a,5/e),f=d*h(o,d*e),g=f*f;return{stiffness:g,damping:2*o*Math.sqrt(g),duration:r(e)}}(o)),o.isResolvedFromDuration=!0;f(o.stiffness)&&f(o.damping,!0)||(o.stiffness=100,o.damping=10)}return o}function v(t=.3,e=.3){const n="object"!=typeof t?{visualDuration:t,keyframes:[0,1],bounce:e}:t,s=n.keyframes[0],c=n.keyframes[n.keyframes.length-1],h={done:!1,value:s},{stiffness:d,damping:l,mass:p,duration:f,isResolvedFromDuration:g,isTimeDefined:v}=m({...n}),y=t=>g?0:-i(t),b=l/(2*Math.sqrt(d*p)),M=i(Math.sqrt(d/p)),x=b*M,w={target:c,delta:c-s,velocity:y(n.velocity)||0,restSpeed:0,restDelta:0},k=()=>{const t=Math.abs(w.delta)<5;w.restSpeed=n.restSpeed||(t?.01:2),w.restDelta=n.restDelta||(t?.005:.5)};let S,A,P;if(k(),b<1){const t=u(M,b),e={A:0,sinC:0,cosC:0,t:-1,env:0,sin:0,cos:0};P=()=>{e.A=(w.velocity+x*w.delta)/t,e.sinC=x*e.A+w.delta*t,e.cosC=x*w.delta-e.A*t};const n=n=>{n!==e.t&&(e.t=n,e.env=Math.exp(-x*n),e.sin=Math.sin(t*n),e.cos=Math.cos(t*n))};S=t=>(n(t),w.target-e.env*(e.A*e.sin+w.delta*e.cos)),A=t=>(n(t),e.env*(e.sinC*e.sin+e.cosC*e.cos))}else if(1===b){S=t=>w.target-Math.exp(-M*t)*(w.delta+(w.velocity+M*w.delta)*t);const t={C:0};P=()=>{t.C=w.velocity+M*w.delta},A=e=>Math.exp(-M*e)*(M*t.C*e-w.velocity)}else{const t=M*Math.sqrt(b*b-1),e=x-t,n=x+t,s=t<1||v?1/0:300/t,r=(t,e)=>Math.exp(e>s?(x-t)*s-x*e:-t*e),i={S:0,F:0};P=()=>{const e=(w.velocity+x*w.delta)/t;i.S=(w.delta+e)/2,i.F=(w.delta-e)/2},S=t=>w.target-i.S*r(e,t)-i.F*r(n,t),A=t=>e*i.S*r(e,t)+n*i.F*r(n,t)}P();const R=g&&f||null,C={calculatedDuration:R,retarget:(t,e)=>{w.target=t[t.length-1],w.delta=w.target-t[0],w.velocity=y(e),n.restSpeed&&n.restDelta||k(),C.calculatedDuration=R,h.done=!1,P()},velocity:t=>r(A(t)),next:t=>{const e=S(t);if(g)h.done=t>=f;else{const n=r(A(t));h.done=Math.abs(n)<=w.restSpeed&&Math.abs(w.target-e)<=w.restDelta}return h.value=h.done?w.target:e,h},toString:()=>{const t=Math.min(o(C),a),e=((t,e,n=10)=>{let s="";const r=Math.max(Math.round(e/n),2);for(let e=0;e<r;e++)s+=Math.round(1e4*t(e/(r-1)))/1e4+", ";return`linear(${s.substring(0,s.length-2)})`})(e=>C.next(t*e).value,t,30);return t+"ms "+e}};return C}const y=Object.assign(v,{applyToOptions:t=>{const e=function(t,e=100,n){const{keyframes:s,velocity:r}=t,c=s&&s[s.length-1]-s[0],h=n({...t,velocity:r&&c?r*e/c:r,keyframes:[0,e]}),u=Math.min(o(h),a);return{type:"keyframes",ease:t=>h.next(u*t).value/e,duration:i(u)}}(t,100,v);return t.ease=e.ease,t.duration=r(e.duration),t.type="keyframes",t}}),b=new WeakMap,M=t=>t.startsWith("--"),x=t=>180*t/Math.PI,w=t=>{const e=x(Math.atan2(t[1],t[0]));return S(e)},k={x:4,y:5,translateX:4,translateY:5,scaleX:0,scaleY:3,scaleZ:()=>1,scale:t=>(Math.abs(t[0])+Math.abs(t[3]))/2,rotate:w,rotateZ:w,skewX:t=>x(Math.atan(t[1])),skewY:t=>x(Math.atan(t[2])),skew:t=>(Math.abs(t[1])+Math.abs(t[2]))/2},S=t=>((t%=360)<0&&(t+=360),t),A=t=>Math.hypot(t[0],t[1]),P=t=>Math.hypot(t[4],t[5]),R={x:12,y:13,z:14,translateX:12,translateY:13,translateZ:14,scaleX:A,scaleY:P,scaleZ:t=>Math.hypot(t[8],t[9],t[10]),scale:t=>(A(t)+P(t))/2,rotateX:t=>S(x(Math.atan2(t[6],t[5]))),rotateY:t=>S(x(Math.atan2(-t[2],t[0]))),rotateZ:w,rotate:w,skewX:t=>x(Math.atan(t[4])),skewY:t=>x(Math.atan(t[1])),skew:t=>(Math.abs(t[1])+Math.abs(t[4]))/2};function C(t){return t.includes("scale")?1:0}const D=(t,e)=>{const{transform:n="none"}=getComputedStyle(t);return function(t,e){if(!t||"none"===t)return C(e);const n=t.match(/^matrix(3d)?\(([-\d.e\s,]+)\)$/u);if(!n)return C(e);const s=(n[1]?R:k)[e],r=n[2].split(",").map(t=>parseFloat(t));return"function"==typeof s?s(r):r[s]}(n,e)},O=["transformPerspective","x","y","z","translateX","translateY","translateZ","scale","scaleX","scaleY","scaleZ","rotate","rotateX","rotateY","rotateZ","skew","skewX","skewY"],F=(()=>new Set([...O,"pathRotation"]))();function Y(t){return n(t)&&"offsetHeight"in t&&!("ownerSVGElement"in t)}function I(t){return n(t)&&"ownerSVGElement"in t}const V=["setup","read","resolveKeyframes","preUpdate","update","preRender","render","postRender"];function X(t,n,s={delta:0,timestamp:0,isProcessing:!1}){let r=!1,i=!0;const a=()=>r=!0,o=V.reduce((t,e)=>(t[e]=function(t){let e,n=new Set,s=new Set,r=!1,i=!1;const a=new Set;let o={delta:0,timestamp:0,isProcessing:!1};function c(n){a.has(n)&&(s.add(n),t()),(e=n)(o)}const h={schedule:(t,i=!1,o=!1)=>{const c=o&&r,h=c?n:s;return i&&a.add(t),c&&t!==e&&h.delete(t),h.add(t),t},cancel:t=>{s.delete(t),a.delete(t)},process:t=>{if(o=t,r)return void(i=!0);r=!0;const a=n;n=s,s=a,n.forEach(c),n.clear(),e=void 0,r=!1,i&&(i=!1,h.process(t))}};return h}(a),t),{}),{setup:c,read:h,resolveKeyframes:u,preUpdate:d,update:l,preRender:p,render:f,postRender:g}=o,m=()=>{const a=e.useManualTiming,o=a?s.timestamp:performance.now();r=!1,a||(s.delta=i?1e3/60:Math.max(Math.min(o-s.timestamp,40),1)),s.timestamp=o,s.isProcessing=!0,c.process(s),h.process(s),u.process(s),d.process(s),l.process(s),p.process(s),f.process(s),g.process(s),s.isProcessing=!1,r&&n&&(i=!1,t(m))};return{schedule:V.reduce((e,n)=>{const a=o[n];return e[n]=(e,n=!1,o=!1)=>(r||(r=!0,i=!0,s.isProcessing||t(m)),a.schedule(e,n,o)),e},{}),cancel:t=>{for(let e=0;e<V.length;e++)o[V[e]].cancel(t)},state:s,steps:o}}const E={delta:0,timestamp:0,isProcessing:!1},{schedule:W,cancel:T}=X("undefined"!=typeof requestAnimationFrame?requestAnimationFrame:t=>t,!0,E);let B;function Z(){B=void 0}const j={now:()=>(void 0===B&&j.set(E.isProcessing||e.useManualTiming?E.timestamp:performance.now()),B),set:t=>{B=t,queueMicrotask(Z)}};class L{constructor(t,e={}){this.canTrackVelocity=null,this.events={},this.updateAndNotify=t=>{const e=j.now();if(this.updatedAt!==e&&this.setPrevFrameValue(),this.prev=this.current,this.setCurrent(t),this.current!==this.prev&&(this.dirty(),this.dependents))for(const t of this.dependents)t.dirty()},this.hasAnimated=!1,this.setCurrent(t),this.owner=e.owner}setCurrent(t){var e;this.current=t,this.updatedAt=j.now(),null===this.canTrackVelocity&&void 0!==t&&(this.canTrackVelocity=(e=this.current,!isNaN(parseFloat(e))))}setPrevFrameValue(t=this.current){this.prevFrameValue=t,this.prevUpdatedAt=this.updatedAt}onChange(t){return this.on("change",t)}on(t,e){var n;return"change"===t?this.onChangeSubscribe(e):((n=this.events)[t]||(n[t]=new s)).add(e)}onChangeSubscribe(t){const{events:e}=this;return e.change||this.changeSubscriber?(e.change||(e.change=new s,e.change.add(this.changeSubscriber),this.changeSubscriber=void 0),e.change.add(t)):this.changeSubscriber=t,()=>{this.changeSubscriber===t?this.changeSubscriber=void 0:e.change?.remove(t),this.stopIfUnobserved()}}stopIfUnobserved(){W.read(()=>{this.changeSubscriber||this.events.change?.getSize()||this.stop()})}clearListeners(){this.changeSubscriber=void 0;for(const t in this.events)this.events[t].clear()}attach(t,e){this.passiveEffect=t,this.stopPassiveEffect=e}set(t){this.passiveEffect?this.passiveEffect(t,this.updateAndNotify):this.updateAndNotify(t)}setWithVelocity(t,e,n){this.set(e),this.prev=void 0,this.prevFrameValue=t,this.prevUpdatedAt=this.updatedAt-n}jump(t,e=!0){e&&(this.updatedAt=j.now(),this.stop()),this.updateAndNotify(t),this.prev=t,this.prevUpdatedAt=this.prevFrameValue=void 0,this.stopPassiveEffect?.()}dirty(){const{current:t,changeSubscriber:e}=this;e?e(t):this.events.change?.notify(t)}addDependent(t){(this.dependents||(this.dependents=new Set)).add(t)}removeDependent(t){this.dependents?.delete(t)}get(){const t=this.animation?.liveValue?.();return void 0===t?this.current:t}getCurrent(){return this.current}getPrevious(){return this.prev}getVelocity(){const t=j.now();if(!this.canTrackVelocity||void 0===this.prevFrameValue||t-this.updatedAt>30)return 0;const e=Math.min(this.updatedAt-this.prevUpdatedAt,30);return n=parseFloat(this.current)-parseFloat(this.prevFrameValue),(s=e)?n*(1e3/s):0;var n,s}start(t){return new Promise(e=>this.run(t,e))}run(t,e){this.stop(),this.hasAnimated=!0;let n,s=!1;n=t(()=>{s=!0,this.events.animationComplete?.notify(),this.animation===n&&(this.animation=void 0),e?.()}),s||(this.animation=n),this.events.animationStart?.notify()}stop(){this.animation&&(this.animation.stop(),this.events.animationCancel?.notify()),this.animation=void 0}isAnimating(){return!!this.animation}destroy(){this.dependents?.clear(),this.events.destroy?.notify(),this.clearListeners(),this.stop(),this.stopPassiveEffect?.()}}const z={test:t=>"number"==typeof t,parse:parseFloat,transform:t=>t},q={...z,transform:e=>t(0,1,e)},$={...z,default:1},U={...z,transform:Math.round},K=t=>({test:e=>"string"==typeof e&&e.endsWith(t)&&1===e.split(" ").length,parse:parseFloat,transform:e=>`${e}${t}`}),N=K("deg"),H=K("%"),G=K("px"),J=(()=>({...H,parse:t=>H.parse(t)/100,transform:t=>H.transform(100*t)}))(),Q={rotate:N,pathRotation:N,rotateX:N,rotateY:N,rotateZ:N,scale:$,scaleX:$,scaleY:$,scaleZ:$,skew:N,skewX:N,skewY:N,distance:G,translateX:G,translateY:G,translateZ:G,x:G,y:G,z:G,perspective:G,transformPerspective:G,opacity:q,originX:J,originY:J,originZ:G},_={borderWidth:G,borderTopWidth:G,borderRightWidth:G,borderBottomWidth:G,borderLeftWidth:G,borderRadius:G,borderTopLeftRadius:G,borderTopRightRadius:G,borderBottomRightRadius:G,borderBottomLeftRadius:G,width:G,maxWidth:G,height:G,maxHeight:G,top:G,right:G,bottom:G,left:G,inset:G,insetBlock:G,insetBlockStart:G,insetBlockEnd:G,insetInline:G,insetInlineStart:G,insetInlineEnd:G,padding:G,paddingTop:G,paddingRight:G,paddingBottom:G,paddingLeft:G,paddingBlock:G,paddingBlockStart:G,paddingBlockEnd:G,paddingInline:G,paddingInlineStart:G,paddingInlineEnd:G,margin:G,marginTop:G,marginRight:G,marginBottom:G,marginLeft:G,marginBlock:G,marginBlockStart:G,marginBlockEnd:G,marginInline:G,marginInlineStart:G,marginInlineEnd:G,fontSize:G,backgroundPositionX:G,backgroundPositionY:G,...Q,zIndex:U,fillOpacity:q,strokeOpacity:q,numOctaves:U},tt=(t,e)=>e&&"number"==typeof t?e.transform(t):t,et={};class nt{constructor(t=W.render){this.step=t,this.values=new Map,this.pending=[],this.numPending=0,this.flush=()=>{const{pending:t,numPending:e}=this;this.numPending=0;for(let n=0;n<e;n++)t[n]()}}set(t,e,n,s){if(this.values.get(t)?.onRemove(),s)for(const t of this.values.values())t.value===s&&(n=t.render);const r=()=>n&&this.schedule(n);void 0!==e.get()&&r();const i=e.on("change",r),a=()=>{i(),n&&!s&&this.cancel(n),this.values.get(t)?.onRemove===a&&this.values.delete(t)};return this.values.set(t,{value:e,render:s?void 0:n,onRemove:a}),a}get(t){return this.values.get(t)?.value}release(){const t=new Map;return this.values.forEach((e,n)=>{t.set(n,e.value),e.onRemove()}),this.transformKeys=this.transformValues=void 0,t}schedule(t){const{pending:e,numPending:n}=this;for(let s=0;s<n;s++)if(e[s]===t)return;n||this.step(this.flush),e[this.numPending++]=t}cancel(t){const{pending:e}=this;for(let n=0;n<this.numPending;n++)if(e[n]===t)return void(e[n]=e[--this.numPending])}}function st(t,{step:e,...n}={}){const s=new WeakMap;return Object.assign((n,r)=>{const i=s.get(n)??new nt(e);s.set(n,i);const a=[];for(const e in r){const s=r[e],o=t(n,i,e,s);a.push(o)}return()=>{for(const t of a)t()}},n,{get:(t,e)=>s.get(t)?.get(e),flush:t=>s.get(t)?.flush(),state:t=>s.get(t)})}const rt={x:"translateX",y:"translateY",z:"translateZ",transformPerspective:"perspective"},it={},at=(t,e,n=tt(e,Q[t]))=>(it[t]||(it[t]=(rt[t]||t)+"("))+n+")",ot=(t,e)=>("number"==typeof e?e:parseFloat(e))===(t.startsWith("scale")?1:0);const ct=new Set(["originX","originY","originZ"]),ht=(t,e)=>tt(t.get(e)?.get(),_[e]),ut=(t,e,n,s)=>{let r,i;if(F.has(n)){if("pathRotation"!==n){const t=e.transformKeys??(e.transformKeys=[]);(e.transformValues??(e.transformValues={}))[n]=s,t.includes(n)||(t.push(n),t.sort((t,e)=>O.indexOf(t)-O.indexOf(e)))}e.get("transform")||(Y(t)||e.get("transformBox")||ut(t,e,"transformBox",new L("fill-box")),e.set("transform",new L("none"),()=>{b.get(t)?.check(),t.style.transform=function(t){let e="";const{transformKeys:n=[],transformValues:s={}}=t;for(let t=0;t<n.length;t++){const r=n[t],i=s[r].getCurrent();void 0!==i&&(ot(r,i)||(e+=(e&&" ")+at(r,i)))}const r=t.get("pathRotation")?.get();return r&&(e+=(e&&" ")+at("rotate",r)),e||"none"}(e)})),i=e.get("transform")}else ct.has(n)?(e.get("transformOrigin")||e.set("transformOrigin",new L(""),()=>{const n=ht(e,"originX")??"50%",s=ht(e,"originY")??"50%",r=ht(e,"originZ")??0;t.style.transformOrigin=`${n} ${s} ${r}`}),i=e.get("transformOrigin")):r=M(n)?()=>{t.style.setProperty(n,s.getCurrent())}:()=>{t.style[n]=tt(s.getCurrent(),_[n])};return e.set(n,s,r,i)},dt=(t,e)=>{if(F.has(e))return D(t,e);const n=getComputedStyle(t),s=M(e)?n.getPropertyValue(e):n[e];return"string"==typeof s&&s.trim()||0},lt=st(ut,{test:t=>Y(t)||I(t),read:dt});function pt(t){return t.replace(/([A-Z])/g,t=>`-${t.toLowerCase()}`)}const ft=["transform","opacity","clipPath","filter","offsetDistance","offsetPath","offsetRotate","offsetAnchor"];const gt=(t,e,n,s,r=n)=>{const i=function(t,e){if(!(e in t))return!1;const n=Object.getOwnPropertyDescriptor(Object.getPrototypeOf(t),e)||Object.getOwnPropertyDescriptor(t,e);return n&&"function"==typeof n.set}(t,r);i||!r.startsWith("data")&&!r.startsWith("aria")||(r=pt(r));const a=_[n]||_[r],o=i?()=>{t[r]=tt(s.get(),_[n])}:()=>{const e=tt(s.get(),a);null==e?t.removeAttribute(r):t.setAttribute(r,String(e))};return e.set(n,s,o)};const mt=st((t,e,n,s)=>{if(n.startsWith("path")&&!F.has(n))return function(t,e,n,s){return W.render(()=>t.setAttribute("pathLength","1")),"pathOffset"===n?e.set(n,s,()=>{const e=s.get();t.setAttribute("stroke-dashoffset",""+-e)}):(e.get("stroke-dasharray")||e.set("stroke-dasharray",new L("1 1"),()=>{const n=e.get("pathLength")?.get()??1,s=e.get("pathSpacing")?.get();t.setAttribute("stroke-dasharray",`${n} ${s??1-Number(n)}`)}),e.set(n,s,void 0,e.get("stroke-dasharray")))}(t,e,n,s);if(n.startsWith("attr"))return gt(t,e,n,s,vt(n));if("d"===n)return e.set(n,s,()=>{const e=s.get()?.replace(/path\(|["')]/g,"");void 0===e?t.removeAttribute(n):t.setAttribute(n,e)});return(F.has(n)||ct.has(n)||M(n)||n in t.style?ut:gt)(t,e,n,s)},{test:I,read:(t,e)=>F.has(e)?_[e]?.default||0:M(e)||ft.includes(e)?dt(t,e)||t.getAttribute(pt(e))||0:(e=vt(e),t.getAttribute(pt(e))??t.getAttribute(e)??void 0)});function vt(t){return t.replace(/^attr([A-Z])/,(t,e)=>e.toLowerCase())}const yt=new WeakMap,bt=(t,e,n)=>t+(e-t)*n,Mt={x:!1,y:!1};function xt(t,{min:e,max:n},s){return void 0!==e&&t<e?t=s?bt(e,t,s.min):Math.max(t,e):void 0!==n&&t>n&&(t=s?bt(n,t,s.max):Math.min(t,n)),t}const wt=new WeakMap,kt=(t,e)=>{const n=t.get();"string"==typeof n&&t.set(parseFloat(n)*(n.endsWith("%")?e/100:1))},St=t=>"object"==typeof t?t:{},At=(t=.35)=>{const e=t=>!0===t?.35:+(t||0);return"object"==typeof t?{min:e(t.min),max:e(t.max)}:{min:e(t),max:e(t)}},Pt=({pan:t,origin:e})=>!1!==t||!!e,Rt=(e,{min:n=-1/0,max:s=1/0})=>t(n,s,e),Ct=(t,e,n)=>{const s=Rt(t,St(e)),r=n[t<s?"min":"max"];return r?s+(t-s)/r:t};function Dt(e,n,s,r){const i=new Map,a={x:0,y:0,l:0,a:0},o={...a},c={...a};let h,u,d,l,p=0,f=0,g=0,m=0,v=0,b=0,M=!1,x=0,w={},k=1,S=0,A=()=>{},P={};const R=()=>P=s()||P,C=()=>({point:{x:a.x,y:a.y},offset:{x:a.x-o.x,y:a.y-o.y},scale:Math.exp(a.l-o.l),rotate:a.a-o.a}),D=()=>{const t=e.getBoundingClientRect(),{left:s,top:r}=t,{scale:i,rotate:a,elastic:o}=R(),[c,h,u,l]=d=n(),y=At(o);!1!==i&&c.stop(),!1!==a&&h.stop(),k=Ct(c.get(),i,y),S=Ct(h.get(),a,y),g=e.offsetWidth??t.width,m=e.offsetHeight??t.height,Pt(P)&&(u.stop(),l.stop(),kt(u,g),kt(l,m)),v=u.get(),b=l.get(),p=s+t.width/2-v,f=r+t.height/2-b},O=(t,e,n,s,r)=>{const{origin:i}=P;if("object"!=typeof i)return t;const a=n*Math.PI/180,o=(i.x-.5)*g*e,c=(i.y-.5)*m*e;return{x:p+s+Math.cos(a)*o-Math.sin(a)*c,y:f+r+Math.sin(a)*o+Math.cos(a)*c}},F=()=>wt.get(yt.get(e)),Y=()=>{const{scale:t,rotate:e,snapToOrigin:n,transition:s}=P,[r,i,o,c]=d,h=r.get(),u=i.get(),l=o.get(),g=c.get(),{snap:m}=St(e);let v=Rt(h,St(t)),b=Rt(m?Math.round((u+.2*i.getVelocity())/m)*m:u,St(e));n&&(v=1,b=360*Math.round(u/360));const M=Pt(P)&&!F()?.isDragging,x=O(a,h,u,l,g),w=p+l-x.x,k=f+g-x.y;if(!h||v===h&&b===u&&(!n||!M||!l&&!g))return;const S=y({keyframes:[0,1],stiffness:200,damping:40,...s}),R=j.now(),C=()=>{const{value:s,done:a}=S.next(j.now()-R);a&&A();const[d,p,f,m]=(t=>{const e=bt(h,v,t),n=bt(u,b,t),s=(n-u)*Math.PI/180,r=Math.cos(s)*e/h,i=Math.sin(s)*e/h;return[e,n,l+r*w-i*k-w,g+i*w+r*k-k]})(s);!1!==t&&r.set(d),!1!==e&&i.set(p),M&&!F()?.isDragging&&(o.set(n?bt(l,0,s):f),c.set(n?bt(g,0,s):m))};A=()=>T(C),W.update(C,!0)},I=()=>{if(!d||!M)return;M=!1;const{rotate:e,scale:n,pan:s,elastic:r}=R(),[i,o,h,l]=d,g=At(r),m=i.get(),y=F(),x=y?.isDragging;x||(v=h.get(),b=l.get());const A=O(c,m,o.get(),v,b);let D=0,Y=1;const I=(t,e)=>{const n=St(e),s=Rt(t,n);return g[t<s?"min":"max"]?[xt(t,n,g),t]:[s,s]};if(!1!==n&&m){const[e,s]=I(t(1e-9,1e9,k*Math.exp(a.l-c.l)),n);k=s,Y=e/m,i.set(e)}if(!1!==e){const[t,n]=I(S+a.a-c.a,e);S=n,D=t-o.get(),D&&o.set(t)}if(Pt(P)){h.stop(),l.stop();const t=D*Math.PI/180,e=Math.cos(t)*Y,n=Math.sin(t)*Y,r=A.x-p-v,i=A.y-f-b,o=!1!==s?a.x-c.x:0,u=!1!==s?a.y-c.y:0;if(v+=r+o-(e*r-n*i),b+=i+u-(n*r+e*i),x){const t=y.panSession.history;t.splice(0,t.length-1),t[0].timestamp=-1/0,y.originPoint.x=v,y.originPoint.y=b}h.set(v),l.set(b)}Object.assign(c,a),w.onPinch?.(u,C())},V=()=>{if(!s())return E(u);I(),"wheel"===h&&j.now()-x>150&&E(u)},X=(t,n)=>{if(!s())return;A(),h=n,u=t,M=!1,d=void 0,w={},Object.assign(o,a),Object.assign(c,a),W.read(D),W.preRender(V,!0);const i=r(e,t,C());w="function"==typeof i?{onComplete:i}:i||{}},E=t=>{h&&(T(D),T(V),s()&&(I(),d&&Y()),h=void 0,w.onComplete?.(t,C()))},B=t=>{u=t,M=!0},Z=()=>{const[t,e]=i.values(),n=180*Math.atan2(e.y-t.y,e.x-t.x)/Math.PI-a.a;a.x=(t.x+e.x)/2,a.y=(t.y+e.y)/2,a.l=Math.log(Math.hypot(e.x-t.x,e.y-t.y)||1),a.a+=n-360*Math.round(n/360)},L=t=>i.set(t.pointerId,{x:t.clientX,y:t.clientY}),z=t=>{i.has(t.pointerId)&&(L(t),"pointer"===h&&(Z(),B(t)))},q=t=>{i.delete(t.pointerId)&&("pointer"===h&&E(t),i.size||(K(),U()))},$=t=>{h||i.size>1||i.has(t.pointerId)||"mouse"===t.pointerType||(i.size||K(!0),L(t),2===i.size&&(Z(),X(t,"pointer"),!h||l||wt.get(yt.get(e))||(l=Mt.x||Mt.y?null:(Mt.x=Mt.y=!0,()=>{Mt.x=Mt.y=!1}))))},U=()=>{const t=l;l=void 0,t&&W.postRender(t)},K=t=>{const e=t?"addEventListener":"removeEventListener";window[e]("pointerdown",$),window[e]("pointermove",z),window[e]("pointerup",q),window[e]("pointercancel",q)},N=t=>{if(!(i.size||h&&"gesture"!==h)){const{scale:e,rotation:n,clientX:s,clientY:r}=t;a.x=s,a.y=r,a.l=Math.log(e),a.a=n,h?(B(t),"gestureend"===t.type&&E(t)):"gesturestart"===t.type&&X(t,"gesture")}h&&t.preventDefault()},H={pointerdown:$,wheel:e=>{!e.ctrlKey||h&&"wheel"!==h||(a.x=e.clientX,a.y=e.clientY,(h||(a.l=0,X(e,"wheel"),h))&&(e.preventDefault(),a.l-=t(-50,50,e.deltaY*(e.deltaMode?16:1))/100,x=j.now(),B(e)))},gesturestart:N,gesturechange:N,gestureend:N};for(const t in H)e.addEventListener(t,H[t],{passive:!1});return()=>{for(const t in H)e.removeEventListener(t,H[t]);K(),E(u),A(),i.clear(),U()}}const Ot=["scale","rotate","x","y"],Ft=t=>{const e=yt.get(t),n=I(t)?mt:lt;return Ot.map(s=>{if(e)return e.getValue(s,"scale"===s?1:0);let r=n.get(t,s);var i,a;return r||(i=dt(t,s),r=new L(i,a),n(t,{[s]:r})),r})};function Yt(t,e,n={}){let s=()=>{};"function"==typeof e?s=e:e&&(n=e);const r=et.current;if(r){const t=r.bind(s);s=(e,n)=>{const s=t(e,n);if("object"!=typeof s)return s;const i={};for(const t in s){const e=t,n=s[e];i[e]=n&&r.bind(n)}return i}}const i=function(t,e,n){if(null==t)return[];if(t instanceof EventTarget)return[t];if("string"==typeof t){const s=e?e.current:et.current?.root||document,r=n?.[t]??s.querySelectorAll(t);return r?Array.from(r):[]}return Array.from(t).filter(t=>null!=t)}(t).map(t=>{r?.track(t);const{style:e}=t,i=e.touchAction;e.touchAction="none";const a=Dt(t,()=>Ft(t),()=>n,s);return()=>{a(),e.touchAction=i}});return a=()=>i.forEach(t=>t()),et.current?.add(a),a;var a}export{Dt as addPinchListeners,Yt as pinch}; //# sourceMappingURL=size-rollup-pinch.js.map