forked from visiongeist/toe.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoe.js
More file actions
451 lines (383 loc) · 15.3 KB
/
toe.js
File metadata and controls
451 lines (383 loc) · 15.3 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
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
/*!
* toe.js
* version 3.0.6
* author: Damien Antipa
* https://github.com/dantipa/toe.js
*/
(function ($, window, undefined) {
var state,
gestures = {},
isTouch = 'ontouchstart' in window,
touch = { /** @lends $.toe */
/**
* flag to indicate if the touch handling is
* @type {Boolean}
*/
active: false,
/**
* turns on the tracking of touch events
* will implicitly be called when including
*/
on: function () {
$(document).on('touchstart MSPointerDown pointerdown', touchstart)
.on('touchmove MSPointerMove MSPointerHover pointermove', touchmove)
.on('touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel', touchend);
touch.active = true;
},
/**
* turns off the tracking of touch events
*/
off: function () {
$(document).off('touchstart MSPointerDown pointerdown', touchstart)
.off('touchmove MSPointerMove MSPointerHover pointermove ', touchmove)
.off('touchend touchcancel MSPointerUp MSPointerCancel pointerup pointercancel', touchend);
touch.active = false;
},
/**
* track new gesture
*
* @param {String} namespace for the gesture
* @param {Object} gesture handlers for the touch events
* @param {Function} gesture.touchstart
* @param {Function} gesture.touchmove
* @param {Function} gesture.touchend
*/
track: function (namespace, gesture) {
gestures[namespace] = gesture;
},
/**
* returns normalized event parameters
* @param {Event} event
* @param {Object} extra additional parameters to add
* @return {Object}
*/
addEventParam: function (event, extra) {
var $t = $(event.target),
pos = $t.offset(),
param = {
pageX: event.point[0].x,
pageY: event.point[0].y,
offsetX: pos.left - event.point[0].x,
offsetY: pos.top - event.point[0].y
};
return $.extend(param, extra);
},
/**
* normalizes a Event object for internal usage
* @param {Event} event original event object
* @return {Object}
*/
Event: function (event) { // normalizes and simplifies the event object
var normalizedEvent = {
type: event.type,
timestamp: new Date().getTime(),
target: event.target, // target is always consistent through start, move, end
point: []
};
var points = [];
// Touch
if (event.type.indexOf('touch') > -1) {
points = event.changedTouches || event.originalEvent.changedTouches || event.touches || event.originalEvent.touches;
} else
// MSPointer
if (event.type.match(/.*?pointer.*?/i)) {
points = [event.originalEvent];
}
$.each(points, function (i, e) {
normalizedEvent.point.push({x: e.pageX, y: e.pageY});
});
return normalizedEvent;
},
/**
* creates cross event a new state object
* @param {Object} start point object
* @return {Object}
*/
State: function (start) {
var p = start.point[0];
return {
start: start,
move: [],
end: null
};
},
/**
* Math methods for point arithmetic
* @type {Object}
*/
calc: {
/**
* calculates the passed time between two points
* @param {Object} start
* @param {Object} end
* @return {Number} passed time in ms
*/
getDuration: function (start, end) {
return end.timestamp - start.timestamp;
},
/**
* calculates the distance between two points
* @param {Object} start
* @param {Object} end
* @return {Number} distance in px
*/
getDistance: function (start, end) {
return Math.sqrt(Math.pow(end.x - start.x, 2) + Math.pow(end.y - start.y, 2));
},
/**
* calculates the angle between two points
* @param {Object} start
* @param {Object} end
* @return {Number} in degree
*/
getAngle: function (start, end) {
return Math.atan2(end.y - start.y, end.x - start.x) * 180 / Math.PI;
},
/**
* returns the direction of a movement based on an angle
* @param {Number} angle
* @return {String} 'up', 'down', 'left', 'right' or 'unknown'
*/
getDirection: function (angle) {
return angle < -45 && angle > -135 ? 'up' :
angle >= -45 && angle <= 45 ? 'right' :
angle >= 45 && angle < 135 ? 'down' :
angle >= 135 || angle <= -135 ? 'left' :
'unknown';
},
/**
* returns the scale of a transformation
* @param {Object} start
* @param {Object} move
* @return {Number}
*/
getScale: function (start, move) {
var sp = start.point,
mp = move.point;
if (sp.length === 2 && mp.length === 2) { // needs to have the position of two fingers
return (Math.sqrt(Math.pow(mp[0].x - mp[1].x, 2) + Math.pow(mp[0].y - mp[1].y, 2)) / Math.sqrt(Math.pow(sp[0].x - sp[1].x, 2) + Math.pow(sp[0].y - sp[1].y, 2))).toFixed(2);
}
return 0;
},
/**
* calculates the rotation angle
* @param {Object} start
* @param {Object} move
* @return {Number} in degree
*/
getRotation: function (start, move) {
var sp = start.point,
mp = move.point;
if (sp.length === 2 && mp.length === 2) {
return ((Math.atan2(mp[0].y - mp[1].y, mp[0].x - mp[1].x) * 180 / Math.PI) - (Math.atan2(sp[0].y - sp[1].y, sp[0].x - sp[1].x) * 180 / Math.PI)).toFixed(2);
}
return 0;
}
}
}; // touch obj
/**
* loops over all gestures
* @private
* @param {String} type the handler's name
* @param {Event} event
* @param {Object} state
* @param {Obejcect} point
*/
function loopHandler(type, event, state, point) {
$.each(gestures, function (i, g) {
g[type].call(this, event, state, point);
});
}
/**
* @private
* @param {Object} event
*/
function touchstart(event) {
var start = touch.Event(event);
state = touch.State(start); // create a new State object and add start event
loopHandler('touchstart', event, state, start);
}
/**
* @private
* @param {Object} event
*/
function touchmove(event) {
if (!state) { return; }
var move = touch.Event(event);
state.move.push(move);
loopHandler('touchmove', event, state, move);
}
/**
* @private
* @param {Object} event
*/
function touchend(event) {
var end = touch.Event(event);
state.end = end;
loopHandler('touchend', event, state, end);
}
touch.on();
// add to namespace
$.toe = touch;
}(jQuery, this));
(function ($, touch, window, undefined) {
var namespace = 'swipe', cfg = {
distance: 40, // minimum
duration: 1200, // maximum
direction: 'all'
};
touch.track(namespace, {
touchstart: function (event, state, start) {
state[namespace] = {
finger: start.point.length
};
},
touchmove: function (event, state, move) {
// if another finger was used then increment the amount of fingers used
state[namespace].finger = move.point.length > state[namespace].finger ? move.point.length : state[namespace].finger;
},
touchend: function (event, state, end) {
var opt = $.extend(cfg, event.data),
duration,
distance;
// calc
duration = touch.calc.getDuration(state.start, end);
distance = touch.calc.getDistance(state.start.point[0], end.point[0]);
// check if the swipe was valid
if (duration < opt.duration && distance > opt.distance) {
state[namespace].angle = touch.calc.getAngle(state.start.point[0], end.point[0]);
state[namespace].direction = touch.calc.getDirection(state[namespace].angle);
state[namespace].distance = distance;
// fire if the amount of fingers match
if (opt.direction === 'all' || state[namespace].direction === opt.direction) {
$(event.target).trigger($.Event(namespace, touch.addEventParam(state.start, state[namespace])));
}
}
}
});
}(jQuery, jQuery.toe, this));
(function ($, touch, window, undefined) {
var clientWidth = document.documentElement.clientWidth;
var clientHeight = document.documentElement.clientHeight;
var averageScreenLength = Math.sqrt(clientWidth * clientHeight);
var relativeDistance = (2 / 100) * averageScreenLength;
var namespace = 'tap', cfg = {
distance: relativeDistance,
duration: 300,
finger: 1
};
touch.track(namespace, {
touchstart: function (event, state, start) {
state[namespace] = {
finger: start.point.length
};
},
touchmove: function (event, state, move) {
// if another finger was used then increment the amount of fingers used
state[namespace].finger = move.point.length > state[namespace].finger ? move.point.length : state[namespace].finger;
},
touchend: function (event, state, end) {
var opt = $.extend(cfg, event.data),
duration,
distance;
// calc
duration = touch.calc.getDuration(state.start, end);
distance = touch.calc.getDistance(state.start.point[0], end.point[0]);
// check if the tap was valid
if (duration < opt.duration && distance < opt.distance) {
// fire if the amount of fingers match
if (state[namespace].finger === opt.finger) {
$(event.target).trigger(
$.Event(namespace, touch.addEventParam(state.start, state[namespace]))
);
}
}
}
});
}(jQuery, jQuery.toe, this));
(function ($, touch, window, undefined) {
var timer, abort,
namespace = 'taphold', cfg = {
distance: 20,
duration: 500,
finger: 1
};
touch.track(namespace, {
touchstart: function (event, state, start) {
var opt = $.extend(cfg, event.data);
abort = false;
state[namespace] = {
finger: start.point.length
};
clearTimeout(timer);
timer = setTimeout(function () {
if (!abort && touch.active) {
if (state[namespace].finger === opt.finger) {
$(event.target).trigger($.Event(namespace, touch.addEventParam(start, state[namespace])));
}
}
}, opt.duration);
},
touchmove: function (event, state, move) {
var opt = $.extend(cfg, event.data),
distance;
// if another finger was used then increment the amount of fingers used
state[namespace].finger = move.point.length > state[namespace].finger ? move.point.length : state[namespace].finger;
// calc
distance = touch.calc.getDistance(state.start.point[0], move.point[0]);
if (distance > opt.distance) { // illegal move
abort = true;
}
},
touchend: function (event, state, end) {
abort = true;
clearTimeout(timer);
}
});
}(jQuery, jQuery.toe, this));
(function ($, touch, window, undefined) {
var namespace = 'transform', cfg = {
scale: 0.1, // minimum
rotation: 15
},
started;
touch.track(namespace, {
touchstart: function (event, state, start) {
started = false;
state[namespace] = {
start: start,
move: []
};
},
touchmove: function (event, state, move) {
var opt = $.extend(cfg, event.data);
if (move.point.length !== 2) {
return;
}
state[namespace].move.push(move);
if (state[namespace].start.point.length !== 2 && move.point.length === 2) { // in case the user failed to start with 2 fingers
state[namespace].start = $.extend({}, move);
}
state[namespace].rotation = touch.calc.getRotation(state[namespace].start, move);
state[namespace].scale = touch.calc.getScale(state[namespace].start, move);
if (Math.abs(1 - state[namespace].scale) > opt.scale || Math.abs(state[namespace].rotation) > opt.rotation) {
if (!started) {
$(event.target).trigger($.Event('transformstart', state[namespace]));
started = true;
}
$(event.target).trigger($.Event('transform', state[namespace]));
}
},
touchend: function (event, state, end) {
if (started) {
started = false;
if (end.point.length !== 2) { // in case the user failed to end with 2 fingers
state.end = $.extend({}, state[namespace].move[state[namespace].move.length - 1]);
}
state[namespace].rotation = touch.calc.getRotation(state[namespace].start, state.end);
state[namespace].scale = touch.calc.getScale(state[namespace].start, state.end);
$(event.target).trigger($.Event('transformend', state[namespace]));
}
}
});
}(jQuery, jQuery.toe, this));