forked from paztis/fingers.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfingers.min.js
10 lines (8 loc) · 14.6 KB
/
fingers.min.js
1
2
3
4
5
6
7
8
9
10
/*! FingersJS - v1.6.0 - 2017-06-16
* https://github.com/vitekr/FingersJS
*
* Copyright (c) 2017 Vít Rusňák <vit.rusnak@gmail.com>;
* Based on original Fingers.js by Jérôme HENAFF <jerome.henaff@gmail.com>
* Licensed under the MIT license */
!function(t,e){"use strict";var i=function t(e){return new t.Instance(e)};i.__extend=function(t){return Array.prototype.slice.call(arguments,1).forEach(function(e){if(e)for(var i in e)t[i]=e[i]}),t};var n={DIRECTION:{UP:"up",DOWN:"down",LEFT:"left",RIGHT:"right"},GROW:{IN:"in",OUT:"out"},PPCM:30,getVelocity:function(t,e){return Math.abs(e/t)||0},getOrientedVelocity:function(t,e){return e/t||0},getDirection:function(t,e){return Math.abs(t)>=Math.abs(e)?t>0?this.DIRECTION.RIGHT:this.DIRECTION.LEFT:e>0?this.DIRECTION.DOWN:this.DIRECTION.UP},isVertical:function(t){return t===this.DIRECTION.UP||t===this.DIRECTION.DOWN},getAngle:function(t,e){return Math.atan2(t,e)},getDistance:function(t,e){return Math.sqrt(t*t+e*e)},setPPCM:function(t){this.PPCM=Math.round(Math.sqrt(screen.width*screen.width+screen.height*screen.height)/t/2.54),console.log(screen.width+"x"+screen.height+"@"+t+"in; PPCM= "+this.PPCM)}};i.Utils=n;var s=function(t){this._init(t)};s.FINGER_MAP={},s.prototype={element:null,gestureList:null,_init:function(t){this.element=t,this.gestureList=[],this.startListening()},getElement:function(){return this.element},getGestures:function(){return this.gestureList},addGesture:function(t,e){var i=new t(e);return this.gestureList.push(i),i},removeGesture:function(t){t.removeAllHandlers();var e=this.gestureList.indexOf(t);this.gestureList.splice(e,1)},removeAllGestures:function(){for(var t=0,e=this.gestureList.length;t<e;t++)this.gestureList[t].removeAllHandlers();this.gestureList.length=0},startListening:function(){if(null===this._stopListeningF){var t=this,e=this._onTouchStart.bind(this),i=this._onTouchMove.bind(this),n=this._onTouchEnd.bind(this),s=this._onTouchCancel.bind(this);this.element.addEventListener("touchstart",e),this.element.addEventListener("touchmove",i),this.element.addEventListener("touchend",n),this.element.addEventListener("touchcancel",s),this._stopListeningF=function(){t.element.removeEventListener("touchstart",e),t.element.removeEventListener("touchmove",i),t.element.removeEventListener("touchend",n),t.element.removeEventListener("touchcancel",s)}}},_stopListeningF:null,stopListening:function(){null!==this._stopListeningF&&(this._stopListeningF(),this._stopListeningF=null)},_onTouchStart:function(t){for(var e=0,i=t.changedTouches.length;e<i;e++){var n=t.changedTouches[e],a=null,h=n.identifier;void 0===s.FINGER_MAP[h]?(a=new r(h,Date.now(),n.pageX,n.pageY,this.getElement().id),s.FINGER_MAP[h]=a):a=s.FINGER_MAP[h];for(var o=0,l=this.gestureList.length;o<l;o++)this.gestureList[o]._onFingerAdded(a)}t.preventDefault()},_onTouchMove:function(t){for(var e,i=0,n=t.changedTouches.length;i<n;i++){e=t.changedTouches[i];var r=s.FINGER_MAP[e.identifier];void 0!==r&&r._setCurrentP(Date.now(),e.pageX,e.pageY,!1)}t.preventDefault()},_onTouchEnd:function(t){var e=s.FINGER_MAP[t.changedTouches[0].identifier];void 0!==e&&(e._setEndP(Date.now()),e._clearHandlerObjects(),delete s.FINGER_MAP[e.id]),t.preventDefault()},_onTouchCancel:function(t){for(var e=0,i=t.changedTouches.length;e<i;e++){var n=s.FINGER_MAP[t.changedTouches[e].identifier];if(void 0!==n&&-1!==this.fingerList.indexOf(n)){n._setEndP(Date.now()),n._clearHandlerObjects(),delete s.FINGER_MAP[n.id];break}}t.preventDefault()}},i.Instance=s;var r=function(t,e,i,n,s){this.id=t,this.state=r.STATE.ACTIVE,this.target=s,this._handlerList=[],this.startP=new a(e,i,n),this.previousP=new a(e,i,n),this.currentP=new a(e,i,n)};r.STATE={ACTIVE:"active",REMOVED:"removed"},r.CONSTANTS={inactivityTime:100},r.prototype={id:null,state:null,startP:null,previousP:null,currentP:null,_handlerList:null,_addHandlerObject:function(t){this._handlerList.push(t)},_clearHandlerObjects:function(){this._handlerList.length=0},_removeHandlerObject:function(t){var e=this._handlerList.indexOf(t);this._handlerList.splice(e,1)},_setCurrentP:function(t,e,i,n){if(this.getX()!=e||this.getY()!=i||n){this.previousP.copy(this.currentP),this.currentP.set(t,e,i);for(var s=0;s<this._handlerList.length;s++)this._handlerList[s]._onFingerUpdate(this)}},_setEndP:function(t){t-this.getTime()>r.CONSTANTS.inactivityTime&&this._setCurrentP(t,this.getX(),this.getY(),!0),this.state=r.STATE.REMOVED;for(var e=this._handlerList.slice(0),i=0;i<e.length;i++)e[i]._onFingerRemoved(this)},getTime:function(){return this.currentP.timestamp},getDeltaTime:function(){return this.currentP.timestamp-this.previousP.timestamp},getTotalTime:function(){return this.currentP.timestamp-this.startP.timestamp},getInactivityTime:function(){return Date.now()-this.currentP.timestamp},getX:function(){return this.currentP.x},getY:function(){return this.currentP.y},getTarget:function(){return this.target},getDeltaX:function(){return this.currentP.x-this.previousP.x},getDeltaY:function(){return this.currentP.y-this.previousP.y},getDeltaDistance:function(){return n.getDistance(this.getDeltaX(),this.getDeltaY())},getTotalX:function(){return this.currentP.x-this.startP.x},getTotalY:function(){return this.currentP.y-this.startP.y},getDistance:function(){return n.getDistance(this.getTotalX(),this.getTotalY())},getDeltaDirection:function(){return n.getDirection(this.getDeltaX(),this.getDeltaY())},getDirection:function(){return n.getDirection(this.getTotalX(),this.getTotalY())},getVelocityX:function(){return n.getVelocity(this.getDeltaTime(),this.getDeltaX())},getVelocityY:function(){return n.getVelocity(this.getDeltaTime(),this.getDeltaY())},getVelocity:function(){return n.getVelocity(this.getDeltaTime(),this.getDeltaDistance())},getVelocityAverage:function(){return n.getVelocity(this.getTotalTime(),this.getDistance())},getOrientedVelocityX:function(){return n.getOrientedVelocity(this.getDeltaTime(),this.getDeltaX())},getOrientedVelocityY:function(){return n.getOrientedVelocity(this.getDeltaTime(),this.getDeltaY())}},i.Finger=r;var a=function(t,e,i){this.set(t,e,i)};a.prototype={timestamp:null,x:null,y:null,set:function(t,e,i){this.timestamp=t,this.x=e,this.y=i},copy:function(t){this.timestamp=t.timestamp,this.x=t.x,this.y=t.y}},i.Position=a;var h={getFingersAngle:function(t,e){return n.getAngle(e.currentP.x-t.currentP.x,e.currentP.y-t.currentP.y)},getFingersDistance:function(t,e){return n.getDistance(e.currentP.x-t.currentP.x,e.currentP.y-t.currentP.y)},getFingersCenter:function(t,e){return{x:~~((t.currentP.x+e.currentP.x)/2),y:~~((t.currentP.y+e.currentP.y)/2)}},getMultipleFingersCenter:function(t,e,i,n,s){for(var r={x:0,y:0},a=arguments.length,h=0;h<a;h++)r.x+=arguments[h].currentP.x,r.y+=arguments[h].currentP.y;return r.x=~~(r.x/a),r.y=~~(r.y/a),r}};i.FingerUtils=h;var o=function(t,e){this.options=i.__extend({},e||{},t||{}),this._handlerList=[],this.listenedFingers=[]};o.EVENT_TYPE={instant:"instant",start:"start",end:"end",move:"move"},o.prototype={options:null,_handlerList:null,isListening:!1,listenedFingers:null,addHandler:function(t){return this._handlerList.push(t),this},removeHandler:function(t){return this._removeHandlerObject(t),this},removeAllHandlers:function(){return this._handlerList.length=0,this},getHandlers:function(){return this._handlerList},getHandler:function(t){var e=this._handlerList.indexOf(t);return this._handlerList[e]},fire:function(t,e){for(var i=0,n=this._handlerList.length;i<n;i++)this._handlerList[i](t,e,this.listenedFingers)},_onFingerAdded:function(t){},_onFingerUpdate:function(t){},_onFingerRemoved:function(t){},_addListenedFinger:function(t){this.listenedFingers.push(t),t._addHandlerObject(this),this.isListening||(this.isListening=!0)},_removeListenedFinger:function(t){t._removeHandlerObject(this),this.listenedFingers.length=0,this.isListening=!1},_removeAllListenedFingers:function(){for(var t=0,e=this.listenedFingers.length;t<e;t++)this.listenedFingers[t],this.listenedFingers[t]._removeHandlerObject(this);this.listenedFingers.length=0,this.isListening=!1}},i.Gesture=o,i.gesture={},i.gesture={};var l=function(t){function e(e){t.call(this,e,s)}var s={distanceThreshold:.3};return i.__extend(e.prototype,t.prototype,{_onFingerAdded:function(e){this.isListening?this.listenedFingers.length>0&&this._removeListenedFinger(this.listenedFingers[0]):0===this.listenedFingers.length?(this._addListenedFinger(e),this.fire(t.EVENT_TYPE.start,null)):this._removeListenedFinger(this.listenedFingers[0])},_onFingerUpdate:function(e){var i=this.options.distanceThreshold*n.PPCM;e.getDeltaDistance()>i&&this.fire(t.EVENT_TYPE.move,null)},_onFingerRemoved:function(e){this.fire(t.EVENT_TYPE.end,null),this._removeListenedFinger(e)}}),e}(i.Gesture);i.gesture.Drag=l;var d=function(t){function e(e){t.call(this,e,s),this._onHoldTimeLeftF=this._onHoldTimeLeft.bind(this),this.data={target:"null"}}var s={distanceThreshold:.8,duration:600};return i.__extend(e.prototype,t.prototype,{timer:null,_onFingerAdded:function(t){this.isListening?1===this.listenedFingers.length&&(this.listenedFingers[0]._removeHandlerObject(this),this._onHoldCancel(),this.listenedFingers.length=0,this.isListening=!1):0===this.listenedFingers.length?(this._addListenedFinger(t),clearTimeout(this.timer),this.timer=setTimeout(this._onHoldTimeLeftF,this.options.duration),this.data.target=t.getTarget()):(this.listenedFingers[0]._removeHandlerObject(this),this.listenedFingers.length=0,clearTimeout(this.timer))},_onFingerUpdate:function(t){var e=this.options.distanceThreshold*n.PPCM;this.listenedFingers.length>0&&this.listenedFingers[0].getDistance()>e&&(this._onHoldCancel(),t._removeHandlerObject(this))},_onFingerRemoved:function(t){t._removeHandlerObject(this),this._onHoldCancel()},_onHoldTimeLeftF:null,_onHoldTimeLeft:function(){this.fire(t.EVENT_TYPE.instant,this.data),this._onHoldCancel()},_onHoldCancel:function(){this.listenedFingers.length=0,this.isListening=!1,clearTimeout(this.timer)}}),e}(i.Gesture);i.gesture.Hold=d;var c=function(t){function e(e){t.call(this,e,s),this.data={grow:null,scale:1,target:null}}var s={pinchInDetect:.6,pinchOutDetect:1.4};return i.__extend(e.prototype,t.prototype,{_startDistance:0,data:null,_onFingerAdded:function(e){this.isListening?this._removeAllListenedFingers():this.listenedFingers.length<3?(this._addListenedFinger(e),this.isListening=!1):"active"===this.listenedFingers[0].state&&"active"===this.listenedFingers[1].state&&"active"===this.listenedFingers[2].state?(this._addListenedFinger(e),this._startDistance=this._getFingersDistance(),this.data.target=e.getTarget(),this.fire(t.EVENT_TYPE.start,this.data)):this._removeAllListenedFingers()},_onFingerUpdate:function(t){},_onFingerRemoved:function(e){switch(this.listenedFingers.length){case 1:case 2:case 3:e._removeHandlerObject(this),this.listenedFingers.length=0,this.isListening=!1;break;case 4:var i=this._getFingersDistance()/this._startDistance;(i<=this.options.pinchInDetect||i>=this.options.pinchOutDetect)&&(this.data.grow=i>1?n.GROW.OUT:n.GROW.IN,this.data.scale=i,this.fire(t.EVENT_TYPE.instant,this.data));break;default:this._removeAllListenedFingers()}},_getFingersDistance:function(){var t=this.listenedFingers[0].currentP,e=this.listenedFingers[1].currentP;return i.Utils.getDistance(e.x-t.x,e.y-t.y)}}),e}(i.Gesture);i.gesture.Pinch=c;var g=function(t){function e(e){t.call(this,e,s),this.data={nbTap:0,lastTapTimestamp:0,tapPosition:[0,0],target:null}}var s={nbFingers:1,nbTapMin:1,nbTapMax:1,tapInterval:200,distanceThreshold:.6};return i.__extend(e.prototype,t.prototype,{data:null,_distance:0,_threshold:0,_delay:0,_onFingerAdded:function(t){this._delay=t.getTime()-this.data.lastTapTimestamp,this._delay>this.options.tapInterval?(this.data.lastTapTimestamp=t.getTime(),this.data.nbTap=0,this._threshold=this.options.distanceThreshold*n.PPCM,this.data.tapPosition=[t.getX(),t.getY()],this.data.target=t.getTarget()):this._distance=i.Utils.getDistance(t.getX()-this.data.tapPosition[0],t.getY()-this.data.tapPosition[1]),this._addListenedFinger(t)},_onFingerUpdate:function(t){t.getDistance()>this._threshold&&this._removeListenedFinger(t)},_onFingerRemoved:function(e){this._removeListenedFinger(e),this.data.nbTap++,this._delay<this.options.tapInterval&&this._distance<this._threshold&&this.data.nbTap>=this.options.nbTapMin&&this.data.nbTap<=this.options.nbTapMax&&this.fire(t.EVENT_TYPE.instant,this.data)}}),e}(i.Gesture);i.gesture.Tap=g;var u=function(t){function e(e){t.call(this,e,s),this.data={totalRotation:0,deltaRotation:0,totalDistance:0,deltaDistance:0,totalScale:1,deltaScale:1,scale:!0,rotate:!0,target:null}}var s={distanceThreshold:5,angleThreshold:.1};return i.__extend(e.prototype,t.prototype,{_startAngle:0,_lastAngle:0,_startDistance:0,_lastDistance:0,_threshold:1,data:null,_onFingerAdded:function(e){this.isListening?this._removeAllListenedFingers():0===this.listenedFingers.length?(this._addListenedFinger(e),this.isListening=!1,this.options.scale=!0,this.options.rotate=!0):"active"===this.listenedFingers[0].state?(this._addListenedFinger(e),this._lastAngle=this._getFingersAngle(),this._startAngle=this._lastAngle,this._lastDistance=this._getFingersDistance(),this._startDistance=this._lastDistance,this.data.target=e.getTarget(),this.fire(t.EVENT_TYPE.start,this.data)):this._removeAllListenedFingers()},_onFingerUpdate:function(e){if(2===this.listenedFingers.length){var i=this.options.distanceThreshold*n.PPCM,s=this._getFingersAngle();this.data.totalRotation=this._startAngle-s,this.data.deltaRotation=this._lastAngle-s,this._lastAngle=s;var r=this._getFingersDistance();this.data.totalScale=r/this._startDistance,this.data.deltaScale=r/this._lastDistance,this.data.totalDistance=this._startDistance-r,this.data.deltaDistance=this._lastDistance-r,this._lastDistance=r;var a=(this.data.totalRotation^this.data.totalRotation>>31)-(this.data.totalRotation>>31),h=(this.data.totalDistance^this.data.totalDistance>>31)-(this.data.totalDistance>>31);this.data.rotate&&a>this.options.angleThreshold&&(this.data.scale=!1),this.data.scale&&h>i&&(this.data.rotate=!1),this.data.rotate&&this.data.scale||this.fire(t.EVENT_TYPE.move,this.data)}},_onFingerRemoved:function(e){switch(this.listenedFingers.length){case 1:e._removeHandlerObject(this),this.listenedFingers.length=0,this.isListening=!1;break;case 2:this._removeAllListenedFingers(),this.fire(t.EVENT_TYPE.end,this.data),this.data.rotate=this.data.scale=!0;break;default:this._removeAllListenedFingers()}},_getFingersAngle:function(){return i.FingerUtils.getFingersAngle(this.listenedFingers[0],this.listenedFingers[1])},_getFingersDistance:function(){return i.FingerUtils.getFingersDistance(this.listenedFingers[0],this.listenedFingers[1])}}),e}(i.Gesture);i.gesture.Transform=u,"function"==typeof define&&define.amd?define(function(){return i}):"undefined"!=typeof module&&module.exports?module.exports=i:t.Fingers=i}(window);
//# sourceMappingURL=fingers.min.js.map