-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
416 lines (416 loc) · 16.5 KB
/
index.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//var tMitter=function(){function d(a,b){a=a.toLowerCase();a in this._events||(this._events[a]=[]);-1===this._events[a].indexOf(b)&&this._events[a].push(b)}function e(a,b){a?b?delete this._events[a][this._events[a].indexOf(b)]:delete this._events[a]:this._events={}}function f(a,b){if(a&&this._events[a])for(var c=this._events[a].length;c--;)this._events[a][c](b)}return function(a){a._events={};a.on=d;a.off=e;a.trigger=f}}();
import { tmitter } from 'tmitter';
/**
* @constructor
* @param el {htmlElement} the element where the events are going to me detected, you can add this to the body to capture all.
* @param config {object} used to set some settings, read he first 10 lines of the method, and you see the options
* @class
* the events are:
" 'tap'
* 'doubletap'
* 'pinchstart'
* 'pinch'
* 'pinchend'
* 'throw'
* 'throwleft'
* 'throwright'
* 'throwup'
* 'throwdown'
* 'longpress'
* 'touchstart'
* 'touchmove'
* 'touchend'
* 'panstart'
* 'pan'
* 'panend'
* 'rotate'
* 'swipe'
* 'swipeup'
* 'swiperight'
* 'swipedown'
* 'swipeleft'
* ''
*/
export class GestureRecognizer {
constructor(el, config) {
this.lastFingers = [1, 2];
this.el = el;
config = config !== undefined ? config : {};
this.swipeSpeed = config.swipeSpeed || 400;
this.swipePrecision = config.swipePrecision || 6;
this.minPinchDistance = config.minPinchDistance || 1;
this.minSwipeDistance = config.minSwipeDistance || 20;
this.longPressTime = config.longPressTime || 800;
this.longTolerance = config.longTolerance || 5;
this.tapTolerance = config.tapTolerance || 5;
this.panDistance = config.panDistance || 5;
this.doubleTapSpeed = config.doubleTapSpeed || 400;
this.doubleTapDistance = config.doubleTapDistance || 15;
this.minFirstPanDistance = config.minFirstPanDistance || 5;
this.touchMode = !!config.touchMode;
this.events = {
tap: tmitter(),
doubletap: tmitter(),
pinchstart: tmitter(),
pinch: tmitter(),
pinchend: tmitter(),
throw: tmitter(),
throwleft: tmitter(),
throwright: tmitter(),
throwup: tmitter(),
throwdown: tmitter(),
longpress: tmitter(),
touchstart: tmitter(),
touchmove: tmitter(),
touchend: tmitter(),
panstart: tmitter(),
pan: tmitter(),
panend: tmitter(),
rotate: tmitter(),
swipe: tmitter(),
swipeup: tmitter(),
swipedown: tmitter(),
swipeleft: tmitter(),
swiperight: tmitter(),
};
this.finger = {};
this.fingerCount = 0;
this.destroyed = false;
this.lastDistance = undefined;
this.originDistance = 0;
if (this.touchMode) {
el.addEventListener('touchstart', this.touchStartHandler);
}
else {
this.mouseStartHandler = this.mouseStartHandler.bind(this);
this.mouseMoveHandler = this.mouseMoveHandler.bind(this);
this.mouseEndHandler = this.mouseEndHandler.bind(this);
el.addEventListener('mousedown', this.mouseStartHandler);
}
}
touchStartHandler(e) {
this.updateFinger(e.touches, e.timeStamp, e);
if (e.touches.length === 1) {
document.addEventListener('touchmove', this.touchMoveHandler);
document.addEventListener('touchend', this.touchEndHandler);
}
}
;
touchMoveHandler(e) {
this.updateFinger(e.touches, e.timeStamp, e);
}
;
touchEndHandler(e) {
this.updateFinger(e.touches, e.timeStamp, e);
if (!e.touches.length) {
document.removeEventListener('touchmove', this.touchMoveHandler);
document.removeEventListener('touchend', this.touchEndHandler);
}
}
;
mouseStartHandler(e) {
if (this.touchMode)
return;
//this.mouseEventToTouch(e);
this.updateFinger([e], e.timeStamp, e);
document.body.addEventListener('mousemove', this.mouseMoveHandler);
document.body.addEventListener('mouseup', this.mouseEndHandler);
}
;
mouseMoveHandler(e) {
//this.mouseEventToTouch(e);
this.updateFinger([e], e.timeStamp, e);
}
;
mouseEndHandler(e) {
this.updateFinger([], e.timeStamp, e);
document.body.removeEventListener('mousemove', this.mouseMoveHandler);
document.body.removeEventListener('mouseup', this.mouseEndHandler);
}
;
/**
* Method that is called on any touchEvent
* and executes the GestureDetection-methods
*/
updateFinger(finger, time, e) {
if (this.destroyed)
return;
var orgPrevent = e.preventDefault;
e.preventDefault = function () {
orgPrevent.apply(e);
};
var i; // used for loops
var identifier = []; // list of the touch identifirer on the given event
// handle all finger that are currently on the screen
for (i = 0; i < finger.length; i++) {
// copy the touch Object to create compatibility between android and iOS
// because iOS is not creating a new touch Object on touchmove.
var curFinger = this.touchToFinger(finger[i], e);
identifier.push(curFinger.identifier);
if (!this.finger[curFinger.identifier]) {
// for each new finger set the beginFinger
this.finger[curFinger.identifier] = {
beginFinger: curFinger
};
this.events.touchstart.trigger({ finger: this.finger[curFinger.identifier], originalEvent: e });
this.fingerCount++;
this.clearLongPress();
if (identifier.length === 1 && !this.destroyed) {
this.detectLongpress(this.finger[curFinger.identifier], e);
}
}
if (this.destroyed)
return;
this.finger[curFinger.identifier].lastFinger = this.finger[curFinger.identifier].curFinger || curFinger;
this.finger[curFinger.identifier].curFinger = curFinger;
}
// detect if Fingers left the screen
for (i in this.finger) {
if (identifier.indexOf(i) === -1 && identifier.length === 0) {
var f = this.finger[i];
delete this.finger[i];
this.fingerCount--;
this.detectSwipe(f, e);
if (identifier.length === 0) {
this.detectThrow(f, e);
}
this.detectTap(f, e);
this.events.touchend.trigger({ finger: f, originalEvent: e });
if (f.panning) {
this.events.panend.trigger({ finger: f, originalEvent: e });
}
this.clearLongPress();
if (identifier.length < 2 && this.curPinch) {
this.events.pinchend.trigger({ finger: f, originalEvent: e, pinch: this.curPinch });
this.curPinch = undefined;
}
}
else {
this.detectMove(this.finger[i], e);
}
}
this.detectPinch(e);
}
/**
* helps to clone a touch object and adds a timeStamp
*/
touchToFinger(touch, e) {
return {
clientX: touch.clientX,
clientY: touch.clientY,
force: touch.force,
identifier: touch.identifier,
pageX: touch.pageX,
pageY: touch.pageY,
radiusX: touch.radiusX,
radiusY: touch.radiusY,
screenX: touch.screenX,
screenY: touch.screenY,
target: touch.target,
timeStamp: e.timeStamp,
currentTarget: e.currentTarget,
preventDefault: function () {
e.preventDefault();
},
stopPropagation: function () {
e.stopPropagation();
}
};
}
detectMove(f, e) {
if (this.fingerCount !== 1 || !f)
return;
f.offset = {
x: f.curFinger.screenX - f.lastFinger.screenX,
y: f.curFinger.screenY - f.lastFinger.screenY
};
this.events.touchmove.trigger({ finger: f, originalEvent: e });
f.offset = {
x: f.curFinger.screenX - f.lastFinger.screenX,
y: f.curFinger.screenY - f.lastFinger.screenY
};
if (!f.longPressed) {
if (f.panning) {
this.events.pan.trigger({ finger: f, originalEvent: e });
}
else if (distanceOfFingers(f.beginFinger, f.curFinger) > this.minFirstPanDistance) {
f.offset = {
x: f.curFinger.screenX - f.beginFinger.screenX,
y: f.curFinger.screenY - f.beginFinger.screenY
};
f.panning = true;
this.events.panstart.trigger({ finger: f, originalEvent: e });
this.events.pan.trigger({ finger: f, originalEvent: e });
}
}
}
detectTap(f, e) {
if (this.fingerCount > 0)
return;
if (distanceOfFingers(f.beginFinger, f.lastFinger) > this.tapTolerance)
return;
//detect doubleTap
if (this.lastTap && f.lastFinger !== this.lastTap.lastFinger
&& distanceOfFingers(f.lastFinger, this.lastTap.lastFinger) < this.doubleTapDistance
&& (f.curFinger.timeStamp - this.lastTap.curFinger.timeStamp) < this.doubleTapSpeed) {
this.events.doubletap.trigger({ finger: f, originalEvent: e });
}
else if (!f.longPressed) {
this.events.tap.trigger({ finger: f, originalEvent: e });
this.lastTap = f;
}
}
// used to detect a longpress, when only one finger is holded on the screen for 2 seconds
detectLongpress(f, e) {
if (this.longPress) {
return;
}
this.longPress = {
finger: f,
timeout: setTimeout(() => {
if (!this.longPress) {
return;
}
if (this.longPress.finger.panning) {
return;
}
if (distanceOfFingers(this.longPress.finger.beginFinger, this.longPress.finger.lastFinger) > this.longTolerance) {
return;
}
this.longPress.finger.longPressed = true;
this.events.longpress.trigger({ finger: this.longPress.finger, originalEvent: e });
this.longPress = null;
}, this.longPressTime)
};
}
clearLongPress() {
if (!this.longPress)
return;
clearTimeout(this.longPress.timeout);
this.longPress = null;
}
detectPinch(e) {
var i;
var finger = [];
var curDistance;
for (i in this.finger) {
finger.push(this.finger[i]);
}
if (finger.length !== 2) {
return;
}
if (this.lastFingers[0] === finger[0] && this.lastFingers[1] === finger[1]) {
var dx = finger[0].curFinger.screenX - finger[1].curFinger.screenX;
var dy = finger[0].curFinger.screenY - finger[1].curFinger.screenY;
curDistance = Math.sqrt(dx * dx + dy * dy);
if (this.lastDistance) {
var lastAngle = posToAngle(finger[0].lastFinger, finger[1].lastFinger);
var currAngle = posToAngle(finger[0].curFinger, finger[1].curFinger);
var angleChanged = angleChange(lastAngle, currAngle);
if (angleChanged) {
this.events.rotate.trigger({ lastAngle: lastAngle, curAngle: currAngle, angleChange: angleChanged });
}
var distanceChange = curDistance - this.lastDistance;
if (Math.abs(distanceChange) > this.minPinchDistance) {
if (this.curPinch === null) {
this.curPinch = 0;
this.originDistance = curDistance;
this.events.pinchstart.trigger({ startDistance: this.originDistance });
}
this.curPinch = curDistance / this.originDistance;
this.events.pinch.trigger({ startDistance: this.originDistance, curDistance: curDistance, pinch: curDistance / this.originDistance });
}
}
}
else {
this.lastDistance = undefined;
}
this.lastDistance = curDistance;
this.lastFingers = finger;
}
detectSwipe(finger, e) {
if ((finger.lastFinger.timeStamp - finger.beginFinger.timeStamp) < this.swipeSpeed) {
if (distanceOfFingers(finger.beginFinger, finger.lastFinger) < this.minSwipeDistance) {
return;
}
if (Math.abs(finger.beginFinger.screenX - finger.lastFinger.screenX) / this.swipePrecision > Math.abs(finger.beginFinger.screenY - finger.lastFinger.screenY)) {
this.events.swipe.trigger({ originalEvent: e, direction: finger.beginFinger.screenX - finger.lastFinger.screenX });
// up-down swipe
if (finger.beginFinger.screenX - finger.lastFinger.screenX < 0) {
this.events.swipeleft.trigger({ originalEvent: e, lastOffsetX: Math.abs(finger.beginFinger.screenX - finger.lastFinger.screenX) });
}
else {
this.events.swiperight.trigger({ originalEvent: e, lastOffsetX: Math.abs(finger.beginFinger.screenX - finger.lastFinger.screenX) });
}
}
else if (Math.abs(finger.beginFinger.screenX - finger.lastFinger.screenX) < Math.abs(finger.beginFinger.screenY - finger.lastFinger.screenY) / this.swipePrecision) {
this.events.swipe.trigger(finger.beginFinger.screenX - finger.lastFinger.screenX);
if (finger.beginFinger.screenY - finger.lastFinger.screenY < 0) {
this.events.swipeup.trigger({ originalEvent: e, lastOffsetY: Math.abs(finger.beginFinger.screenY - finger.lastFinger.screenY) });
}
else {
this.events.swipedown.trigger({ originalEvent: e, lastOffsetY: Math.abs(finger.beginFinger.screenY - finger.lastFinger.screenY) });
}
}
}
}
//detects left and right throw
detectThrow(f, e) {
var deltaTime = f.curFinger.timeStamp - f.lastFinger.timeStamp;
var deltaX = f.curFinger.screenX - f.lastFinger.screenX;
var deltaY = f.curFinger.screenY - f.lastFinger.screenY;
var delta = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
//var speedX = (1000 / deltaTime) * deltaX;
var speed = (1000 / deltaTime) * delta;
if (speed > 200) {
const event = { finger: f.curFinger, event: e };
this.events.throw.trigger(event);
if (deltaX > deltaY) {
if (deltaX < 0) {
this.events.throwright.trigger('throwright');
}
else {
this.events.throwleft.trigger('throwleft');
}
}
else {
if (deltaY < 0) {
this.events.throwup.trigger(event);
}
else {
this.events.throwdown.trigger(event);
}
}
}
}
destroy() {
this.el.removeEventListener('touchstart', this.touchStartHandler);
this.el.removeEventListener('touchmove', this.touchMoveHandler);
this.el.removeEventListener('touchend', this.touchEndHandler);
this.el.removeEventListener('mousedown', this.mouseStartHandler);
//this.off();
this.finger = null;
this.lastFingers = [];
this.clearLongPress();
delete this.el;
}
;
}
/**
*two helper Methods for the rotation
*/
function posToAngle(startFinger, endFinger) {
const dx = endFinger.clientX - startFinger.clientX;
const dy = endFinger.clientY - startFinger.clientY;
const angle = Math.acos(dx / Math.sqrt(dx * dx + dy * dy)) / Math.PI * 180;
return 0 > dy ? angle : -angle;
}
;
function angleChange(a, b) {
return (a - b);
}
function distanceOfFingers(one, two) {
var dx = Math.abs(one.screenX - two.screenX);
var dy = Math.abs(one.screenY - two.screenY);
return Math.sqrt((dx * dx) + (dy * dy));
}