-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTouchTracker.js
110 lines (97 loc) · 3.66 KB
/
TouchTracker.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Owner: [email protected]
* @license MPL 2.0
* @copyright Famous Industries, Inc. 2014
*/
define(function(require, exports, module) {
var EventHandler = require('famous/core/EventHandler');
var _now = Date.now;
function _timestampTouch(touch, event, history) {
return {
x: touch.clientX,
y: touch.clientY,
identifier : touch.identifier,
origin: event.origin,
timestamp: _now(),
count: event.touches.length,
history: history
};
}
function _handleStart(event) {
for (var i = 0; i < event.changedTouches.length; i++) {
var touch = event.changedTouches[i];
var data = _timestampTouch(touch, event, null);
this.eventOutput.emit('trackstart', data);
if (!this.selective && !this.touchHistory[touch.identifier]) this.track(data);
}
}
function _handleMove(event) {
for (var i = 0; i < event.changedTouches.length; i++) {
var touch = event.changedTouches[i];
var history = this.touchHistory[touch.identifier];
if (history) {
var data = _timestampTouch(touch, event, history);
this.touchHistory[touch.identifier].push(data);
this.eventOutput.emit('trackmove', data);
}
}
}
function _handleEnd(event) {
for (var i = 0; i < event.changedTouches.length; i++) {
var touch = event.changedTouches[i];
var history = this.touchHistory[touch.identifier];
if (history) {
var data = _timestampTouch(touch, event, history);
this.eventOutput.emit('trackend', data);
delete this.touchHistory[touch.identifier];
}
}
}
function _handleUnpipe() {
for (var i in this.touchHistory) {
var history = this.touchHistory[i];
this.eventOutput.emit('trackend', {
touch: history[history.length - 1].touch,
timestamp: Date.now(),
count: 0,
history: history
});
delete this.touchHistory[i];
}
}
/**
* Helper to TouchSync – tracks piped in touch events, organizes touch
* events by ID, and emits track events back to TouchSync.
* Emits 'trackstart', 'trackmove', and 'trackend' events upstream.
*
* @class TouchTracker
* @constructor
* @param {Boolean} selective if false, save state for each touch.
*/
function TouchTracker(selective) {
this.selective = selective;
this.touchHistory = {};
this.eventInput = new EventHandler();
this.eventOutput = new EventHandler();
EventHandler.setInputHandler(this, this.eventInput);
EventHandler.setOutputHandler(this, this.eventOutput);
this.eventInput.on('touchstart', _handleStart.bind(this));
this.eventInput.on('touchmove', _handleMove.bind(this));
this.eventInput.on('touchend', _handleEnd.bind(this));
this.eventInput.on('touchcancel', _handleEnd.bind(this));
this.eventInput.on('unpipe', _handleUnpipe.bind(this));
}
/**
* Record touch data, if selective is false.
* @private
* @method track
* @param {Object} data touch data
*/
TouchTracker.prototype.track = function track(data) {
this.touchHistory[data.identifier] = [data];
};
module.exports = TouchTracker;
});