diff --git a/dist/pinch-zoom-min.js b/dist/pinch-zoom-min.js index 210002b..7425653 100644 --- a/dist/pinch-zoom-min.js +++ b/dist/pinch-zoom-min.js @@ -1 +1 @@ -!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,n=()=>{};class i{constructor(t,e){this._element=t,this.startPointers=[],this.currentPointers=[];const{start:i=(()=>!0),move:s=n,end:r=n}=e;this._startCallback=i,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(n){0===n.button&&this._triggerPointerStart(new t(n),n)&&(e(n)?(this._element.setPointerCapture(n.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 n of Array.from(e.changedTouches))this._triggerPointerStart(new t(n),e)}_move(e){const n=this.currentPointers.slice(),i="changedTouches"in e?Array.from(e.changedTouches).map(e=>new t(e)):[new t(e)],s=[];for(const t of i){const e=this.currentPointers.findIndex(e=>e.id===t.id);-1!==e&&(s.push(t),this.currentPointers[e]=t)}0!==s.length&&this._moveCallback(n,s,e)}_triggerPointerEnd(t,e){const n=this.currentPointers.findIndex(e=>e.id===t.id);return-1!==n&&(this.currentPointers.splice(n,1),this.startPointers.splice(n,1),this._endCallback(t,e),!0)}_pointerEnd(n){if(this._triggerPointerEnd(new t(n),n))if(e(n)){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 n of Array.from(e.changedTouches))this._triggerPointerEnd(new t(n),e)}}!function(t,e){void 0===e&&(e={});var n=e.insertAt;if(t&&"undefined"!=typeof document){var i=document.head||document.getElementsByTagName("head")[0],s=document.createElement("style");s.type="text/css","top"===n&&i.firstChild?i.insertBefore(s,i.firstChild):i.appendChild(s),s.styleSheet?s.styleSheet.cssText=t:s.appendChild(document.createTextNode(t))}}("pinch-zoom {\n display: block;\n overflow: hidden;\n touch-action: none;\n --scale: 1;\n --x: 0;\n --y: 0;\n}\n\npinch-zoom > * {\n transform: translate(var(--x), var(--y)) scale(var(--scale));\n transform-origin: 0 0;\n will-change: transform;\n}\n");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 d(t,e){return e?{clientX:(t.clientX+e.clientX)/2,clientY:(t.clientY+e.clientY)/2}:t}function u(t,e){return"number"==typeof t?t:t.trimRight().endsWith("%")?e*parseFloat(t)/100:parseFloat(t)}let g;function p(){return g||(g=document.createElementNS("http://www.w3.org/2000/svg","svg"))}function m(){return p().createSVGMatrix()}function _(){return p().createSVGPoint()}const f=.01,v=-1,E=-1,P=(t,e)=>Math.round(100*t)-Math.round(100*e);class y extends HTMLElement{constructor(){super(),this._transform=m(),new MutationObserver(()=>this._stageElChange()).observe(this,{childList:!0});const t=new i(this,{start:(e,n)=>{if(2===t.currentPointers.length||!this._positioningEl)return!1;const i=t.currentPointers.length+1===1;return!(i&&!this._allowPan())&&(this._maybeStopPropagate(n),i||this._maybeEmitCancel([e,...t.currentPointers])),n.preventDefault(),!0},move:(e,n,i)=>{this._onPointerMove(e,t.currentPointers,i)},end:(e,n)=>{this._onPointerEnd(e,t.currentPointers,n)}});this.addEventListener("wheel",t=>this._onWheel(t))}static get observedAttributes(){return[s]}attributeChangedCallback(t,e,n){t===s&&this.scaler.width?i+=r.width-a.x:h.x<0&&(i+=-h.x),a.y>r.height?s+=r.height-a.y:h.y<0&&(s+=-h.y),this._updateTransform(e,i,s,n)}_updateTransform(t,e,n,i){if(!(t1&&console.warn(" 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:n}=t;const{ctrlKey:i,deltaMode:s}=t;1===s&&(n*=15);const r=1-n/(i?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,n){if(!this._positioningEl)return;if(t.length<2&&!this._allowPan())return;const i=this._positioningEl.getBoundingClientRect(),s=d(t[0],t[1]),r=d(e[0],e[1]),o=s.clientX-i.left,a=s.clientY-i.top,h=c(t[0],t[1]),l=c(e[0],e[1]),u=h?l/h:1;this._applyChange({originX:o,originY:a,scaleDiff:u,panX:r.clientX-s.clientX,panY:r.clientY-s.clientY,allowChangeEvent:!0}),this._maybeStopPropagate(n)}_maybeResetScale(){P(this.scale,this.resetToMinScaleLimit)<=0&&this._resetToMinScale()}_onPointerEnd(t,e,n){if(!this._positioningEl)return;const i=1+e.length,s=1==i;i>=2&&this._maybeResetScale(),s&&!this._allowPan()||this._maybeStopPropagate(n)}_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:n=0,originX:i=0,originY:s=0,scaleDiff:r=1,allowChangeEvent:o=!1}=t,a=m().translate(e,n).translate(i,s).translate(this.x,this.y).scale(r).translate(-i,-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&&P(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))})}}customElements.define("pinch-zoom",y)}()}(); +!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,n=()=>{};class i{constructor(t,e){this._element=t,this.startPointers=[],this.currentPointers=[];const{start:i=(()=>!0),move:s=n,end:r=n}=e;this._startCallback=i,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(n){0===n.button&&this._triggerPointerStart(new t(n),n)&&(e(n)?(this._element.setPointerCapture(n.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 n of Array.from(e.changedTouches))this._triggerPointerStart(new t(n),e)}_move(e){const n=this.currentPointers.slice(),i="changedTouches"in e?Array.from(e.changedTouches).map(e=>new t(e)):[new t(e)],s=[];for(const t of i){const e=this.currentPointers.findIndex(e=>e.id===t.id);-1!==e&&(s.push(t),this.currentPointers[e]=t)}0!==s.length&&this._moveCallback(n,s,e)}_triggerPointerEnd(t,e){const n=this.currentPointers.findIndex(e=>e.id===t.id);return-1!==n&&(this.currentPointers.splice(n,1),this.startPointers.splice(n,1),this._endCallback(t,e),!0)}_pointerEnd(n){if(this._triggerPointerEnd(new t(n),n))if(e(n)){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 n of Array.from(e.changedTouches))this._triggerPointerEnd(new t(n),e)}}!function(t,e){void 0===e&&(e={});var n=e.insertAt;if(t&&"undefined"!=typeof document){var i=document.head||document.getElementsByTagName("head")[0],s=document.createElement("style");s.type="text/css","top"===n&&i.firstChild?i.insertBefore(s,i.firstChild):i.appendChild(s),s.styleSheet?s.styleSheet.cssText=t:s.appendChild(document.createTextNode(t))}}("pinch-zoom {\n display: block;\n overflow: hidden;\n touch-action: none;\n --scale: 1;\n --x: 0;\n --y: 0;\n}\n\npinch-zoom > * {\n transform: translate(var(--x), var(--y)) scale(var(--scale));\n transform-origin: 0 0;\n will-change: transform;\n}\n");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 d(t,e){return e?{clientX:(t.clientX+e.clientX)/2,clientY:(t.clientY+e.clientY)/2}:t}function u(t,e){return"number"==typeof t?t:t.trimRight().endsWith("%")?e*parseFloat(t)/100:parseFloat(t)}let g;function p(){return g||(g=document.createElementNS("http://www.w3.org/2000/svg","svg"))}function m(){return p().createSVGMatrix()}function _(){return p().createSVGPoint()}const f=.01,v=-1,E=-1,P=0,y=(t,e)=>Math.round(100*t)-Math.round(100*e);class w extends HTMLElement{constructor(){super(),this._transform=m(),new MutationObserver(()=>this._stageElChange()).observe(this,{childList:!0});const t=new i(this,{start:(e,n)=>{if(2===t.currentPointers.length||!this._positioningEl)return!1;const i=t.currentPointers.length+1===1;return!(i&&!this._allowPan())&&(this._maybeStopPropagate(n),i||this._maybeEmitCancel([e,...t.currentPointers])),n.preventDefault(),!0},move:(e,n,i)=>{this._onPointerMove(e,t.currentPointers,i)},end:(e,n)=>{this._onPointerEnd(e,t.currentPointers,n)}});this.addEventListener("wheel",t=>this._onWheel(t)),this.addEventListener("click",e=>this._onClick(e,t))}static get observedAttributes(){return[s]}attributeChangedCallback(t,e,n){t===s&&this.scaler.width?i+=r.width-a.x:h.x<0&&(i+=-h.x),a.y>r.height?s+=r.height-a.y:h.y<0&&(s+=-h.y),this._updateTransform(e,i,s,n)}_updateTransform(t,e,n,i){if(!(t1&&console.warn(" 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:n}=t;const{ctrlKey:i,deltaMode:s}=t;1===s&&(n*=15);const r=1-n/(i?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,n){if(!this._positioningEl)return;if(t.length<2&&!this._allowPan())return;const i=this._positioningEl.getBoundingClientRect(),s=d(t[0],t[1]),r=d(e[0],e[1]),o=s.clientX-i.left,a=s.clientY-i.top,h=c(t[0],t[1]),l=c(e[0],e[1]),u=h?l/h:1;this._applyChange({originX:o,originY:a,scaleDiff:u,panX:r.clientX-s.clientX,panY:r.clientY-s.clientY,allowChangeEvent:!0}),this._maybeStopPropagate(n)}_maybeResetScale(){y(this.scale,this.resetToMinScaleLimit)<=0&&this._resetToMinScale()}_onPointerEnd(t,e,n){if(!this._positioningEl)return;const i=1+e.length,s=1==i;i>=2&&this._maybeResetScale(),s&&!this._allowPan()||this._maybeStopPropagate(n)}_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:n=0,originX:i=0,originY:s=0,scaleDiff:r=1,allowChangeEvent:o=!1}=t,a=m().translate(e,n).translate(i,s).translate(this.x,this.y).scale(r).translate(-i,-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&&y(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",w)}()}(); diff --git a/dist/pinch-zoom.cjs.js b/dist/pinch-zoom.cjs.js index 8f453d2..c3fee31 100644 --- a/dist/pinch-zoom.cjs.js +++ b/dist/pinch-zoom.cjs.js @@ -76,6 +76,7 @@ function createPoint() { const MIN_SCALE = 0.01; const ALLOW_PAN_MIN_SCALE = -1; const RESET_TO_MIN_SCALE_LIMIT = -1; +const BUTTON_LEFT = 0; const roundedCmp = (a, b) => { return Math.round(a * 100) - Math.round(b * 100); }; @@ -114,6 +115,7 @@ class PinchZoom extends HTMLElement { }, }); this.addEventListener('wheel', event => this._onWheel(event)); + this.addEventListener('click', event => this._onClick(event, pointerTracker)); } static get observedAttributes() { return [minScaleAttr]; } attributeChangedCallback(name, oldValue, newValue) { @@ -438,6 +440,17 @@ class PinchZoom extends HTMLElement { }); } } + _onClick(event, pointerTracker) { + // We never handle non-left-clicks + if (event.button !== BUTTON_LEFT) { + return; + } + const wasPanning = pointerTracker.currentPointers.length === 0; + const handled = !(wasPanning && !this._allowPan()); + if (handled) { + this._maybeStopPropagate(event); + } + } } customElements.define('pinch-zoom', PinchZoom); diff --git a/dist/pinch-zoom.d.ts b/dist/pinch-zoom.d.ts index 7fad721..392aff3 100644 --- a/dist/pinch-zoom.d.ts +++ b/dist/pinch-zoom.d.ts @@ -64,5 +64,6 @@ export default class PinchZoom extends HTMLElement { private _maybeStopPropagate; private _allowPan; private _maybeEmitCancel; + private _onClick; } export {}; diff --git a/dist/pinch-zoom.es.js b/dist/pinch-zoom.es.js index d2c95e8..3a6a716 100644 --- a/dist/pinch-zoom.es.js +++ b/dist/pinch-zoom.es.js @@ -72,6 +72,7 @@ function createPoint() { const MIN_SCALE = 0.01; const ALLOW_PAN_MIN_SCALE = -1; const RESET_TO_MIN_SCALE_LIMIT = -1; +const BUTTON_LEFT = 0; const roundedCmp = (a, b) => { return Math.round(a * 100) - Math.round(b * 100); }; @@ -110,6 +111,7 @@ class PinchZoom extends HTMLElement { }, }); this.addEventListener('wheel', event => this._onWheel(event)); + this.addEventListener('click', event => this._onClick(event, pointerTracker)); } static get observedAttributes() { return [minScaleAttr]; } attributeChangedCallback(name, oldValue, newValue) { @@ -434,6 +436,17 @@ class PinchZoom extends HTMLElement { }); } } + _onClick(event, pointerTracker) { + // We never handle non-left-clicks + if (event.button !== BUTTON_LEFT) { + return; + } + const wasPanning = pointerTracker.currentPointers.length === 0; + const handled = !(wasPanning && !this._allowPan()); + if (handled) { + this._maybeStopPropagate(event); + } + } } customElements.define('pinch-zoom', PinchZoom); diff --git a/dist/pinch-zoom.js b/dist/pinch-zoom.js index 1583de7..cca7c3a 100644 --- a/dist/pinch-zoom.js +++ b/dist/pinch-zoom.js @@ -257,6 +257,7 @@ var PinchZoom = (function () { const MIN_SCALE = 0.01; const ALLOW_PAN_MIN_SCALE = -1; const RESET_TO_MIN_SCALE_LIMIT = -1; + const BUTTON_LEFT = 0; const roundedCmp = (a, b) => { return Math.round(a * 100) - Math.round(b * 100); }; @@ -295,6 +296,7 @@ var PinchZoom = (function () { }, }); this.addEventListener('wheel', event => this._onWheel(event)); + this.addEventListener('click', event => this._onClick(event, pointerTracker)); } static get observedAttributes() { return [minScaleAttr]; } attributeChangedCallback(name, oldValue, newValue) { @@ -619,6 +621,17 @@ var PinchZoom = (function () { }); } } + _onClick(event, pointerTracker) { + // We never handle non-left-clicks + if (event.button !== BUTTON_LEFT) { + return; + } + const wasPanning = pointerTracker.currentPointers.length === 0; + const handled = !(wasPanning && !this._allowPan()); + if (handled) { + this._maybeStopPropagate(event); + } + } } customElements.define('pinch-zoom', PinchZoom); diff --git a/dist/pinch-zoom.mjs b/dist/pinch-zoom.mjs index d2c95e8..3a6a716 100644 --- a/dist/pinch-zoom.mjs +++ b/dist/pinch-zoom.mjs @@ -72,6 +72,7 @@ function createPoint() { const MIN_SCALE = 0.01; const ALLOW_PAN_MIN_SCALE = -1; const RESET_TO_MIN_SCALE_LIMIT = -1; +const BUTTON_LEFT = 0; const roundedCmp = (a, b) => { return Math.round(a * 100) - Math.round(b * 100); }; @@ -110,6 +111,7 @@ class PinchZoom extends HTMLElement { }, }); this.addEventListener('wheel', event => this._onWheel(event)); + this.addEventListener('click', event => this._onClick(event, pointerTracker)); } static get observedAttributes() { return [minScaleAttr]; } attributeChangedCallback(name, oldValue, newValue) { @@ -434,6 +436,17 @@ class PinchZoom extends HTMLElement { }); } } + _onClick(event, pointerTracker) { + // We never handle non-left-clicks + if (event.button !== BUTTON_LEFT) { + return; + } + const wasPanning = pointerTracker.currentPointers.length === 0; + const handled = !(wasPanning && !this._allowPan()); + if (handled) { + this._maybeStopPropagate(event); + } + } } customElements.define('pinch-zoom', PinchZoom); diff --git a/lib/pinch-zoom.ts b/lib/pinch-zoom.ts index c9dc510..e600714 100644 --- a/lib/pinch-zoom.ts +++ b/lib/pinch-zoom.ts @@ -91,6 +91,8 @@ const MIN_SCALE = 0.01; const ALLOW_PAN_MIN_SCALE = -1; const RESET_TO_MIN_SCALE_LIMIT = -1; +const BUTTON_LEFT = 0; + const roundedCmp = (a: number, b: number) => { return Math.round(a * 100) - Math.round(b * 100) } @@ -142,6 +144,7 @@ export default class PinchZoom extends HTMLElement { }); this.addEventListener('wheel', event => this._onWheel(event)); + this.addEventListener('click', event => this._onClick(event, pointerTracker)); } attributeChangedCallback(name: string, oldValue: string, newValue: string) { @@ -552,4 +555,17 @@ export default class PinchZoom extends HTMLElement { }); } } + + private _onClick(event: MouseEvent, pointerTracker: PointerTracker) { + // We never handle non-left-clicks + if (event.button !== BUTTON_LEFT) { + return; + } + + const wasPanning = pointerTracker.currentPointers.length === 0; + const handled = !(wasPanning && !this._allowPan()); + if (handled) { + this._maybeStopPropagate(event); + } + } }