UNPKG

coherent-gameface-interaction-manager

Version:
3 lines (2 loc) 6.92 kB
var touchGestures=(()=>{var v=(i,e)=>()=>(i&&(e=i(i=0)),e);var W=(i,e)=>()=>(e||i((e={exports:{}}).exports,e),e.exports);function _(i,e){if(e<0&&i>-200&&i<200)return"top";if(e>0&&i>-200&&i<200)return"bottom";if(i<0&&e>-200&&e<200)return"left";if(i>0&&e>-200&&e<200)return"right";if(i<=-200&&e<=-200)return"top-left";if(i>=200&&e<=-200)return"top-right";if(i<=-200&&e>=200)return"bottom-left";if(i>=200&&e>=200)return"bottom-right"}function d(i){return i instanceof HTMLElement?i:document.querySelector(i)}var M=v(()=>{});function F(i){return i*180/Math.PI}function E(i,e,t,n){let o=i-t,c=e-n;return Math.hypot(o,c)}function L(i,e,t,n){return{x:(i+t)/2,y:(e+n)/2}}var p=v(()=>{});var T,f,N,P=v(()=>{M();p();T=2,f=class{constructor(){this.activeTouches=new Map}hold(e){if(!e)return console.error("Options not provided for hold!");let t,n=d(e.element);if(!n)return console.error("Element not found!");let o=({touches:r})=>{this.activeTouches.set(r[0].identifier,r[0]),t=setTimeout(()=>{e.callback&&e.callback()},e.time||1e3)},c=({touches:r})=>{this.activeTouches.delete(r[0].identifier),clearTimeout(t)};return n.addEventListener("touchstart",o),n.addEventListener("touchend",c),{remove(){n.removeEventListener("touchstart",o),n.removeEventListener("touchend",c)}}}tap(e){if(!e)return console.error("Options not provided for tap!");let t,n,o=!0,c=e.tapsNumber||1,r=d(e.element);if(!r)return console.error("Element not found!");let u=({touches:s})=>{this.activeTouches.set(s[0].identifier,s[0]),clearTimeout(n),o=!0,t=setTimeout(()=>{o=!1},e.tapTime||200)},a=({touches:s})=>{this.activeTouches.delete(s[0].identifier),clearTimeout(t),o&&(c--,n=setTimeout(()=>{c=e.tapsNumber||1,clearTimeout(n)},e.betweenTapsTime||500),!(c!==0||!e.callback)&&(e.callback(),o=!0,clearTimeout(t),c=e.tapsNumber||1))};return r.addEventListener("touchstart",u),r.addEventListener("touchend",a),{remove(){r.removeEventListener("touchstart",u),r.removeEventListener("touchend",a)}}}drag(e){if(!e)return console.error("Options not provided for drag!");let t=d(e.element);if(!t)return console.error("Element not found!");let n=({touches:r,target:u,currentTarget:a})=>{this.activeTouches.set(r[0].identifier,r[0]),document.addEventListener("touchmove",o),document.addEventListener("touchend",c),e.onDragStart&&e.onDragStart({x:r[0].clientX,y:r[0].clientY,target:u,currentTarget:a})},o=({touches:r})=>{this.activeTouches.has(r[0].identifier)&&e.onDrag&&e.onDrag({x:r[0].clientX,y:r[0].clientY})},c=({touches:r})=>{this.activeTouches.delete(r[0].identifier),document.removeEventListener("touchmove",o),document.removeEventListener("touchend",c),e.onDragEnd&&e.onDragEnd({x:r[0].clientX,y:r[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,o,c=!0,r=100;e.touchNumber||=1;let u=d(e.element);if(!u)return console.error("Element not found!");let a=({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(()=>{c=!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;let{clientX:I,clientY:S}=this.activeTouches.get(l[0].identifier),x=l[0].clientX-I,y=l[0].clientY-S;n=_(x,y),o=E(I,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),c=!0,t=null)},m=()=>c&&e.callback&&n&&o>r;return u.addEventListener("touchstart",a),{remove(){u.removeEventListener("touchstart",a)}}}pinch(e){if(!e)return console.error("Options not provided for pinch!");let t,n=40,o=d(e.element);if(!o)return console.error("Element not found!");let c=({touches:a})=>{this.activeTouches.set(a[0].identifier,a[0]),!(this.activeTouches.size<T)&&(document.addEventListener("touchmove",r),document.addEventListener("touchend",u),t=E(this.activeTouches.get(0).clientX,this.activeTouches.get(0).clientY,this.activeTouches.get(1).clientX,this.activeTouches.get(1).clientY))},r=({touches:a})=>{if(this.activeTouches.size!==T)return;this.activeTouches.set(a[0].identifier,a[0]);let s=E(this.activeTouches.get(0).clientX,this.activeTouches.get(0).clientY,this.activeTouches.get(1).clientX,this.activeTouches.get(1).clientY),h=Math.sign(s-t)*n;t=s;let m=L(this.activeTouches.get(0).clientX,this.activeTouches.get(0).clientY,this.activeTouches.get(1).clientX,this.activeTouches.get(1).clientY);e.callback&&e.callback({pinchDelta:h,midpoint:m})},u=({touches:a})=>{this.activeTouches.delete(a[0].identifier),this.activeTouches.size===0&&(document.removeEventListener("touchmove",r),document.removeEventListener("touchend",u))};return o.addEventListener("touchstart",c),{remove(){o.removeEventListener("touchstart",c)}}}rotate(e){if(!e)return console.error("Options not provided for rotate!");let t=0,n,o=d(e.element);if(!o)return console.error("Element not found!");let c=({touches:s})=>{this.activeTouches.size<T||(this.activeTouches.set(s[0].identifier,s[0]),t=a()-n,e.callback&&e.callback(t))},r=({touches:s})=>{this.activeTouches.set(s[0].identifier,s[0]),this.activeTouches.size===T&&(n=a()-t,document.addEventListener("touchmove",c),document.addEventListener("touchend",u))},u=({touches:s})=>{this.activeTouches.delete(s[0].identifier),document.removeEventListener("touchmove",c),document.removeEventListener("touchend",u)};o.addEventListener("touchstart",r);let a=()=>{let m=this.activeTouches.get(0).clientY-this.activeTouches.get(1).clientY,l=this.activeTouches.get(0).clientX-this.activeTouches.get(1).clientX;return(F(Math.atan2(m,l))+360+90)%360};return{remove(){o.removeEventListener("touchstart",r)}}}},N=new f});var g,b,O=v(()=>{g=class i{constructor(){this.actions=[],this.keyboardFunctions=[],this.gamepadFunctions=[]}init(){window._IM||(window._IM=new i)}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(o=>e.includes(o))&&n.type===t&&n.actions.length===e.length)}getGamepadActions(e){return _IM.gamepadFunctions.filter(t=>t.actions.every(n=>e.includes(n))&&t.actions.length===e.length)}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)}},b=new g});var D=W((U,w)=>{P();O();b.init();w.exports=N});return D();})(); //# sourceMappingURL=touch-gestures.min.js.map