-
Notifications
You must be signed in to change notification settings - Fork 29
/
doubletap.js
52 lines (46 loc) · 1.44 KB
/
doubletap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Gesture recognizer for the `doubletap` gesture.
*
* Taps happen when an element is pressed and then released.
*/
(function(exports) {
var DOUBLETAP_TIME = 300;
var WIGGLE_THRESHOLD = 10;
/**
* A simple object for storing the position of a pointer.
*/
function PointerPosition(pointer) {
this.x = pointer.clientX;
this.y = pointer.clientY;
}
/**
* calculate the squared distance of the given pointer from this
* PointerPosition's pointer
*/
PointerPosition.prototype.calculateSquaredDistance = function(pointer) {
var dx = this.x - pointer.clientX;
var dy = this.y - pointer.clientY;
return dx*dx + dy*dy;
};
function pointerDown(e) {
var pointers = e.getPointerList();
if (pointers.length != 1) return;
var now = new Date();
if (now - this.lastDownTime < DOUBLETAP_TIME && this.lastPosition && this.lastPosition.calculateSquaredDistance(pointers[0]) < WIGGLE_THRESHOLD * WIGGLE_THRESHOLD) {
this.lastDownTime = 0;
this.lastPosition = null;
var payload = {
};
window._createCustomEvent('gesturedoubletap', e.target, payload);
}
this.lastPosition = new PointerPosition(pointers[0]);
this.lastDownTime = now;
}
/**
* Make the specified element create gesturetap events.
*/
function emitDoubleTaps(el) {
el.addEventListener('pointerdown', pointerDown);
}
exports.Gesture._gestureHandlers.gesturedoubletap = emitDoubleTaps;
})(window);