forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecognizers.ts
More file actions
58 lines (51 loc) · 1.38 KB
/
recognizers.ts
File metadata and controls
58 lines (51 loc) · 1.38 KB
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
import { pointerCoord, Coordinates } from '../util/dom';
export class PanRecognizer {
private startCoord: Coordinates;
private dirty: boolean = false;
private threshold: number;
private maxCosine: number;
private _angle: any = 0;
private _isPan: number = 0;
constructor(private direction: string, threshold: number, maxAngle: number) {
let radians = maxAngle * (Math.PI / 180);
this.maxCosine = Math.cos(radians);
this.threshold = threshold * threshold;
}
start(coord: Coordinates) {
this.startCoord = coord;
this._angle = 0;
this._isPan = 0;
this.dirty = true;
}
detect(coord: Coordinates): boolean {
if (!this.dirty) {
return false;
}
let deltaX = (coord.x - this.startCoord.x);
let deltaY = (coord.y - this.startCoord.y);
let distance = deltaX * deltaX + deltaY * deltaY;
if (distance >= this.threshold) {
let angle = Math.atan2(deltaY, deltaX);
let cosine = (this.direction === 'y')
? Math.sin(angle)
: Math.cos(angle);
this._angle = angle;
if (cosine > this.maxCosine) {
this._isPan = 1;
} else if (cosine < -this.maxCosine) {
this._isPan = -1;
} else {
this._isPan = 0;
}
this.dirty = false;
return true;
}
return false;
}
angle(): any {
return this._angle;
}
pan(): number {
return this._isPan;
}
}