-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathScaleGestureDetector.ts
More file actions
169 lines (127 loc) · 4.29 KB
/
ScaleGestureDetector.ts
File metadata and controls
169 lines (127 loc) · 4.29 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
import { DEFAULT_TOUCH_SLOP } from '../constants';
import { AdaptedEvent, EventTypes } from '../interfaces';
import PointerTracker from '../tools/PointerTracker';
export interface ScaleGestureListener {
onScaleBegin: (detector: ScaleGestureDetector) => boolean;
onScale: (detector: ScaleGestureDetector) => boolean;
onScaleEnd: (detector: ScaleGestureDetector) => void;
}
export default class ScaleGestureDetector implements ScaleGestureListener {
public onScaleBegin: (detector: ScaleGestureDetector) => boolean;
public onScale: (detector: ScaleGestureDetector) => boolean;
public onScaleEnd: (detector: ScaleGestureDetector) => void;
private focusX!: number;
private focusY!: number;
private currentSpan!: number;
private prevSpan!: number;
private initialSpan!: number;
private currentTime!: number;
private prevTime!: number;
private inProgress = false;
private spanSlop: number;
private minSpan: number;
public constructor(callbacks: ScaleGestureListener) {
this.onScaleBegin = callbacks.onScaleBegin;
this.onScale = callbacks.onScale;
this.onScaleEnd = callbacks.onScaleEnd;
this.spanSlop = DEFAULT_TOUCH_SLOP * 2;
this.minSpan = 0;
}
public onTouchEvent(event: AdaptedEvent, tracker: PointerTracker): boolean {
this.currentTime = event.time;
const action: EventTypes = event.eventType;
const numOfPointers = tracker.getTrackedPointersCount();
const streamComplete: boolean =
action === EventTypes.UP ||
action === EventTypes.ADDITIONAL_POINTER_UP ||
action === EventTypes.CANCEL;
if (action === EventTypes.DOWN || streamComplete) {
if (this.inProgress) {
this.onScaleEnd(this);
this.inProgress = false;
this.initialSpan = 0;
}
if (streamComplete) {
return true;
}
}
const configChanged: boolean =
action === EventTypes.DOWN ||
action === EventTypes.ADDITIONAL_POINTER_UP ||
action === EventTypes.ADDITIONAL_POINTER_DOWN;
const pointerUp = action === EventTypes.ADDITIONAL_POINTER_UP;
const ignoredPointer: number | undefined = pointerUp
? event.pointerId
: undefined;
// Determine focal point
const div: number = pointerUp ? numOfPointers - 1 : numOfPointers;
const coordsSum = tracker.getRelativeCoordsSum();
const focusX = coordsSum.x / div;
const focusY = coordsSum.y / div;
// Determine average deviation from focal point
let devSumX = 0;
let devSumY = 0;
tracker.getData().forEach((value, key) => {
if (key === ignoredPointer) {
return;
}
devSumX += Math.abs(value.relativeCoords.x - focusX);
devSumY += Math.abs(value.relativeCoords.y - focusY);
});
const devX: number = devSumX / div;
const devY: number = devSumY / div;
const spanX: number = devX * 2;
const spanY: number = devY * 2;
const span = Math.hypot(spanX, spanY);
// Begin/end events
const wasInProgress: boolean = this.inProgress;
this.focusX = focusX;
this.focusY = focusY;
if (this.inProgress && (span < this.minSpan || configChanged)) {
this.onScaleEnd(this);
this.inProgress = false;
this.initialSpan = span;
}
if (configChanged) {
this.initialSpan = this.prevSpan = this.currentSpan = span;
}
if (
!this.inProgress &&
span >= this.minSpan &&
(wasInProgress || Math.abs(span - this.initialSpan) > this.spanSlop)
) {
this.prevSpan = this.currentSpan = span;
this.prevTime = this.currentTime;
this.inProgress = this.onScaleBegin(this);
}
// Handle motion
if (action !== EventTypes.MOVE) {
return true;
}
this.currentSpan = span;
if (this.inProgress && !this.onScale(this)) {
return true;
}
this.prevSpan = this.currentSpan;
this.prevTime = this.currentTime;
return true;
}
public getCurrentSpan(): number {
return this.currentSpan;
}
public getFocusX(): number {
return this.focusX;
}
public getFocusY(): number {
return this.focusY;
}
public getTimeDelta(): number {
return this.currentTime - this.prevTime;
}
public getScaleFactor(numOfPointers: number): number {
if (numOfPointers < 2) {
return 1;
}
return this.prevSpan > 0 ? this.currentSpan / this.prevSpan : 1;
}
}