coherent-gameface-interaction-manager
Version:
Library for the most common UI interactions
7 lines (6 loc) • 10.1 kB
JavaScript
(function(v,f){typeof exports=="object"&&typeof module<"u"?module.exports=f():typeof define=="function"&&define.amd?define(f):(v=typeof globalThis<"u"?globalThis:v||self,v.zoom=f())})(this,(function(){"use strict";function v(c){return c*180/Math.PI}function f(){return(Math.random()+1).toString(36).substring(7)}function p(c,e,t,n){const i=c-t,r=e-n;return Math.hypot(i,r)}function y(c,e,t,n){return{x:(c+t)/2,y:(e+n)/2}}class _{constructor(){this.actions=[],this.keyboardFunctions=[],this.gamepadFunctions=[]}init(){window._IM||(window._IM=new _)}getKeys(e){return _IM.keyboardFunctions.filter(t=>t.keys.every(n=>e.includes(n)))}getKeysIndex(e){return _IM.keyboardFunctions.findIndex(t=>t.keys.every(n=>e.includes(n)))}getGamepadAction({actions:e,type:t}){return _IM.gamepadFunctions.find(n=>n.actions.every(i=>e.includes(i))&&n.type===t&&n.actions.length===e.length)}getGamepadActions(e,t=!0){return _IM.gamepadFunctions.filter(n=>n.actions.every(i=>e.includes(i))&&(t?n.actions.length===e.length:!0))}getGamepadActionIndex(e){return _IM.gamepadFunctions.findIndex(t=>t.actions.every(n=>e.includes(n)))}getAction(e){return _IM.actions.find(t=>t.name===e)}getActionIndex(e){return _IM.actions.findIndex(t=>t.name===e)}hasDuplicateCallback(e,t){return e.callbacks.some(n=>n===t)}addCallbackToEntry(e,t,n){if(this.hasDuplicateCallback(e,t)){const i=typeof t=="string"?"action":"function",r=typeof t=="string"?t:"(anonymous function)";return console.error(`Duplicate callback detected!
${n.identifier}
Type: '${n.type}'
Callback: ${r}
This ${i} is already registered for this combination and type. To update it, first remove with the .off() method.`)}return e.callbacks.push(t)}removeKeyboardFunction(e){const t=_IM.keyboardFunctions.indexOf(e);t!==-1&&_IM.keyboardFunctions.splice(t,1)}removeGamepadFunction(e){const t=_IM.gamepadFunctions.indexOf(e);t!==-1&&_IM.gamepadFunctions.splice(t,1)}}const E=new _;class F{register(e,t){if(E.getAction(e))return console.error(`The following action "${e}" is already registered!`);_IM.actions.push({name:e,callback:t})}remove(e){const t=E.getActionIndex(e);if(t===-1)return console.error(`${e} is not a registered action!`);_IM.actions.splice(t,1)}execute(e,t){const n=E.getAction(e);if(!n)return console.error(`${e} is not a registered action!`);n.callback(t)}}const T=new F;function M(c,e){if(e<0&&c>-200&&c<200)return"top";if(e>0&&c>-200&&c<200)return"bottom";if(c<0&&e>-200&&e<200)return"left";if(c>0&&e>-200&&e<200)return"right";if(c<=-200&&e<=-200)return"top-left";if(c>=200&&e<=-200)return"top-right";if(c<=-200&&e>=200)return"bottom-left";if(c>=200&&e>=200)return"bottom-right"}function d(c){return c instanceof HTMLElement?c:document.querySelector(c)}const g=2;class L{constructor(){this.activeTouches=new Map}hold(e){if(!e)return console.error("Options not provided for hold!");let t;const n=d(e.element);if(!n)return console.error("Element not found!");const i=({touches:o})=>{this.activeTouches.set(o[0].identifier,o[0]),t=setTimeout(()=>{e.callback&&e.callback()},e.time||1e3)},r=({touches:o})=>{this.activeTouches.delete(o[0].identifier),clearTimeout(t)};return n.addEventListener("touchstart",i),n.addEventListener("touchend",r),{remove(){n.removeEventListener("touchstart",i),n.removeEventListener("touchend",r)}}}tap(e){if(!e)return console.error("Options not provided for tap!");let t,n,i=!0,r=e.tapsNumber||1;const o=d(e.element);if(!o)return console.error("Element not found!");const a=({touches:s})=>{this.activeTouches.set(s[0].identifier,s[0]),clearTimeout(n),i=!0,t=setTimeout(()=>{i=!1},e.tapTime||200)},u=({touches:s})=>{this.activeTouches.delete(s[0].identifier),clearTimeout(t),i&&(r--,n=setTimeout(()=>{r=e.tapsNumber||1,clearTimeout(n)},e.betweenTapsTime||500),!(r!==0||!e.callback)&&(e.callback(),i=!0,clearTimeout(t),r=e.tapsNumber||1))};return o.addEventListener("touchstart",a),o.addEventListener("touchend",u),{remove(){o.removeEventListener("touchstart",a),o.removeEventListener("touchend",u)}}}drag(e){if(!e)return console.error("Options not provided for drag!");const t=d(e.element);if(!t)return console.error("Element not found!");const n=({touches:o,target:a,currentTarget:u})=>{this.activeTouches.set(o[0].identifier,o[0]),document.addEventListener("touchmove",i),document.addEventListener("touchend",r),e.onDragStart&&e.onDragStart({x:o[0].clientX,y:o[0].clientY,target:a,currentTarget:u})},i=({touches:o})=>{this.activeTouches.has(o[0].identifier)&&e.onDrag&&e.onDrag({x:o[0].clientX,y:o[0].clientY})},r=({touches:o})=>{this.activeTouches.delete(o[0].identifier),document.removeEventListener("touchmove",i),document.removeEventListener("touchend",r),e.onDragEnd&&e.onDragEnd({x:o[0].clientX,y:o[0].clientY})};return t.addEventListener("touchstart",n),{remove(){t.removeEventListener("touchstart",n)}}}swipe(e){if(!e)return console.error("Options not provided for swipe!");let t,n,i,r=!0;const o=100;e.touchNumber||=1;const a=d(e.element);if(!a)return console.error("Element not found!");const u=({touches:l})=>{this.activeTouches.set(l[0].identifier,l[0]),this.activeTouches.size>e.touchNumber&&(document.removeEventListener("touchmove",s),document.removeEventListener("touchend",h)),this.activeTouches.size===e.touchNumber&&(t=setTimeout(()=>{r=!1,clearTimeout(t),t=null},1e3),document.addEventListener("touchmove",s),document.addEventListener("touchend",h))},s=({touches:l})=>{if(!this.activeTouches.has(l[0].identifier))return;const I=this.activeTouches.get(l[0].identifier);if(!I)return;const{clientX:b,clientY:S}=I,O=l[0].clientX-b,P=l[0].clientY-S;n=M(O,P),i=p(b,S,l[0].clientX,l[0].clientY)},h=({touches:l})=>{this.activeTouches.delete(l[0].identifier),this.activeTouches.size===0&&(document.removeEventListener("touchmove",s),document.removeEventListener("touchend",h),m()&&e.callback(n),clearTimeout(t),r=!0,t=null)},m=()=>r&&n&&i&&i>o;return a.addEventListener("touchstart",u),{remove(){a.removeEventListener("touchstart",u)}}}pinch(e){if(!e)return console.error("Options not provided for pinch!");let t;const n=40,i=d(e.element);if(!i)return console.error("Element not found!");const r=({touches:u})=>{if(this.activeTouches.set(u[0].identifier,u[0]),this.activeTouches.size<g)return;const s=this.activeTouches.get(0),h=this.activeTouches.get(1);!s||!h||(document.addEventListener("touchmove",o),document.addEventListener("touchend",a),t=p(s.clientX,s.clientY,h.clientX,h.clientY))},o=({touches:u})=>{if(this.activeTouches.size!==g)return;this.activeTouches.set(u[0].identifier,u[0]);const s=this.activeTouches.get(0),h=this.activeTouches.get(1);if(!s||!h)return;const m=p(s.clientX,s.clientY,h.clientX,h.clientY),l=Math.sign(m-t)*n;t=m;const I=y(s.clientX,s.clientY,h.clientX,h.clientY);e.callback&&e.callback({pinchDelta:l,midpoint:I})},a=({touches:u})=>{this.activeTouches.delete(u[0].identifier),this.activeTouches.size===0&&(document.removeEventListener("touchmove",o),document.removeEventListener("touchend",a))};return i.addEventListener("touchstart",r),{remove(){i.removeEventListener("touchstart",r)}}}rotate(e){if(!e)return console.error("Options not provided for rotate!");let t=0,n=0;const i=d(e.element);if(!i)return console.error("Element not found!");const r=({touches:s})=>{this.activeTouches.size<g||(this.activeTouches.set(s[0].identifier,s[0]),t=u()-n,e.callback&&e.callback(t))},o=({touches:s})=>{this.activeTouches.set(s[0].identifier,s[0]),this.activeTouches.size===g&&(n=u()-t,document.addEventListener("touchmove",r),document.addEventListener("touchend",a))},a=({touches:s})=>{this.activeTouches.delete(s[0].identifier),document.removeEventListener("touchmove",r),document.removeEventListener("touchend",a)};i.addEventListener("touchstart",o);const u=()=>{const m=this.activeTouches.get(0).clientY-this.activeTouches.get(1).clientY,l=this.activeTouches.get(0).clientX-this.activeTouches.get(1).clientX;return(v(Math.atan2(m,l))+360+90)%360};return{remove(){i.removeEventListener("touchstart",o)}}}}const x=new L;class N{constructor(e){this.enabled=!1,this.actionName=`pan-and-zoom-${f()}`,this.offset={x:0,y:0},this.transform={x:0,y:0,scale:1},this.touchEvents=null,this._touchEnabled=!1,this.options=e,this.options.maxZoom=this.options.maxZoom||1/0,this.options.minZoom=this.options.minZoom||.1,this.options.zoomFactor=this.options.zoomFactor||.1,this.onWheel=this.onWheel.bind(this),this.init()}set touchEnabled(e){this._touchEnabled!==e&&(this._touchEnabled=e,this._touchEnabled?this.addTouchEvents():this.removeTouchEvents())}init(){if(!this.enabled){if(this.zoomableElement=document.querySelector(this.options.element),!this.zoomableElement)return console.error(`${this.options.element} is not a correct element selector`);this.zoomableElement.addEventListener("wheel",this.onWheel),this.registerActions(),this.zoomableElement.style.transformOrigin="top left",this.enabled=!0}}deinit(){this.enabled&&(this.zoomableElement.removeEventListener("wheel",this.onWheel),this.removeActions(),this.enabled=!1)}onWheel(e){e.preventDefault(),T.execute(this.actionName,{x:e.clientX,y:e.clientY,zoomDirection:Math.sign(e.deltaY)})}registerActions(){T.register(this.actionName,({x:e,y:t,zoomDirection:n})=>{const i=this.calculateOffsets(e,t);this.options.onZoom&&this.options.onZoom();const r=Number((this.transform.scale-n*this.options.zoomFactor).toFixed(5));if(r<this.options.minZoom||r>this.options.maxZoom)return;const o={x:i.x/this.transform.scale,y:i.y/this.transform.scale},a=this.transform;a.x+=o.x*(this.transform.scale-r),a.y+=o.y*(this.transform.scale-r),a.scale=r,this.zoomableElement.style.transform=`matrix(${a.scale}, 0, 0, ${a.scale}, ${a.x}, ${a.y})`,this.transform=a})}removeActions(){T.remove(this.actionName)}addTouchEvents(){this.touchEvents=x.pinch({element:this.zoomableElement,callback:({pinchDelta:e,midpoint:t})=>{T.execute(this.actionName,{x:t.x,y:t.y,zoomDirection:Math.sign(e)*-1})}})}removeTouchEvents(){this.touchEvents?.remove(),this.touchEvents=null}calculateOffsets(e,t){const n=this.zoomableElement.getBoundingClientRect();return{x:e-n.x,y:t-n.y}}}return E.init(),N}));
//# sourceMappingURL=zoom.min.js.map