-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathRotationGestureDetector.ts
More file actions
177 lines (135 loc) · 4.48 KB
/
RotationGestureDetector.ts
File metadata and controls
177 lines (135 loc) · 4.48 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
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
import { AdaptedEvent, EventTypes } from '../interfaces';
import PointerTracker from '../tools/PointerTracker';
export interface RotationGestureListener {
onRotationBegin: (detector: RotationGestureDetector) => boolean;
onRotation: (detector: RotationGestureDetector) => boolean;
onRotationEnd: (detector: RotationGestureDetector) => void;
}
export default class RotationGestureDetector
implements RotationGestureListener
{
onRotationBegin: (detector: RotationGestureDetector) => boolean;
onRotation: (detector: RotationGestureDetector) => boolean;
onRotationEnd: (detector: RotationGestureDetector) => void;
private currentTime = 0;
private previousTime = 0;
private previousAngle = 0;
private rotation = 0;
private anchorX = 0;
private anchorY = 0;
private isInProgress = false;
private keyPointers: number[] = [NaN, NaN];
constructor(callbacks: RotationGestureListener) {
this.onRotationBegin = callbacks.onRotationBegin;
this.onRotation = callbacks.onRotation;
this.onRotationEnd = callbacks.onRotationEnd;
}
private updateCurrent(event: AdaptedEvent, tracker: PointerTracker): void {
this.previousTime = this.currentTime;
this.currentTime = event.time;
const [firstPointerID, secondPointerID] = this.keyPointers;
const firstPointerRelativeCoords =
tracker.getLastRelativeCoords(firstPointerID);
const secondPointerRelativeCoords =
tracker.getLastRelativeCoords(secondPointerID);
this.anchorX =
(firstPointerRelativeCoords.x + secondPointerRelativeCoords.x) / 2;
this.anchorY =
(firstPointerRelativeCoords.y + secondPointerRelativeCoords.y) / 2;
const firstPointerAbsoluteCoords =
tracker.getLastAbsoluteCoords(firstPointerID);
const secondPointerAbsoluteCoords =
tracker.getLastAbsoluteCoords(secondPointerID);
const vectorX: number =
secondPointerAbsoluteCoords.x - firstPointerAbsoluteCoords.x;
const vectorY: number =
secondPointerAbsoluteCoords.y - firstPointerAbsoluteCoords.y;
// Angle diff should be positive when rotating in clockwise direction
const angle: number = -Math.atan2(vectorY, vectorX);
this.rotation = Number.isNaN(this.previousAngle)
? 0
: this.previousAngle - angle;
this.previousAngle = angle;
if (this.rotation > Math.PI) {
this.rotation -= Math.PI;
} else if (this.rotation < -Math.PI) {
this.rotation += Math.PI;
}
if (this.rotation > Math.PI / 2) {
this.rotation -= Math.PI;
} else if (this.rotation < -Math.PI / 2) {
this.rotation += Math.PI;
}
}
private finish(): void {
if (!this.isInProgress) {
return;
}
this.isInProgress = false;
this.keyPointers = [NaN, NaN];
this.onRotationEnd(this);
}
private setKeyPointers(tracker: PointerTracker): void {
if (this.keyPointers[0] && this.keyPointers[1]) {
return;
}
const pointerIDs: IterableIterator<number> = tracker.getData().keys();
this.keyPointers[0] = pointerIDs.next().value as number;
this.keyPointers[1] = pointerIDs.next().value as number;
}
public onTouchEvent(event: AdaptedEvent, tracker: PointerTracker): boolean {
switch (event.eventType) {
case EventTypes.DOWN:
this.isInProgress = false;
break;
case EventTypes.ADDITIONAL_POINTER_DOWN:
if (this.isInProgress) {
break;
}
this.isInProgress = true;
this.previousTime = event.time;
this.previousAngle = NaN;
this.setKeyPointers(tracker);
this.updateCurrent(event, tracker);
this.onRotationBegin(this);
break;
case EventTypes.MOVE:
if (!this.isInProgress) {
break;
}
this.updateCurrent(event, tracker);
this.onRotation(this);
break;
case EventTypes.ADDITIONAL_POINTER_UP:
if (!this.isInProgress) {
break;
}
if (this.keyPointers.indexOf(event.pointerId) >= 0) {
this.finish();
}
break;
case EventTypes.UP:
if (this.isInProgress) {
this.finish();
}
break;
}
return true;
}
public getTimeDelta(): number {
return this.currentTime + this.previousTime;
}
public getAnchorX(): number {
return this.anchorX;
}
public getAnchorY(): number {
return this.anchorY;
}
public getRotation(): number {
return this.rotation;
}
public reset(): void {
this.keyPointers = [NaN, NaN];
this.isInProgress = false;
}
}