2 * Hammer.JS - v1.0.5 - 2013-04-07
\r
3 * http://eightmedia.github.com/hammer.js
\r
4 * Jorik Tangelder <j.tangelder@gmail.com>, MIT license
\r
7 ( function( Math, window, document, undefined ){
\r
9 var ELEENT_LIST = [],
\r
12 ABS = new Function( 'v', 'return v<0?-v:v' );
\r
14 X.UI.Gesture = Hammer;
\r
16 function Hammer( uinodeRoot, uinode, type ){
\r
17 this.uinode = uinode;
\r
18 this.enabled = true;
\r
20 Hammer.startup && Hammer.startup( uinodeRoot );
\r
22 this.options = Hammer.defaults;
\r
24 // start detection on touchstart
\r
25 Utils.addEvents( uinode, Hammer.EVENT_TYPES_START, this );
\r
27 this.listen( type );
\r
30 Hammer.defaults = {};
\r
32 Hammer.prototype.handleEvent = function( e ){
\r
33 //var sourceEventType = e.type.toLowerCase();
\r
35 var type = IdToGestureID[ e.type ],
\r
36 gestures = Detection.gestures,
\r
37 numTouches = 0,// count the total touches on the screen
\r
38 pointerType, i, l, touches, ret, active, gesture, startEv,
\r
39 hammer, deltaTime, deltaX, deltaY, velocity;
\r
43 if( e.pointerType ){
\r
45 switch( e.pointerType ){
\r
47 case 2 : //e.MSPOINTER_TYPE_TOUCH :
\r
48 type |= TOUCH; break;
\r
50 case 3 : //e.MSPOINTER_TYPE_PEN :
\r
53 case 4 : //e.MSPOINTER_TYPE_MOUSE :
\r
54 type |= MOUSE; break;
\r
65 // onmouseup, but when touchend has been fired we do nothing.
\r
66 // this is for touchdevices which also fire a mouseup on touchend
\r
67 if( type & MOUSE && touch_triggered ){
\r
68 return X.Callback.STOP_NOW | X.Callback.STOP_PROPAGATION | X.Callback.PREVENT_DEFAULT;
\r
70 // mousebutton must be down or a touch event
\r
72 type & TOUCH || //sourceEventType.match(/touch/) || // touch events are always on screen
\r
73 ( type & POINTER && type & START ) || //sourceEventType.match(/pointerdown/) || // pointerevents touch
\r
74 ( type & MOUSE && e.which === 1 ) //(sourceEventType.match(/mouse/) && e.which === 1) // mouse is pressed
\r
76 enable_detect = true;
\r
79 // we are in a touch event, set the touch triggered bool to true,
\r
80 // this for the conflicts that may occur on ios and android
\r
81 type & ( TOUCH | POINTER ) && ( touch_triggered = true );
\r
82 //if (sourceEventType.match(/touch|pointer/)) { touch_triggered = true;}
\r
84 // when touch has been triggered in this detection session
\r
85 // and we are now handling a mouse event, we stop that to prevent conflicts
\r
86 if( enable_detect ){
\r
87 // update pointerevent
\r
88 if( Hammer.HAS_POINTEREVENTS ){ //eventType !== Hammer.EVENT_END ){
\r
89 POINTERS[ e.identifier = e.pointerId ] = type & END ? null : e;
\r
91 // we can use forEach since pointerEvents only is in IE10
\r
92 for( i = 0, l = POINTERS.length; i < l; ++i ){
\r
93 POINTERS[ i ] && ( touches[ touches.length ] = POINTERS[ i ] );
\r
95 numTouches = touches.length;
\r
98 if ( type & TOUCH ){ //sourceEventType.match(/touch/)) {
\r
99 touches = Hammer.DO_TOUCHES_FIX && type & END ? [] : e.touches;
\r
100 numTouches = touches.length;
\r
103 if( !touch_triggered ){
\r
104 numTouches = ( type & END ) ? 0 : 1;
\r
105 touches = numTouches === 0 ? [] : [{
\r
112 // if we are in a end event, but when we remove one touch and
\r
113 // we still have enough, set eventType to move
\r
114 if( 0 < numTouches && type & END ){ // eventType === Hammer.EVENT_END ){
\r
115 type = type & POINTER_TYPE_MASK | MOVE;
\r
116 //eventType = Hammer.EVENT_MOVE;
\r
117 } else if( !numTouches ){
\r
118 // no touches, force the end event
\r
119 type = type & POINTER_TYPE_MASK | END;
\r
120 //eventType = Hammer.EVENT_END;
\r
123 // because touchend has no touches, and we often want to use these in our gestures,
\r
124 // we send the last move event as our eventData in touchend
\r
125 ( !numTouches && last_move_event !== null ) ?
\r
126 ( e = last_move_event ):
\r
127 ( last_move_event = e ); // store the last move event
\r
130 center : Utils.getCenter( touches ),
\r
131 timeStamp : Date.now ? Date.now() : +new Date,
\r
134 eventType : type & EVENT_TYPE_MASK,
\r
135 pointerType : type & POINTER_TYPE_MASK
\r
138 if( type & START ){
\r
139 if( !this.enabled ) return;
\r
140 // already busy with a Hammer.gesture detection on an element
\r
141 if( Detection.current ) return;
\r
142 Detection.current = {
\r
143 hammer : this, // reference to HammerInstance we're working for
\r
144 startEvent : Utils.extend( {}, e ), // start eventData for distances, timing etc
\r
145 lastEvent : false, // last eventData
\r
146 name : '' // current gesture we're in/detected, can be 'tap', 'hold' etc
\r
148 Detection.stopped = false;
\r
150 active = hammer.activeGesture;
\r
152 if( !Detection.current || Detection.stopped ){
\r
155 hammer = Detection.current.hammer;
\r
156 active = hammer.activeGesture;
\r
159 // ----------------------------------------------------------------------------------------------------------------
\r
160 // ret = Detection.detect( e );
\r
162 // ----------------------------------------------------------------------------------------------------------------
\r
163 // extend event data with calculations about scale, distance etc
\r
164 // e = Detection.extendEventData( e );
\r
165 startEv = Detection.current.startEvent;
\r
168 // if the touches change, set the new touches over the startEvent touches
\r
169 // this because touchevents don't have all the touches on touchstart, or the
\r
170 // user must place his fingers at the EXACT same time on the screen, which is not realistic
\r
171 // but, sometimes it happens that both fingers are touching at the EXACT same time
\r
172 if( startEv && ( numTouches !== startEv.touches.length || touches === startEv.touches ) ){
\r
173 // extend 1 level deep to get the touchlist with the touch objects
\r
174 startEv.touches.length = i = 0;
\r
175 for( ; i < numTouches; ++i ){
\r
176 startEv.touches[ startEv.touches.length ] = Utils.extend( {}, touches[ i ] );
\r
180 deltaTime = e.timeStamp - startEv.timeStamp;
\r
181 deltaX = center.pageX - startEv.center.pageX;
\r
182 deltaY = center.pageY - startEv.center.pageY;
\r
183 velocity = Utils.getVelocity( deltaTime, deltaX, deltaY );
\r
186 deltaTime : deltaTime,
\r
191 velocityX : velocity.x,
\r
192 velocityY : velocity.y,
\r
194 distance : Utils.getDistance( startEv.center, center ),
\r
195 angle : Utils.getAngle( startEv.center, center ),
\r
196 direction : Utils.getDirection( startEv.center, center ),
\r
198 scale : Utils.getScale( startEv.touches, touches ),
\r
199 rotation : Utils.getRotation( startEv.touches, touches ),
\r
201 startEvent : startEv
\r
204 // store as previous event event
\r
205 Detection.current.lastEvent = e;
\r
207 // call Hammer.gesture handlers
\r
208 for( i = 0, l = gestures.length; i < l; ++i ){
\r
209 gesture = gestures[ i ];
\r
210 if( Detection.stopped ) break;
\r
211 //if( active[ gesture.name ] ) console.log( gesture.name );
\r
212 // only when the instance options have enabled this gesture
\r
213 active[ gesture.name ] &&
\r
214 // if a handler returns false, we stop with the detection
\r
215 ( ret |= ( gesture.handler.call( gesture, e, hammer ) || X.Callback.NONE ) );
\r
218 // endevent, but not the last touch, so dont stop
\r
219 type & END && numTouches === 0 && Detection.stopDetect();
\r
221 // ----------------------------------------------------------------------------------------------------------------
\r
222 // trigger the handler
\r
223 //handler.call( context, HamEvent.collectEventData( element, eventType, e ) );
\r
225 // remove pointerevent from list
\r
226 if( Hammer.HAS_POINTEREVENTS && type & END ){ // eventType === Hammer.EVENT_END ){
\r
231 //debug(sourceEventType +" "+ eventType);
\r
233 // on the end we reset everything
\r
234 if( numTouches === 0 ){
\r
235 last_move_event = null;
\r
236 enable_detect = false;
\r
237 touch_triggered = false;
\r
238 POINTERS.length = 0;
\r
244 Hammer.startup = function( uinodeRoot ){
\r
245 // find what eventtypes we add listeners to
\r
247 * we have different events for each device/browser
\r
248 * determine what we need and set them in the Hammer.EVENT_TYPES constant
\r
250 // determine the eventtype we want to set
\r
251 // for non pointer events browsers and mixed browsers,
\r
252 // like chrome on windows8 touch laptop
\r
255 // Register all gestures inside Gestures
\r
256 for( name in Gestures ){
\r
257 //Gestures.hasOwnProperty( name ) &&
\r
258 Detection.register( Gestures[ name ] );
\r
261 if( X.Dom.EVENT_POINTER ){
\r
262 Hammer.EVENT_TYPES_START = [ X.UI.Event._POINTER_DOWN ];
\r
263 types = [ X.UI.Event._POINTER_MOVE, X.UI.Event._POINTER_UP, X.UI.Event._POINTER_CANCEL ];
\r
265 if( X.Dom.EVENT_TOUCH ){
\r
266 Hammer.EVENT_TYPES_START = [ X.UI.Event._TOUCH_START ];
\r
267 types = [ X.UI.Event._TOUCH_MOVE, X.UI.Event._MOUSE_MOVE, X.UI.Event._TOUCH_END, X.UI.Event._TOUCH_CANCEL ];
\r
269 Hammer.EVENT_TYPES_START = [ X.UI.Event._MOUSE_DOWN ];
\r
270 types = [ X.UI.Event._MOUSE_MOVE, X.UI.Event._MOUSE_UP, X.UI.Event._MOUSE_CANCEL ];
\r
273 // Add touch events on the document
\r
274 Utils.addEvents( uinodeRoot, types, Hammer.prototype.handleEvent );
\r
276 // Hammer is ready...!
\r
277 delete Hammer.startup;
\r
280 Hammer.prototype.trigger = function( type, gesture ){
\r
281 if( !this.types[ type ] ) return;
\r
282 var e = Utils.extend( {}, gesture );
\r
284 return this.uinode.dispatch( e );
\r
287 Hammer.prototype.listen = function( type ){
\r
288 var gestures = Detection.gestures,
\r
289 i = gestures.length, g;
\r
291 g = gestures[ --i ];
\r
292 if( g.startID <= type && type <= g.endID ){
\r
293 if( !this.activeGesture ) this.activeGesture = {};
\r
294 if( !this.types ) this.types = {};
\r
295 this.activeGesture[ g.name ] = this.types[ type ] = 1;
\r
301 Hammer.prototype.unlisten = function( type ){
\r
302 var gestures = Detection.gestures,
\r
303 i = gestures.length, g;
\r
304 if( !this.activeGesture ) return;
\r
306 g = gestures[ --i ];
\r
307 if( g.startID <= type && type <= g.endID ){
\r
308 if( this.activeGesture[ g.name ] ){
\r
309 if( this.types[ type ] ) delete this.types[ type ];
\r
310 for( i = g.startID; i <= g.endID; ++i ){
\r
311 if( this.types[ i ] ) return;
\r
313 delete this.activeGesture[ g.name ];
\r
321 * "Android version < 2.2" return ev.touches.length === 1 when touchend, others return ev.touches.length === 0
\r
323 Hammer.DO_TOUCHES_FIX = Hammer.HAS_TOUCHEVENTS && X.UA.Android < 2.2;
\r
325 // detect touchevents
\r
326 Hammer.HAS_POINTEREVENTS = navigator.pointerEnabled || navigator.msPointerEnabled;
\r
328 // eventtypes per touchevent (start, move, end)
\r
329 // are filled by HamEvent.determineEventTypes on setup
\r
330 Hammer.EVENT_TYPES_START = null;
\r
332 // direction defines
\r
333 Hammer.DIRECTION_DOWN = 'down';
\r
334 Hammer.DIRECTION_LEFT = 'left';
\r
335 Hammer.DIRECTION_UP = 'up';
\r
336 Hammer.DIRECTION_RIGHT = 'right';
\r
338 // plugins namespace
\r
339 Hammer.plugins = {};
\r
349 EVENT_TYPE_MASK = START | MOVE | END,
\r
350 POINTER_TYPE_MASK = POINTER | TOUCH | MOUSE | PEN,
\r
351 IdToGestureID = {};
\r
352 IdToGestureID[ X.UI.Event._POINTER_DOWN ] = START;
\r
353 IdToGestureID[ X.UI.Event._POINTER_MOVE ] = MOVE;
\r
354 IdToGestureID[ X.UI.Event._POINTER_UP ] = END;
\r
355 IdToGestureID[ X.UI.Event._POINTER_CANCEL ] = END;
\r
357 IdToGestureID[ X.UI.Event._TOUCH_START ] = START;
\r
358 IdToGestureID[ X.UI.Event._TOUCH_MOVE ] = MOVE;
\r
359 IdToGestureID[ X.UI.Event._TOUCH_END ] = END;
\r
360 IdToGestureID[ X.UI.Event._TOUCH_CANCEL ] = END;
\r
362 IdToGestureID[ X.UI.Event._MOUSE_DOWN ] = START;
\r
363 IdToGestureID[ X.UI.Event._MOUSE_MOVE ] = MOVE;
\r
364 IdToGestureID[ X.UI.Event._MOUSE_UP ] = END;
\r
365 IdToGestureID[ X.UI.Event._MOUSE_CANCEL ] = END;
\r
370 * touch events with mouse fallback
\r
371 * @param {HTMLElement} element
\r
372 * @param {String} eventType like Hammer.EVENT_MOVE
\r
373 * @param {Function} handler
\r
375 addEvents : function( uinode, types, context ){
\r
376 for( var i = 0; i < types.length; ++i ){
\r
377 uinode.listen( types[ i ], context );
\r
383 * also used for cloning when dest is an empty object
\r
384 * @param {Object} dest
\r
385 * @param {Object} src
\r
386 * @parm {Boolean} merge do a merge
\r
387 * @returns {Object} dest
\r
389 extend : function extend( dest, src, merge ){
\r
390 for( var key in src ){
\r
391 if( dest[ key ] !== undefined && merge ) continue;
\r
392 dest[ key ] = src[ key ];
\r
398 * find if a node is in the given parent
\r
399 * used for event delegation tricks
\r
400 * @param {HTMLElement} node
\r
401 * @param {HTMLElement} parent
\r
402 * @returns {boolean} has_parent
\r
404 hasParent : function( node, parent ){
\r
405 while( node && node.tagName ){ /* tagName for ie */
\r
406 if( node === parent ) return true;
\r
407 node = node.parentNode;
\r
413 * get the center of all the touches
\r
414 * @param {Array} touches
\r
415 * @returns {Object} center
\r
417 getCenter : function getCenter(touches) {
\r
419 l = touches.length,
\r
426 pageX : touches[ 0 ].pageX,
\r
427 pageY : touches[ 0 ].pageY
\r
431 pageX : ( touches[ 0 ].pageX + touches[ 1 ].pageX ) / 2,
\r
432 pageY : ( touches[ 0 ].pageY + touches[ 1 ].pageY ) / 2
\r
437 for( ; i < l; ++i ){
\r
438 valuesX[ valuesX.length ] = touches[ i ].pageX;
\r
439 valuesY[ valuesY.length ] = touches[ i ].pageY;
\r
442 pageX : ( ( Math.min.apply( null, valuesX ) + Math.max.apply( null, valuesX ) ) / 2 ),
\r
443 pageY : ( ( Math.min.apply( null, valuesY ) + Math.max.apply( null, valuesY ) ) / 2 )
\r
448 * calculate the velocity between two points
\r
449 * @param {Number} deltaTime
\r
450 * @param {Number} deltaX
\r
451 * @param {Number} deltaY
\r
452 * @returns {Object} velocity
\r
454 getVelocity : function getVelocity( deltaTime, deltaX, deltaY ) {
\r
456 x : ABS( deltaX / deltaTime ) || 0,
\r
457 y : ABS( deltaY / deltaTime ) || 0
\r
462 * calculate the angle between two coordinates
\r
463 * @param {Touch} touch1
\r
464 * @param {Touch} touch2
\r
465 * @returns {Number} angle
\r
467 getAngle : function getAngle(touch1, touch2) {
\r
468 var y = touch2.pageY - touch1.pageY,
\r
469 x = touch2.pageX - touch1.pageX;
\r
470 return Math.atan2( y, x ) * 180 / Math.PI;
\r
474 * angle to direction define
\r
475 * @param {Touch} touch1
\r
476 * @param {Touch} touch2
\r
477 * @returns {String} direction constant, like Hammer.DIRECTION_LEFT
\r
479 getDirection : function getDirection( touch1, touch2 ){
\r
480 var x = touch1.pageX - touch2.pageX,
\r
481 y = touch1.pageY - touch2.pageY;
\r
482 return ABS( y ) <= ABS( x ) ?
\r
483 ( x > 0 ? Hammer.DIRECTION_LEFT : Hammer.DIRECTION_RIGHT ) :
\r
484 ( y > 0 ? Hammer.DIRECTION_UP : Hammer.DIRECTION_DOWN );
\r
488 * calculate the distance between two touches
\r
489 * @param {Touch} touch1
\r
490 * @param {Touch} touch2
\r
491 * @returns {Number} distance
\r
493 getDistance : function getDistance( touch1, touch2 ){
\r
494 var x = touch2.pageX - touch1.pageX,
\r
495 y = touch2.pageY - touch1.pageY;
\r
496 return Math.sqrt( ( x * x ) + ( y * y ) );
\r
500 * calculate the scale factor between two touchLists (fingers)
\r
501 * no scale is 1, and goes down to 0 when pinched together, and bigger when pinched out
\r
502 * @param {Array} start
\r
503 * @param {Array} end
\r
504 * @returns {Number} scale
\r
506 getScale : function getScale( start, end ){
\r
507 // need two fingers...
\r
508 return ( 2 <= start.length && 2 <= end.length ) ?
\r
509 Utils.getDistance( end[ 0 ], end[ 1 ] ) / Utils.getDistance( start[ 0 ], start[ 1 ] ) :
\r
514 * calculate the rotation degrees between two touchLists (fingers)
\r
515 * @param {Array} start
\r
516 * @param {Array} end
\r
517 * @returns {Number} rotation
\r
519 getRotation : function getRotation( start, end ){
\r
520 // need two fingers
\r
521 return ( 2 <= start.length && 2 <= end.length ) ?
\r
522 Utils.getAngle( end[ 1 ], end[ 0 ] ) - Utils.getAngle( start[ 1 ], start[ 0 ] ) :
\r
527 * boolean if the direction is vertical
\r
528 * @param {String} direction
\r
529 * @returns {Boolean} is_vertical
\r
531 isVertical : function isVertical( direction ){
\r
532 return direction === Hammer.DIRECTION_UP || direction === Hammer.DIRECTION_DOWN;
\r
537 * this holds the last move event,
\r
538 * used to fix empty touchend issue
\r
539 * see the onTouch event for an explanation
\r
542 var last_move_event = null;
\r
545 * when the mouse is hold down, this is true
\r
548 var enable_detect = false;
\r
551 * when touch events have been fired, this is true
\r
554 var touch_triggered = false;
\r
557 // contains all registred Gestures in the correct order
\r
560 // data of the current Hammer.gesture detection session
\r
563 // the previous Hammer.gesture session data
\r
564 // is a full clone of the previous gesture.current object
\r
567 // when this becomes true, no gestures are fired
\r
571 * clear the Hammer.gesture vars
\r
572 * this is called on endDetect, but can also be used when a final Hammer.gesture has been detected
\r
573 * to stop other Gestures from being fired
\r
575 stopDetect : function stopDetect() {
\r
576 // clone current data to the store as the previous gesture
\r
577 // used for the double tap gesture, since this is an other gesture detect session
\r
578 Detection.previous = Utils.extend( {}, Detection.current );
\r
580 // reset the current
\r
581 Detection.current = null;
\r
584 Detection.stopped = true;
\r
588 * register new gesture
\r
589 * @param {Object} gesture object, see gestures.js for documentation
\r
590 * @returns {Array} gestures
\r
592 register : function( gesture ){
\r
593 // add an enable gesture options if there is no given
\r
594 var options = gesture.defaults || {},
\r
595 list = Detection.gestures,
\r
596 _index, i = 0, l = list.length, index;
\r
597 if( options[ gesture.name ] === undefined ) options[ gesture.name ] = true;
\r
599 // extend Hammer default options with the Hammer.gesture options
\r
600 Utils.extend( Hammer.defaults, options, true );
\r
603 gesture.index = gesture.index || 1000;
\r
605 // add Hammer.gesture to the list
\r
606 //Detection.gestures.push( gesture );
\r
608 // sort the list by index
\r
609 //Detection.gestures.sort( function( a, b ){
\r
611 // a.index < b.index ? -1 :
\r
612 // a.index > b.index ? 1 : 0;
\r
615 list[ 0 ] = gesture;
\r
618 _index = gesture.index;
\r
619 for( i = 0; i < l; ++i ){
\r
620 index = list[ i ].index;
\r
621 if( i === 0 && _index < index ){
\r
622 list.unshift( gesture );
\r
626 list[ l ] = gesture;
\r
629 if( index <= _index && _index < list[ i + 1 ].index ){
\r
630 list.splice( i, 0, gesture );
\r
637 var Gestures = Gestures || {};
\r
641 * ==============================
\r
644 * --------------------
\r
645 * The object structure of a gesture:
\r
647 * { name: 'mygesture',
\r
650 * mygesture_option: true
\r
652 * handler: function(type, e, inst) {
\r
653 * // trigger gesture event
\r
654 * inst.trigger(this.name, e );
\r
658 * @param {String} name
\r
659 * this should be the name of the gesture, lowercase
\r
660 * it is also being used to disable/enable the gesture per instance config.
\r
662 * @param {Number} [index=1000]
\r
663 * the index of the gesture, where it is going to be in the stack of gestures detection
\r
664 * like when you build an gesture that depends on the drag gesture, it is a good
\r
665 * idea to place it after the index of the drag gesture.
\r
667 * @param {Object} [defaults={}]
\r
668 * the default settings of the gesture. these are added to the instance settings,
\r
669 * and can be overruled per instance. you can also add the name of the gesture,
\r
670 * but this is also added by default (and set to true).
\r
672 * @param {Function} handler
\r
673 * this handles the gesture detection of your custom gesture and receives the
\r
674 * following arguments:
\r
676 * @param {Object} eventData
\r
677 * event data containing the following properties:
\r
678 * timeStamp {Number} time the event occurred
\r
679 * target {HTMLElement} target element
\r
680 * touches {Array} touches (fingers, pointers, mouse) on the screen
\r
681 * pointerType {String} kind of pointer that was used. matches Hammer.POINTER_MOUSE|TOUCH
\r
682 * center {Object} center position of the touches. contains pageX and pageY
\r
683 * deltaTime {Number} the total time of the touches in the screen
\r
684 * deltaX {Number} the delta on x axis we haved moved
\r
685 * deltaY {Number} the delta on y axis we haved moved
\r
686 * velocityX {Number} the velocity on the x
\r
687 * velocityY {Number} the velocity on y
\r
688 * angle {Number} the angle we are moving
\r
689 * direction {String} the direction we are moving. matches Hammer.DIRECTION_UP|DOWN|LEFT|RIGHT
\r
690 * distance {Number} the distance we haved moved
\r
691 * scale {Number} scaling of the touches, needs 2 touches
\r
692 * rotation {Number} rotation of the touches, needs 2 touches *
\r
693 * eventType {String} matches Hammer.EVENT_START|MOVE|END
\r
694 * srcEvent {Object} the source event, like TouchStart or MouseDown *
\r
695 * startEvent {Object} contains the same properties as above,
\r
696 * but from the first touch. this is used to calculate
\r
697 * distances, deltaTime, scaling etc
\r
699 * @param {Hammer.Instance} inst
\r
700 * the instance we are doing the detection for. you can get the options from
\r
701 * the inst.options object and trigger the gesture event by calling inst.trigger
\r
705 * --------------------
\r
706 * inside the handler you can get/set Detection.current. This is the current
\r
707 * detection session. It has the following properties
\r
708 * @param {String} name
\r
709 * contains the name of the gesture we have detected. it has not a real function,
\r
710 * only to check in other gestures if something is detected.
\r
711 * like in the drag gesture we set it to 'drag' and in the swipe gesture we can
\r
712 * check if the current gesture is 'drag' by accessing Detection.current.name
\r
715 * @param {Hammer.Instance} inst
\r
716 * the instance we do the detection for
\r
719 * @param {Object} startEvent
\r
720 * contains the properties of the first gesture detection in this session.
\r
721 * Used for calculations about timing, distance, etc.
\r
724 * @param {Object} lastEvent
\r
725 * contains all the properties of the last gesture detect in this session.
\r
727 * after the gesture detection session has been completed (user has released the screen)
\r
728 * the Detection.current object is copied into Detection.previous,
\r
729 * this is usefull for gestures like doubletap, where you need to know if the
\r
730 * previous gesture was a tap
\r
732 * options that have been set by the instance can be received by calling inst.options
\r
734 * You can trigger a gesture event by calling inst.trigger("mygesture", event).
\r
735 * The first param is the name of your gesture, the second the event argument
\r
738 * Register gestures
\r
739 * --------------------
\r
740 * When an gesture is added to the Gestures object, it is auto registered
\r
741 * at the setup of the first Hammer instance. You can also call Detection.register
\r
742 * manually and pass your gesture object as a param
\r
748 * Touch stays at the same place for x time
\r
749 * @events hold holdend
\r
754 startID : X.UI.Event.HOLD,
\r
755 endID : X.UI.Event.HOLD_END,
\r
757 hold_timeout : 500,
\r
762 handler : function holdGesture( e, hammer ){
\r
763 switch( e.eventType ){
\r
765 // clear any running timers
\r
766 this.timerID && X.Timer.remove( this.timerID );
\r
768 // set the gesture so we can check in the timeout if it still is
\r
769 Detection.current.name = this.name;
\r
770 Gestures.Hold.holding = false;
\r
772 // set timer and if after the timeout it still is hold,
\r
773 // we trigger the hold event
\r
774 this.timerID = X.Timer.add( hammer.options.hold_timeout, 1, Gestures.Hold._onTimer, [ e, hammer ] );
\r
777 // when you move or end we clear the timer
\r
779 if( e.distance <= hammer.options.hold_threshold ) return;
\r
781 this.timerID && X.Timer.remove( this.timerID );
\r
782 if( Gestures.Hold.holding === true ){
\r
783 Gestures.Hold.holding = false;
\r
784 return hammer.trigger( X.UI.Event.HOLD_END, e );
\r
789 _onTimer : function( e, hammer ){
\r
790 if( Detection.current.name === 'hold' ){
\r
791 hammer.trigger( X.UI.Event.HOLD, e );
\r
792 Gestures.Hold.holding = true;
\r
799 * Quick touch at a place or double at the same place
\r
800 * @events tap, doubletap
\r
805 startID : X.UI.Event.TAP,
\r
806 endID : X.UI.Event.DOUBLE_TAP,
\r
808 tap_max_touchtime : 250,
\r
809 tap_max_distance : 10,
\r
811 doubletap_distance : 20,
\r
812 doubletap_interval : 300
\r
814 handler : function tapGesture( e, hammer ){
\r
815 // previous gesture, for the double tap since these are two different gesture detections
\r
816 var prev = Detection.previous;
\r
817 if( e.eventType === END ){
\r
818 // when the touchtime is higher then the max touch time
\r
819 // or when the moving distance is too much
\r
820 if( hammer.options.tap_max_touchtime < e.deltaTime || hammer.options.tap_max_distance < e.distance ) return;
\r
822 // check if double tap
\r
823 if( prev && prev.name === 'tap' && ( e.timeStamp - prev.lastEvent.timeStamp ) < hammer.options.doubletap_interval && e.distance < hammer.options.doubletap_distance ){
\r
824 return hammer.trigger( X.UI.Event.DOUBLE_TAP, e );
\r
827 if( hammer.options.tap_always && Detection.current.name !== 'tap' ){ // EventFire中にalert すると mouseleave で再び呼ばれるのを防ぐ
\r
828 Detection.current.name = 'tap';
\r
829 return hammer.trigger( X.UI.Event.TAP, e );
\r
837 * triggers swipe events when the end velocity is above the threshold
\r
838 * @events swipe, swipeleft, swiperight, swipeup, swipedown
\r
843 startID : X.UI.Event.SWIP,
\r
844 endID : X.UI.Event.SWIP_DOWN,
\r
846 // set 0 for unlimited, but this can conflict with transform
\r
847 swipe_max_touches : 1,
\r
848 swipe_velocity : 0.7
\r
850 handler : function swipeGesture(e, hammer) {
\r
851 if( e.eventType === END ){
\r
853 if( 0 < hammer.options.swipe_max_touches && hammer.options.swipe_max_touches < e.touches.length ) return;
\r
855 // when the distance we moved is too small we skip this gesture
\r
856 // or we can be already in dragging
\r
857 if( hammer.options.swipe_velocity < e.velocityX || hammer.options.swipe_velocity < e.velocityY ){
\r
858 // trigger swipe events
\r
859 hammer.trigger( X.UI.Event.SWIP, e );
\r
861 e.direction === Hammer.DIRECTION_UP ?
\r
862 X.UI.Event.SWIP_UP :
\r
863 e.direction === Hammer.DIRECTION_DOWN ?
\r
864 X.UI.Event.SWIP_DOWN :
\r
865 e.direction === Hammer.DIRECTION_LEFT ?
\r
866 X.UI.Event.SWIP_LEFT :
\r
867 X.UI.Event.SWIP_RIGHT,
\r
877 * Move with x fingers (default 1) around on the page. Blocking the scrolling when
\r
878 * moving left and right is a good practice. When all the drag events are blocking
\r
879 * you disable scrolling on that area.
\r
880 * @events drag, dragstart, dragend, drapleft, dragright, dragup, dragdown
\r
885 startID : X.UI.Event.DRAG,
\r
886 endID : X.UI.Event.DRAG_DOWN,
\r
888 drag_min_distance : 10,
\r
889 // set 0 for unlimited, but this can conflict with transform
\r
890 drag_max_touches : 1,
\r
891 // prevent default browser behavior when dragging occurs
\r
892 // be careful with it, it makes the element a blocking element
\r
893 // when you are using the drag gesture, it is a good practice to set this true
\r
894 drag_block_horizontal : false,
\r
895 drag_block_vertical : false,
\r
896 // drag_lock_to_axis keeps the drag gesture on the axis that it started on,
\r
897 // It disallows vertical directions if the initial direction was horizontal, and vice versa.
\r
898 drag_lock_to_axis : false,
\r
899 // drag lock only kicks in when distance > drag_lock_min_distance
\r
900 // This way, locking occurs only when the distance has become large enough to reliably determine the direction
\r
901 drag_lock_min_distance : 25
\r
904 handler : function dragGesture( e, hammer ){
\r
905 var last_direction;
\r
906 // current gesture isnt drag, but dragged is true
\r
907 // this means an other gesture is busy. now call dragend
\r
908 if( Detection.current.name !== this.name && this.triggered ){
\r
909 hammer.trigger( X.UI.Event.DRAG_END, e );
\r
910 this.triggered = false;
\r
915 if( 0 < hammer.options.drag_max_touches && hammer.options.drag_max_touches < e.touches.length ) return;
\r
917 switch( e.eventType ){
\r
919 this.triggered = false;
\r
923 // when the distance we moved is too small we skip this gesture
\r
924 // or we can be already in dragging
\r
925 if( e.distance < hammer.options.drag_min_distance && Detection.current.name !== this.name ) return;
\r
927 // we are dragging!
\r
928 Detection.current.name = this.name;
\r
930 // lock drag to axis?
\r
931 if( Detection.current.lastEvent.drag_locked_to_axis || ( hammer.options.drag_lock_to_axis && hammer.options.drag_lock_min_distance <= e.distance ) ){
\r
932 e.drag_locked_to_axis = true;
\r
934 last_direction = Detection.current.lastEvent.direction;
\r
935 if( e.drag_locked_to_axis && last_direction !== e.direction ){
\r
936 // keep direction on the axis that the drag gesture started on
\r
937 e.direction = Utils.isVertical( last_direction ) ?
\r
938 ( e.deltaY < 0 ? Hammer.DIRECTION_UP : Hammer.DIRECTION_DOWN ) :
\r
939 ( e.deltaX < 0 ? Hammer.DIRECTION_LEFT : Hammer.DIRECTION_RIGHT );
\r
942 // first time, trigger dragstart event
\r
943 if( !this.triggered ){
\r
944 hammer.trigger( X.UI.Event.DRAG_START, e );
\r
945 this.triggered = true;
\r
948 // trigger normal event
\r
949 hammer.trigger( X.UI.Event.DRAG, e );
\r
951 // direction event, like dragdown
\r
953 e.direction === Hammer.DIRECTION_UP ?
\r
954 X.UI.Event.DRAG_UP :
\r
955 e.direction === Hammer.DIRECTION_DOWN ?
\r
956 X.UI.Event.DRAG_DOWN :
\r
957 e.direction === Hammer.DIRECTION_LEFT ?
\r
958 X.UI.Event.DRAG_LEFT :
\r
959 X.UI.Event.DRAG_RIGHT,
\r
963 // block the browser events
\r
965 ( hammer.options.drag_block_vertical && Utils.isVertical( e.direction ) ) ||
\r
966 ( hammer.options.drag_block_horizontal && !Utils.isVertical( e.direction ) )
\r
967 ) && e.preventDefault();
\r
972 this.triggered && hammer.trigger( X.UI.Event.DRAG_END, e );
\r
973 this.triggered = false;
\r
981 * User want to scale or rotate with 2 fingers
\r
982 * @events transform, transformstart, transformend, pinch, pinchin, pinchout, rotate
\r
984 Gestures.Transform = {
\r
985 name : 'transform',
\r
987 startID : X.UI.Event.TRANSFORM,
\r
988 endID : X.UI.Event.ROTATE,
\r
990 // factor, no scale is 1, zoomin is to 0 and zoomout until higher then 1
\r
991 transform_min_scale : 0.01,
\r
992 // rotation in degrees
\r
993 transform_min_rotation : 1,
\r
994 // prevent default browser behavior when two touches are on the screen
\r
995 // but it makes the element a blocking element
\r
996 // when you are using the transform gesture, it is a good practice to set this true
\r
997 transform_always_block : false
\r
1000 handler : function transformGesture( e, hammer ){
\r
1001 // current gesture isnt drag, but dragged is true
\r
1002 // this means an other gesture is busy. now call dragend
\r
1003 if( Detection.current.name !== this.name && this.triggered ){
\r
1004 hammer.trigger( X.UI.Event.TRANSFORM_END, e );
\r
1005 this.triggered = false;
\r
1009 // atleast multitouch
\r
1010 if( e.touches.length < 2 ) return;
\r
1012 // prevent default when two fingers are on the screen
\r
1013 hammer.options.transform_always_block && e.preventDefault();
\r
1015 switch(e.eventType) {
\r
1017 this.triggered = false;
\r
1021 var scale_threshold = ABS( 1 - e.scale ),
\r
1022 rotation_threshold = ABS( e.rotation );
\r
1024 // when the distance we moved is too small we skip this gesture
\r
1025 // or we can be already in dragging
\r
1026 if( scale_threshold < hammer.options.transform_min_scale && rotation_threshold < hammer.options.transform_min_rotation ) return;
\r
1028 // we are transforming!
\r
1029 Detection.current.name = this.name;
\r
1031 // first time, trigger dragstart event
\r
1032 if( !this.triggered ){
\r
1033 hammer.trigger( X.UI.Event.TRANSFORM_START, e );
\r
1034 this.triggered = true;
\r
1037 hammer.trigger( X.UI.Event.TRANSFORM, e );
\r
1038 // basic transform event
\r
1040 // trigger rotate event
\r
1041 hammer.options.transform_min_rotation < rotation_threshold && hammer.trigger( X.UI.Event.ROTATE, e );
\r
1043 // trigger pinch event
\r
1044 if( scale_threshold > hammer.options.transform_min_scale ){
\r
1045 hammer.trigger( X.UI.Event.PINCH, e );
\r
1046 hammer.trigger( e.scale < 1 ? X.UI.Event.PINCH_IN : X.UI.Event.PINCH_OUT, e );
\r
1051 // trigger dragend
\r
1052 this.triggered && hammer.trigger( X.UI.Event.TRANSFORM_END, e );
\r
1053 this.triggered = false;
\r
1061 * Called as first, tells the user has touched the screen
\r
1064 Gestures.Touch = {
\r
1066 index : -Infinity,
\r
1068 // call preventDefault at touchstart, and makes the element blocking by
\r
1069 // disabling the scrolling of the page, but it improves gestures like
\r
1070 // transforming and dragging.
\r
1071 // be careful with using this, it can be very annoying for users to be stuck
\r
1073 prevent_default : false,
\r
1075 // disable mouse events, so only touch (or pen!) input triggers events
\r
1076 prevent_mouseevents : false
\r
1078 handler : function touchGesture( e, hammer ){
\r
1079 if( hammer.options.prevent_mouseevents && e.pointerType === MOUSE ){
\r
1080 Detection.stopDetect();
\r
1084 hammer.options.prevent_default && e.preventDefault();
\r
1086 e.eventType === START && hammer.trigger( this.name, e );
\r
1092 * Called as last, tells the user has released the screen
\r
1095 Gestures.Release = {
\r
1098 handler : function releaseGesture( e, hammer ){
\r
1099 e.eventType === END && hammer.trigger( this.name, e );
\r
1103 })( Math, window, document );