/
touch_zoom_rotate.js
219 lines (183 loc) · 6.69 KB
/
touch_zoom_rotate.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
'use strict';
const DOM = require('../../util/dom');
const util = require('../../util/util');
const window = require('../../util/window');
const inertiaLinearity = 0.15,
inertiaEasing = util.bezier(0, 0, inertiaLinearity, 1),
inertiaDeceleration = 12, // scale / s^2
inertiaMaxSpeed = 2.5, // scale / s
significantScaleThreshold = 0.15,
significantRotateThreshold = 4;
/**
* The `TouchZoomRotateHandler` allows the user to zoom and rotate the map by
* pinching on a touchscreen.
*
* @param {Map} map The Mapbox GL JS map to add the handler to.
*/
class TouchZoomRotateHandler {
constructor(map) {
this._map = map;
this._el = map.getCanvasContainer();
util.bindAll([
'_onStart',
'_onMove',
'_onEnd'
], this);
}
/**
* Returns a Boolean indicating whether the "pinch to rotate and zoom" interaction is enabled.
*
* @returns {boolean} `true` if the "pinch to rotate and zoom" interaction is enabled.
*/
isEnabled() {
return !!this._enabled;
}
/**
* Enables the "pinch to rotate and zoom" interaction.
*
* @param {Object} [options]
* @param {string} [options.around] If "center" is passed, map will zoom around the center
*
* @example
* map.touchZoomRotate.enable();
* @example
* map.touchZoomRotate.enable({ around: 'center' });
*/
enable(options) {
if (this.isEnabled()) return;
this._el.addEventListener('touchstart', this._onStart, false);
this._enabled = true;
this._aroundCenter = options && options.around === 'center';
}
/**
* Disables the "pinch to rotate and zoom" interaction.
*
* @example
* map.touchZoomRotate.disable();
*/
disable() {
if (!this.isEnabled()) return;
this._el.removeEventListener('touchstart', this._onStart);
this._enabled = false;
}
/**
* Disables the "pinch to rotate" interaction, leaving the "pinch to zoom"
* interaction enabled.
*
* @example
* map.touchZoomRotate.disableRotation();
*/
disableRotation() {
this._rotationDisabled = true;
}
/**
* Enables the "pinch to rotate" interaction.
*
* @example
* map.touchZoomRotate.enable();
* map.touchZoomRotate.enableRotation();
*/
enableRotation() {
this._rotationDisabled = false;
}
_onStart(e) {
if (e.touches.length !== 2) return;
const p0 = DOM.mousePos(this._el, e.touches[0]),
p1 = DOM.mousePos(this._el, e.touches[1]);
this._startVec = p0.sub(p1);
this._startScale = this._map.transform.scale;
this._startBearing = this._map.transform.bearing;
this._gestureIntent = undefined;
this._inertia = [];
window.document.addEventListener('touchmove', this._onMove, false);
window.document.addEventListener('touchend', this._onEnd, false);
}
_onMove(e) {
if (e.touches.length !== 2) return;
const p0 = DOM.mousePos(this._el, e.touches[0]),
p1 = DOM.mousePos(this._el, e.touches[1]),
p = p0.add(p1).div(2),
vec = p0.sub(p1),
scale = vec.mag() / this._startVec.mag(),
bearing = this._rotationDisabled ? 0 : vec.angleWith(this._startVec) * 180 / Math.PI,
map = this._map;
// Determine 'intent' by whichever threshold is surpassed first,
// then keep that state for the duration of this gesture.
if (!this._gestureIntent) {
const scalingSignificantly = (Math.abs(1 - scale) > significantScaleThreshold),
rotatingSignificantly = (Math.abs(bearing) > significantRotateThreshold);
if (rotatingSignificantly) {
this._gestureIntent = 'rotate';
} else if (scalingSignificantly) {
this._gestureIntent = 'zoom';
}
if (this._gestureIntent) {
this._startVec = vec;
this._startScale = map.transform.scale;
this._startBearing = map.transform.bearing;
}
} else {
const param = { duration: 0, around: map.unproject(p) };
if (this._gestureIntent === 'rotate') {
param.bearing = this._startBearing + bearing;
}
if (this._gestureIntent === 'zoom' || this._gestureIntent === 'rotate') {
param.zoom = map.transform.scaleZoom(this._startScale * scale);
}
map.stop();
this._drainInertiaBuffer();
this._inertia.push([Date.now(), scale, p]);
map.easeTo(param, { originalEvent: e });
}
e.preventDefault();
}
_onEnd(e) {
window.document.removeEventListener('touchmove', this._onMove);
window.document.removeEventListener('touchend', this._onEnd);
this._drainInertiaBuffer();
const inertia = this._inertia,
map = this._map;
if (inertia.length < 2) {
map.snapToNorth({}, { originalEvent: e });
return;
}
const last = inertia[inertia.length - 1],
first = inertia[0],
lastScale = map.transform.scaleZoom(this._startScale * last[1]),
firstScale = map.transform.scaleZoom(this._startScale * first[1]),
scaleOffset = lastScale - firstScale,
scaleDuration = (last[0] - first[0]) / 1000,
p = last[2];
if (scaleDuration === 0 || lastScale === firstScale) {
map.snapToNorth({}, { originalEvent: e });
return;
}
// calculate scale/s speed and adjust for increased initial animation speed when easing
let speed = scaleOffset * inertiaLinearity / scaleDuration; // scale/s
if (Math.abs(speed) > inertiaMaxSpeed) {
if (speed > 0) {
speed = inertiaMaxSpeed;
} else {
speed = -inertiaMaxSpeed;
}
}
const duration = Math.abs(speed / (inertiaDeceleration * inertiaLinearity)) * 1000;
let targetScale = lastScale + speed * duration / 2000;
if (targetScale < 0) {
targetScale = 0;
}
map.easeTo({
zoom: targetScale,
duration: duration,
easing: inertiaEasing,
around: this._aroundCenter ? map.getCenter() : map.unproject(p)
}, { originalEvent: e });
}
_drainInertiaBuffer() {
const inertia = this._inertia,
now = Date.now(),
cutoff = 160; // msec
while (inertia.length > 2 && now - inertia[0][0] > cutoff) inertia.shift();
}
}
module.exports = TouchZoomRotateHandler;