hammerjs
Version:
A javascript library for multi-touch gestures
56 lines (48 loc) • 1.64 kB
JavaScript
/**
* Tap/DoubleTap
* Quick touch at a place or double at the same place
* @events tap, doubletap
*/
Hammer.gestures.Tap = {
name : 'tap',
index : 100,
defaults: {
tap_max_touchtime : 250,
tap_max_distance : 10,
tap_always : true,
doubletap_distance: 20,
doubletap_interval: 300
},
has_moved: false,
handler : function tapGesture(ev, inst) {
var prev, since_prev, did_doubletap;
// reset moved state
if(ev.eventType == EVENT_START) {
this.has_moved = false;
}
// Track the distance we've moved. If it's above the max ONCE, remember that (fixes #406).
else if(ev.eventType == EVENT_MOVE && !this.moved) {
this.has_moved = (ev.distance > inst.options.tap_max_distance);
}
else if(ev.eventType == EVENT_END &&
ev.srcEvent.type != 'touchcancel' &&
ev.deltaTime < inst.options.tap_max_touchtime && !this.has_moved) {
// previous gesture, for the double tap since these are two different gesture detections
prev = Detection.previous;
since_prev = prev && prev.lastEvent && ev.timeStamp - prev.lastEvent.timeStamp;
did_doubletap = false;
// check if double tap
if(prev && prev.name == 'tap' &&
(since_prev && since_prev < inst.options.doubletap_interval) &&
ev.distance < inst.options.doubletap_distance) {
inst.trigger('doubletap', ev);
did_doubletap = true;
}
// do a single tap
if(!did_doubletap || inst.options.tap_always) {
Detection.current.name = 'tap';
inst.trigger(Detection.current.name, ev);
}
}
}
};