1 line
8.6 KiB
JavaScript
1 line
8.6 KiB
JavaScript
!function(){"use strict";!function(){class t{constructor(t){this.id=-1,this.nativePointer=t,this.pageX=t.pageX,this.pageY=t.pageY,this.clientX=t.clientX,this.clientY=t.clientY,self.Touch&&t instanceof Touch?this.id=t.identifier:e(t)&&(this.id=t.pointerId)}getCoalesced(){return"getCoalescedEvents"in this.nativePointer?this.nativePointer.getCoalescedEvents().map(e=>new t(e)):[this]}}const e=t=>self.PointerEvent&&t instanceof PointerEvent,i=()=>{};class n{constructor(t,e){this._element=t,this.startPointers=[],this.currentPointers=[];const{start:n=(()=>!0),move:s=i,end:r=i}=e;this._startCallback=n,this._moveCallback=s,this._endCallback=r,this._pointerStart=this._pointerStart.bind(this),this._touchStart=this._touchStart.bind(this),this._move=this._move.bind(this),this._triggerPointerEnd=this._triggerPointerEnd.bind(this),this._pointerEnd=this._pointerEnd.bind(this),this._touchEnd=this._touchEnd.bind(this),self.PointerEvent?this._element.addEventListener("pointerdown",this._pointerStart):(this._element.addEventListener("mousedown",this._pointerStart),this._element.addEventListener("touchstart",this._touchStart),this._element.addEventListener("touchmove",this._move),this._element.addEventListener("touchend",this._touchEnd))}_triggerPointerStart(t,e){return!!this._startCallback(t,e)&&(this.currentPointers.push(t),this.startPointers.push(t),!0)}_pointerStart(i){0===i.button&&this._triggerPointerStart(new t(i),i)&&(e(i)?(this._element.setPointerCapture(i.pointerId),this._element.addEventListener("pointermove",this._move),this._element.addEventListener("pointerup",this._pointerEnd)):(window.addEventListener("mousemove",this._move),window.addEventListener("mouseup",this._pointerEnd)))}_touchStart(e){for(const i of Array.from(e.changedTouches))this._triggerPointerStart(new t(i),e)}_move(e){const i=this.currentPointers.slice(),n="changedTouches"in e?Array.from(e.changedTouches).map(e=>new t(e)):[new t(e)],s=[];for(const t of n){const e=this.currentPointers.findIndex(e=>e.id===t.id);-1!==e&&(s.push(t),this.currentPointers[e]=t)}0!==s.length&&this._moveCallback(i,s,e)}_triggerPointerEnd(t,e){const i=this.currentPointers.findIndex(e=>e.id===t.id);return-1!==i&&(this.currentPointers.splice(i,1),this.startPointers.splice(i,1),this._endCallback(t,e),!0)}_pointerEnd(i){if(this._triggerPointerEnd(new t(i),i))if(e(i)){if(this.currentPointers.length)return;this._element.removeEventListener("pointermove",this._move),this._element.removeEventListener("pointerup",this._pointerEnd)}else window.removeEventListener("mousemove",this._move),window.removeEventListener("mouseup",this._pointerEnd)}_touchEnd(e){for(const i of Array.from(e.changedTouches))this._triggerPointerEnd(new t(i),e)}}const s="min-scale",r="allow-pan-min-scale",o="reset-to-min-scale-limit",a="reach-min-scale-strategy",h="stop-propagate-handled",l="none";function c(t,e){return e?Math.sqrt((e.clientX-t.clientX)**2+(e.clientY-t.clientY)**2):0}function g(t,e){return e?{clientX:(t.clientX+e.clientX)/2,clientY:(t.clientY+e.clientY)/2}:t}function d(t,e){return"number"==typeof t?t:t.trimRight().endsWith("%")?e*parseFloat(t)/100:parseFloat(t)}let u;function p(){return u||(u=document.createElementNS("http://www.w3.org/2000/svg","svg"))}function m(){return p().createSVGMatrix()}function _(){return p().createSVGPoint()}const f=.01,E=-1,v=-1,P=0,w=(t,e)=>Math.round(100*t)-Math.round(100*e);class b extends HTMLElement{constructor(){super(),this._transform=m(),new MutationObserver(()=>this._stageElChange()).observe(this,{childList:!0});const t=new n(this,{start:(e,i)=>{if(2===t.currentPointers.length||!this._positioningEl)return!1;const n=t.currentPointers.length+1===1;return!(n&&!this._allowPan())&&(this._maybeStopPropagate(i),n||this._maybeEmitCancel([e,...t.currentPointers])),i.preventDefault(),!0},move:(e,i,n)=>{this._onPointerMove(e,t.currentPointers,n)},end:(e,i)=>{this._onPointerEnd(e,t.currentPointers,i)}});this.addEventListener("wheel",t=>this._onWheel(t)),this.addEventListener("click",e=>this._onClick(e,t))}static get observedAttributes(){return[s]}attributeChangedCallback(t,e,i){t===s&&this.scale<this.minScale&&this.setTransform({scale:this.minScale})}get minScale(){const t=this.getAttribute(s);if(!t)return f;const e=parseFloat(t);return Number.isFinite(e)?Math.max(f,e):f}set minScale(t){this.setAttribute(s,String(t))}get reachMinScaleStrategy(){return this.getAttribute(a)||l}set reachMinScaleStrategy(t){this.setAttribute(a,t)}get allowPanMinScale(){const t=this.getAttribute(r);if(!t)return E;const e=parseFloat(t);return Number.isFinite(e)?Math.max(E,e):E}set allowPanMinScale(t){this.setAttribute(r,String(t))}get resetToMinScaleLimit(){const t=this.getAttribute(o);if(!t)return v;const e=parseFloat(t);return Number.isFinite(e)?Math.max(v,e):v}set resetToMinScaleLimit(t){this.setAttribute(o,String(t))}get stopPropagateHandled(){return this.hasAttribute(h)}set stopPropagateHandled(t){t?this.setAttribute(h,""):this.removeAttribute(h)}connectedCallback(){this._stageElChange()}get x(){return this._transform.e}get y(){return this._transform.f}get scale(){return this._transform.a}scaleTo(t,e={}){let{originX:i=0,originY:n=0}=e;const{relativeTo:s="content",allowChangeEvent:r=!1}=e,o="content"===s?this._positioningEl:this;if(!o||!this._positioningEl)return void this.setTransform({scale:t,allowChangeEvent:r});const a=o.getBoundingClientRect();if(i=d(i,a.width),n=d(n,a.height),"content"===s)i+=this.x,n+=this.y;else{const t=this._positioningEl.getBoundingClientRect();i-=t.left,n-=t.top}this._applyChange({allowChangeEvent:r,originX:i,originY:n,scaleDiff:t/this.scale})}setTransform(t={}){const{scale:e=this.scale,allowChangeEvent:i=!1}=t;let{x:n=this.x,y:s=this.y}=t;if(!this._positioningEl)return void this._updateTransform(e,n,s,i);const r=this.getBoundingClientRect(),o=this._positioningEl.getBoundingClientRect();if(!r.width||!r.height)return void this._updateTransform(e,n,s,i);let a=_();a.x=o.left-r.left,a.y=o.top-r.top;let h=_();h.x=o.width+a.x,h.y=o.height+a.y;const l=m().translate(n,s).scale(e).multiply(this._transform.inverse());a=a.matrixTransform(l),h=h.matrixTransform(l),a.x>r.width?n+=r.width-a.x:h.x<0&&(n+=-h.x),a.y>r.height?s+=r.height-a.y:h.y<0&&(s+=-h.y),this._updateTransform(e,n,s,i)}_updateTransform(t,e,i,n){if(t<this.minScale&&(t=this.minScale),(t!==this.scale||e!==this.x||i!==this.y)&&(this._transform.e=e,this._transform.f=i,this._transform.d=this._transform.a=t,this.style.setProperty("--x",this.x+"px"),this.style.setProperty("--y",this.y+"px"),this.style.setProperty("--scale",this.scale+""),n)){const t=new Event("change",{bubbles:!0});this.dispatchEvent(t)}}_stageElChange(){this._positioningEl=void 0,0!==this.children.length&&(this._positioningEl=this.children[0],this.children.length>1&&console.warn("<pinch-zoom> must not have more than one child."),this.setTransform({allowChangeEvent:!0}))}_onWheel(t){if(!this._positioningEl)return;t.preventDefault();const e=this._positioningEl.getBoundingClientRect();let{deltaY:i}=t;const{ctrlKey:n,deltaMode:s}=t;1===s&&(i*=15);const r=1-i/(n?100:300),o=r<1;this._applyChange({scaleDiff:r,originX:t.clientX-e.left,originY:t.clientY-e.top,allowChangeEvent:!0}),o&&this._maybeResetScale()}_onPointerMove(t,e,i){if(!this._positioningEl)return;if(t.length<2&&!this._allowPan())return;const n=this._positioningEl.getBoundingClientRect(),s=g(t[0],t[1]),r=g(e[0],e[1]),o=s.clientX-n.left,a=s.clientY-n.top,h=c(t[0],t[1]),l=c(e[0],e[1]),d=h?l/h:1;this._applyChange({originX:o,originY:a,scaleDiff:d,panX:r.clientX-s.clientX,panY:r.clientY-s.clientY,allowChangeEvent:!0}),this._maybeStopPropagate(i)}_maybeResetScale(){w(this.scale,this.resetToMinScaleLimit)<=0&&this._resetToMinScale()}_onPointerEnd(t,e,i){if(!this._positioningEl)return;const n=1+e.length,s=1==n;n>=2&&this._maybeResetScale(),s&&!this._allowPan()||this._maybeStopPropagate(i)}_resetToMinScale(){"reset"===this.reachMinScaleStrategy?this.setTransform({scale:this.minScale,x:0,y:0}):this.setTransform({scale:this.minScale})}_applyChange(t={}){const{panX:e=0,panY:i=0,originX:n=0,originY:s=0,scaleDiff:r=1,allowChangeEvent:o=!1}=t,a=m().translate(e,i).translate(n,s).translate(this.x,this.y).scale(r).translate(-n,-s).scale(this.scale);this.setTransform({allowChangeEvent:o,scale:a.a,x:a.e,y:a.f})}_maybeStopPropagate(t){this.stopPropagateHandled&&t.stopPropagation()}_allowPan(){return this.allowPanMinScale>0&&w(this.scale,this.allowPanMinScale)>0}_maybeEmitCancel(t){this.stopPropagateHandled&&t.forEach(t=>{this.parentElement&&"function"==typeof this.parentElement.dispatchEvent&&this.parentElement.dispatchEvent((t=>new PointerEvent("pointercancel",{pointerId:t.id,clientX:t.clientX,clientY:t.clientY}))(t))})}_onClick(t,e){if(t.button!==P)return;!(0===e.currentPointers.length&&!this._allowPan())&&this._maybeStopPropagate(t)}}customElements.define("pinch-zoom",b)}()}();
|