-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
494 lines (457 loc) · 18.6 KB
/
index.ts
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//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, ITmitter } from 'tmitter';
interface IGestureRecognizerConfig {
swipeSpeed?: number;
swipePrecision?: number;
minPinchDistance?: number;
minSwipeDistance?: number;
longPressTime?: number;
longTolerance?: number;
tapTolerance?: number;
panDistance?: number;
doubleTapSpeed?: number;
doubleTapDistance?: number;
minFirstPanDistance?: number;
touchMode?: boolean;
}
interface IGestureRecognizerEvents {
tap: ITmitter<any>;
doubletap: ITmitter<any>;
pinchstart: ITmitter<any>;
pinch: ITmitter<any>;
pinchend: ITmitter<any>;
throw: ITmitter<any>;
throwleft: ITmitter<any>;
throwright: ITmitter<any>;
throwup: ITmitter<any>;
throwdown: ITmitter<any>;
longpress: ITmitter<any>;
touchstart: ITmitter<any>;
touchmove: ITmitter<any>;
touchend: ITmitter<any>;
panstart: ITmitter<any>;
pan: ITmitter<any>;
panend: ITmitter<any>;
rotate: ITmitter<any>;
swipe: ITmitter<any>;
swipeup: ITmitter<any>;
swipedown: ITmitter<any>;
swipeleft: ITmitter<any>;
swiperight: ITmitter<any>;
}
/**
* @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 {
public el: HTMLElement;
public swipeSpeed: number;
public swipePrecision: number;
public minPinchDistance: number;
public minSwipeDistance: number;
public longPressTime: number;
public longTolerance: number;
public tapTolerance: number;
public panDistance: number;
public doubleTapSpeed: number;
public doubleTapDistance: number;
public minFirstPanDistance: number;
public touchMode: boolean;
public destroyed: boolean;
public events: IGestureRecognizerEvents;
private finger: any;
private fingerCount: number;
private lastTab: any;
private longPress: any;
private lastTap: any;
// detect pitch
private lastDistance?: number;
private lastFingers = [1, 2];
private curPinch?: number;
private originDistance: number;
constructor(el: HTMLElement, config?: IGestureRecognizerConfig) {
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);
}
}
private touchStartHandler(e: TouchEvent) {
this.updateFinger(e.touches, e.timeStamp, e as any);
if (e.touches.length === 1) {
document.addEventListener('touchmove', this.touchMoveHandler);
document.addEventListener('touchend', this.touchEndHandler);
}
};
private touchMoveHandler(e: TouchEvent) {
this.updateFinger(e.touches, e.timeStamp, e as any);
};
private touchEndHandler(e: TouchEvent) {
this.updateFinger(e.touches, e.timeStamp, e as any);
if (!e.touches.length) {
document.removeEventListener('touchmove', this.touchMoveHandler);
document.removeEventListener('touchend', this.touchEndHandler);
}
};
private mouseStartHandler(e: any) {
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);
};
private mouseMoveHandler(e: any) {
//this.mouseEventToTouch(e);
this.updateFinger([e], e.timeStamp, e);
};
private mouseEndHandler(e: any) {
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
*/
private updateFinger(finger: Touch[] | TouchList|PointerEvent[], time: number, e: PointerEvent) {
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] as any, 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 as any) === -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 as any);
}
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
*/
private touchToFinger(touch: PointerEvent & Touch, e: PointerEvent) {
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();
}
};
}
private detectMove(f: any, e: any) {
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 });
}
}
}
private detectTap(f: any, e: any) {
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
private detectLongpress(f: any, e: any) {
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)
};
}
private clearLongPress() {
if (!this.longPress)
return;
clearTimeout(this.longPress.timeout);
this.longPress = null;
}
private detectPinch(e: any) {
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;
}
private detectSwipe(finger: any, e: any) {
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
private detectThrow(f: any, e: any) {
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);
}
}
}
}
public 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: any, endFinger: any) {
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: number, b: number) {
return (a - b);
}
function distanceOfFingers(one: PointerEvent, two: PointerEvent) {
var dx = Math.abs(one.screenX - two.screenX);
var dy = Math.abs(one.screenY - two.screenY);
return Math.sqrt((dx * dx) + (dy * dy));
}