This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
forked from zuixjs/zkit
/
gesture_helper.js
281 lines (271 loc) · 10.2 KB
/
gesture_helper.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
/**
* zUIx - Gesture Controller
*
* @version 1.0.1 (2018-08-21)
* @author Gene
*
* @version 1.0.0 (2018-03-11)
* @author Gene
*
*/
'use strict';
zuix.controller(function(cp) {
const SCROLL_MODE_NONE = 0;
const SCROLL_MODE_HORIZONTAL = 1;
const SCROLL_MODE_VERTICAL = 2;
const GESTURE_TAP_TIMEOUT = 750;
let scrollMode = SCROLL_MODE_NONE;
let touchPointer;
let ignoreSession = false;
let passiveMode = true;
let startGap = -1;
let currentGesture;
let swipeDirection;
let speedMeter;
let mouseButtonDown = false;
let lastTapTime = new Date().getTime();
// Math.sign Polyfill
Math.sign = Math.sign || function(x) {
return ((x>0)-(x<0))||+x;
};
cp.init = function() {
let view = cp.view();
const options = cp.options();
options.html = false;
options.css = false;
passiveMode = (options.passive !== false && (view.attr('data-o-passive') !== 'false'));
startGap = (options.startGap || view.attr('data-o-startgap'));
};
cp.create = function() {
// TODO: should use event "capturing" instead of "bubbling"
cp.view().on('dragstart', {
handler: function(e) {
if (!ignoreSession && !passiveMode) {
e.preventDefault();
}
},
passive: passiveMode
}).on('mousedown', {
handler: function(e) {
const targetElement = zuix.$(e.target);
if (e.which === 1 && targetElement.parent('[class*="no-gesture"]').length() === 0 && e.x > startGap) {
mouseButtonDown = true;
ignoreSession = false;
// targetElement.css('touch-action', 'none');
// TODO: add 'cp.options().preventDrag'
targetElement.get().draggable = false;
touchStart(e, e.x, e.y);
} else ignoreSession = true;
},
passive: passiveMode
}).on('mousemove', {
handler: function(e) {
if (!ignoreSession && mouseButtonDown) {
touchMove(e, e.x, e.y);
}
},
passive: passiveMode
}).on('mouseup', function(e) {
if (e.which === 1 && !ignoreSession) {
mouseButtonDown = false;
touchStop(e);
}
}).on('touchstart', {
handler: function(e) {
const targetElement = zuix.$(e.target);
if (targetElement.parent('[class*="no-gesture"]').length() === 0 && e.touches[0].clientX > startGap) {
ignoreSession = false;
// targetElement.css('touch-action', 'none');
targetElement.get().draggable = false;
touchStart(e, e.touches[0].clientX, e.touches[0].clientY);
} else ignoreSession = true;
},
passive: passiveMode
}).on('touchmove', {
handler: function(e) {
if (!ignoreSession) {
touchMove(e, e.touches[0].clientX, e.touches[0].clientY);
}
},
passive: passiveMode
}).on('touchend', function(e) {
if (!ignoreSession) {
touchStop(e);
}
});
};
function touchStart(e, x, y) {
let timestamp = new Date().getTime();
touchPointer = {
// original event + cancel method
event: e,
cancel: function() {
touchPointer.event.cancelBubble = true;
if (!passiveMode) {
touchPointer.event.preventDefault();
}
},
// initial touch position
startX: x,
startY: y,
startTime: timestamp,
// relative movement
shiftX: 0,
shiftY: 0,
endTime: 0,
// relative movement at every step
stepX: 0,
stepY: 0,
stepTime: timestamp,
// actual position and speed
velocity: 0,
x: x,
y: y,
scrollIntent: function() {
switch (scrollMode) {
case SCROLL_MODE_HORIZONTAL:
return 'horizontal';
case SCROLL_MODE_VERTICAL:
return 'vertical';
}
return false;
}
};
speedMeter = speedObserver(touchPointer);
cp.trigger('gesture:touch', touchPointer);
}
function touchMove(e, x, y) {
if (touchPointer != null) {
touchPointer.event = e;
touchPointer.x = x;
touchPointer.y = y;
touchPointer.shiftX = (x - touchPointer.startX);
touchPointer.shiftY = (y - touchPointer.startY);
touchPointer.endTime = new Date().getTime();
// detect actual gesture
let gesture = detectGesture();
if (gesture != null && currentGesture !== false) {
if (swipeDirection != null && swipeDirection !== touchPointer.direction) {
// stop gesture detection if not coherent
currentGesture = false;
swipeDirection = 'cancel';
} else {
currentGesture = gesture;
swipeDirection = touchPointer.direction;
}
}
cp.trigger('gesture:pan', touchPointer);
}
}
function touchStop(e) {
if (touchPointer != null) {
speedMeter.update();
touchPointer.event = e;
if (currentGesture == null) {
currentGesture = detectGesture();
}
if (currentGesture != null && currentGesture !== false) {
cp.trigger(currentGesture, touchPointer);
}
}
cp.trigger('gesture:release', touchPointer);
scrollMode = SCROLL_MODE_NONE;
swipeDirection = null;
currentGesture = null;
touchPointer = null;
}
function detectGesture() {
let gesture = null;
// update touchPointer.velocity data
speedMeter.update();
// update tap gesture and swipe direction
const minStepDistance = 3;
const angle = Math.atan2(touchPointer.shiftY-touchPointer.stepY, touchPointer.shiftX-touchPointer.stepX) * 180 / Math.PI;
if ((touchPointer.shiftX) === 0 && (touchPointer.shiftY) === 0 && touchPointer.startTime > lastTapTime+100 && touchPointer.stepTime < GESTURE_TAP_TIMEOUT) {
// gesture TAP
lastTapTime = new Date().getTime();
gesture = 'gesture:tap';
} else if ((scrollMode === SCROLL_MODE_NONE || scrollMode === SCROLL_MODE_HORIZONTAL)
&& touchPointer.stepDistance > minStepDistance && ((angle >= 135 && angle <= 180) || (angle >= -180 && angle <= -135))) {
// gesture swipe RIGHT
touchPointer.direction = 'left';
gesture = 'gesture:swipe';
scrollMode = SCROLL_MODE_HORIZONTAL;
} else if ((scrollMode === SCROLL_MODE_NONE || scrollMode === SCROLL_MODE_HORIZONTAL)
&& touchPointer.stepDistance > minStepDistance && ((angle >= 0 && angle <= 45) || (angle >= -45 && angle < 0))) {
// gesture swipe LEFT
touchPointer.direction = 'right';
gesture = 'gesture:swipe';
scrollMode = SCROLL_MODE_HORIZONTAL;
} else if ((scrollMode === SCROLL_MODE_NONE || scrollMode === SCROLL_MODE_VERTICAL)
&& touchPointer.stepDistance > minStepDistance && (angle > 45 && angle < 135)) {
// gesture swipe UP
touchPointer.direction = 'down';
gesture = 'gesture:swipe';
scrollMode = SCROLL_MODE_VERTICAL;
} else if ((scrollMode === SCROLL_MODE_NONE || scrollMode === SCROLL_MODE_VERTICAL)
&& touchPointer.stepDistance > minStepDistance && (angle > -135 && angle < -45)) {
// gesture swipe DOWN
touchPointer.direction = 'up';
gesture = 'gesture:swipe';
scrollMode = SCROLL_MODE_VERTICAL;
}
// reset touch step data
if (touchPointer.stepDistance > minStepDistance) {
speedMeter.step();
}
return gesture;
}
function speedObserver(tp) {
let direction = '';
let startData = {
time: 0,
x: 0, y: 0
};
let stopData = {
time: 0,
x: 0, y: 0
};
return {
update: function() {
stopData.time = new Date().getTime();
stopData.x = tp.x;
stopData.y = tp.y;
if (direction !== tp.direction) {
this.reset();
return;
}
const elapsedTime = stopData.time - startData.time;
let l = {x: (stopData.x - startData.x), y: (stopData.y - startData.y)};
const d = Math.sqrt(l.x*l.x + l.y*l.y);
tp.distance = d;
// movement speed in px/ms
let speed = (d / elapsedTime);
// add the direction info
tp.velocity = (tp.direction === 'left' || tp.direction === 'up') ? -speed : speed;
// update "step" speed data
tp.stepTime = (tp.endTime-tp.stepTime);
l = {x: (tp.shiftX-tp.stepX), y: (tp.shiftY-tp.stepY)};
tp.stepDistance = Math.sqrt(l.x*l.x+l.y*l.y);
tp.stepSpeed = (tp.stepDistance / tp.stepTime);
},
reset: function() {
// direction changed: reset
direction = tp.direction;
startData.time = new Date().getTime();
startData.x = tp.x;
startData.y = tp.y;
tp.velocity = 0;
tp.distance = 0;
this.step();
},
step: function() {
tp.stepTime = tp.endTime;
tp.stepX = tp.shiftX;
tp.stepY = tp.shiftY;
tp.stepSpeed = 0;
tp.stepDistance = 0;
}
};
}
});