forked from electron/pdf-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gesture_detector.js
164 lines (148 loc) · 4.87 KB
/
gesture_detector.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* A class that listens for touch events and produces events when these
* touches form gestures (e.g. pinching).
*/
class GestureDetector {
/**
* Constructs a GestureDetector.
* @param {!Element} element The element to monitor for touch gestures.
*/
constructor(element) {
this.element_ = element;
this.element_.addEventListener(
'touchstart', this.onTouchStart_.bind(this), { passive: false });
this.element_.addEventListener(
'touchmove', this.onTouch_.bind(this), { passive: true });
this.element_.addEventListener(
'touchend', this.onTouch_.bind(this), { passive: true });
this.element_.addEventListener(
'touchcancel', this.onTouch_.bind(this), { passive: true });
this.pinchStartEvent_ = null;
this.lastEvent_ = null;
this.listeners_ = new Map([
['pinchstart', []],
['pinchupdate', []],
['pinchend', []]
]);
}
/**
* Add a |listener| to be notified of |type| events.
* @param {string} type The event type to be notified for.
* @param {Function} listener The callback.
*/
addEventListener(type, listener) {
if (this.listeners_.has(type)) {
this.listeners_.get(type).push(listener);
}
}
/**
* Call the relevant listeners with the given |pinchEvent|.
* @private
* @param {!Object} pinchEvent The event to notify the listeners of.
*/
notify_(pinchEvent) {
let listeners = this.listeners_.get(pinchEvent.type);
for (let l of listeners)
l(pinchEvent);
}
/**
* The callback for touchstart events on the element.
* @private
* @param {!TouchEvent} event Touch event on the element.
*/
onTouchStart_(event) {
// We must preventDefault if there is a two finger touch. By doing so
// native pinch-zoom does not interfere with our way of handling the event.
if (event.touches.length == 2) {
event.preventDefault();
this.pinchStartEvent_ = event;
this.lastEvent_ = event;
this.notify_({
type: 'pinchstart',
center: GestureDetector.center_(event)
});
}
}
/**
* The callback for touch move, end, and cancel events on the element.
* @private
* @param {!TouchEvent} event Touch event on the element.
*/
onTouch_(event) {
if (!this.pinchStartEvent_)
return;
// Check if the pinch ends with the current event.
if (event.touches.length < 2 ||
this.lastEvent_.touches.length !== event.touches.length) {
let startScaleRatio = GestureDetector.pinchScaleRatio_(
this.lastEvent_, this.pinchStartEvent_);
let center = GestureDetector.center_(this.lastEvent_);
let endEvent = {
type: 'pinchend',
startScaleRatio: startScaleRatio,
center: center
};
this.pinchStartEvent_ = null;
this.lastEvent_ = null;
this.notify_(endEvent);
return;
}
let scaleRatio = GestureDetector.pinchScaleRatio_(event, this.lastEvent_);
let startScaleRatio = GestureDetector.pinchScaleRatio_(
event, this.pinchStartEvent_);
let center = GestureDetector.center_(event);
this.notify_({
type: 'pinchupdate',
scaleRatio: scaleRatio,
direction: scaleRatio > 1.0 ? 'in' : 'out',
startScaleRatio: startScaleRatio,
center: center
});
this.lastEvent_ = event;
}
/**
* Computes the change in scale between this touch event
* and a previous one.
* @private
* @param {!TouchEvent} event Latest touch event on the element.
* @param {!TouchEvent} prevEvent A previous touch event on the element.
* @return {?number} The ratio of the scale of this event and the
* scale of the previous one.
*/
static pinchScaleRatio_(event, prevEvent) {
let distance1 = GestureDetector.distance_(prevEvent);
let distance2 = GestureDetector.distance_(event);
return distance1 === 0 ? null : distance2 / distance1;
}
/**
* Computes the distance between fingers.
* @private
* @param {!TouchEvent} event Touch event with at least 2 touch points.
* @return {number} Distance between touch[0] and touch[1].
*/
static distance_(event) {
let touch1 = event.touches[0];
let touch2 = event.touches[1];
let dx = touch1.clientX - touch2.clientX;
let dy = touch1.clientY - touch2.clientY;
return Math.sqrt(dx * dx + dy * dy);
}
/**
* Computes the midpoint between fingers.
* @private
* @param {!TouchEvent} event Touch event with at least 2 touch points.
* @return {!Object} Midpoint between touch[0] and touch[1].
*/
static center_(event) {
let touch1 = event.touches[0];
let touch2 = event.touches[1];
return {
x: (touch1.clientX + touch2.clientX) / 2,
y: (touch1.clientY + touch2.clientY) / 2
};
}
};