-
Notifications
You must be signed in to change notification settings - Fork 7
/
ScaleSync.js
105 lines (89 loc) · 3.43 KB
/
ScaleSync.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
/* 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 TwoFingerSync = require('./TwoFingerSync');
/**
* Handles piped in two-finger touch events to increase or decrease scale via pinching / expanding.
* Emits 'start', 'update' and 'end' events an object with position, velocity, touch ids, distance, and scale factor.
* Useful for determining a scaling factor from initial two-finger touch.
*
* @class ScaleSync
* @extends TwoFingerSync
* @constructor
* @param {Object} options default options overrides
* @param {Number} [options.scale] scale velocity by this factor
*/
function ScaleSync(options) {
TwoFingerSync.call(this);
this.options = Object.create(ScaleSync.DEFAULT_OPTIONS);
if (options) this.setOptions(options);
this._scaleFactor = 1;
this._startDist = 0;
this._eventInput.on('pipe', _reset.bind(this));
}
ScaleSync.prototype = Object.create(TwoFingerSync.prototype);
ScaleSync.prototype.constructor = ScaleSync;
ScaleSync.DEFAULT_OPTIONS = {
scale : 1
};
function _reset() {
this.touchAId = undefined;
this.touchBId = undefined;
}
// handles initial touch of two fingers
ScaleSync.prototype._startUpdate = function _startUpdate(event) {
this._scaleFactor = 1;
this._startDist = TwoFingerSync.calculateDistance(this.posA, this.posB);
this._eventOutput.emit('start', {
count: event.touches.length,
touches: [this.touchAId, this.touchBId],
distance: this._startDist,
center: TwoFingerSync.calculateCenter(this.posA, this.posB)
});
};
// handles movement of two fingers
ScaleSync.prototype._moveUpdate = function _moveUpdate(diffTime) {
var scale = this.options.scale;
var currDist = TwoFingerSync.calculateDistance(this.posA, this.posB);
var center = TwoFingerSync.calculateCenter(this.posA, this.posB);
var delta = (currDist - this._startDist) / this._startDist;
var newScaleFactor = Math.max(1 + scale * delta, 0);
var veloScale = (newScaleFactor - this._scaleFactor) / diffTime;
this._eventOutput.emit('update', {
delta : delta,
scale: newScaleFactor,
velocity: veloScale,
distance: currDist,
center : center,
touches: [this.touchAId, this.touchBId]
});
this._scaleFactor = newScaleFactor;
};
/**
* Return entire options dictionary, including defaults.
*
* @method getOptions
* @return {Object} configuration options
*/
ScaleSync.prototype.getOptions = function getOptions() {
return this.options;
};
/**
* Set internal options, overriding any default options
*
* @method setOptions
*
* @param {Object} [options] overrides of default options
* @param {Number} [options.scale] scale velocity by this factor
*/
ScaleSync.prototype.setOptions = function setOptions(options) {
if (options.scale !== undefined) this.options.scale = options.scale;
};
module.exports = ScaleSync;
});