-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSnapTransition.js
268 lines (231 loc) · 7.1 KB
/
SnapTransition.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
/* 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 PE = require('famous/physics/PhysicsEngine');
var Particle = require('famous/physics/bodies/Particle');
var Spring = require('famous/physics/constraints/Snap');
var Vector = require('famous/math/Vector');
/**
* SnapTransition is a method of transitioning between two values (numbers,
* or arrays of numbers). It is similar to SpringTransition except
* the transition can be much faster and always has a damping effect.
*
* @class SnapTransition
* @constructor
*
* @param [state=0] {Number|Array} Initial state
*/
function SnapTransition(state) {
state = state || 0;
this.endState = new Vector(state);
this.initState = new Vector();
this._dimensions = 1;
this._restTolerance = 1e-10;
this._absRestTolerance = this._restTolerance;
this._callback = undefined;
this.PE = new PE();
this.particle = new Particle();
this.spring = new Spring({anchor : this.endState});
this.PE.addBody(this.particle);
this.PE.attach(this.spring, this.particle);
}
SnapTransition.SUPPORTS_MULTIPLE = 3;
/**
* @property SnapTransition.DEFAULT_OPTIONS
* @type Object
* @protected
* @static
*/
SnapTransition.DEFAULT_OPTIONS = {
/**
* The amount of time in milliseconds taken for one complete oscillation
* when there is no damping
* Range : [0, Infinity]
*
* @attribute period
* @type Number
* @default 100
*/
period : 100,
/**
* The damping of the snap.
* Range : [0, 1]
*
* @attribute dampingRatio
* @type Number
* @default 0.2
*/
dampingRatio : 0.2,
/**
* The initial velocity of the transition.
*
* @attribute velocity
* @type Number|Array
* @default 0
*/
velocity : 0
};
function _getEnergy() {
return this.particle.getEnergy() + this.spring.getEnergy(this.particle);
}
function _setAbsoluteRestTolerance() {
var distance = this.endState.sub(this.initState).normSquared();
this._absRestTolerance = (distance === 0)
? this._restTolerance
: this._restTolerance * distance;
}
function _setTarget(target) {
this.endState.set(target);
_setAbsoluteRestTolerance.call(this);
}
function _wake() {
this.PE.wake();
}
function _sleep() {
this.PE.sleep();
}
function _setParticlePosition(p) {
this.particle.position.set(p);
}
function _setParticleVelocity(v) {
this.particle.velocity.set(v);
}
function _getParticlePosition() {
return (this._dimensions === 0)
? this.particle.getPosition1D()
: this.particle.getPosition();
}
function _getParticleVelocity() {
return (this._dimensions === 0)
? this.particle.getVelocity1D()
: this.particle.getVelocity();
}
function _setCallback(callback) {
this._callback = callback;
}
function _setupDefinition(definition) {
var defaults = SnapTransition.DEFAULT_OPTIONS;
if (definition.period === undefined) definition.period = defaults.period;
if (definition.dampingRatio === undefined) definition.dampingRatio = defaults.dampingRatio;
if (definition.velocity === undefined) definition.velocity = defaults.velocity;
//setup spring
this.spring.setOptions({
period : definition.period,
dampingRatio : definition.dampingRatio
});
//setup particle
_setParticleVelocity.call(this, definition.velocity);
}
function _update() {
if (this.PE.isSleeping()) {
if (this._callback) {
var cb = this._callback;
this._callback = undefined;
cb();
}
return;
}
if (_getEnergy.call(this) < this._absRestTolerance) {
_setParticlePosition.call(this, this.endState);
_setParticleVelocity.call(this, [0,0,0]);
_sleep.call(this);
}
}
/**
* Resets the state and velocity
*
* @method reset
*
* @param state {Number|Array} State
* @param [velocity] {Number|Array} Velocity
*/
SnapTransition.prototype.reset = function reset(state, velocity) {
this._dimensions = (state instanceof Array)
? state.length
: 0;
this.initState.set(state);
_setParticlePosition.call(this, state);
_setTarget.call(this, state);
if (velocity) _setParticleVelocity.call(this, velocity);
_setCallback.call(this, undefined);
};
/**
* Getter for velocity
*
* @method getVelocity
*
* @return velocity {Number|Array}
*/
SnapTransition.prototype.getVelocity = function getVelocity() {
return _getParticleVelocity.call(this);
};
/**
* Setter for velocity
*
* @method setVelocity
*
* @return velocity {Number|Array}
*/
SnapTransition.prototype.setVelocity = function setVelocity(velocity) {
this.call(this, _setParticleVelocity(velocity));
};
/**
* Detects whether a transition is in progress
*
* @method isActive
*
* @return {Boolean}
*/
SnapTransition.prototype.isActive = function isActive() {
return !this.PE.isSleeping();
};
/**
* Halt the transition
*
* @method halt
*/
SnapTransition.prototype.halt = function halt() {
this.set(this.get());
};
/**
* Get the current position of the transition
s *
* @method get
*
* @return state {Number|Array}
*/
SnapTransition.prototype.get = function get() {
_update.call(this);
return _getParticlePosition.call(this);
};
/**
* Set the end position and transition, with optional callback on completion.
*
* @method set
*
* @param state {Number|Array} Final state
* @param [definition] {Object} Transition definition
* @param [callback] {Function} Callback
*/
SnapTransition.prototype.set = function set(state, definition, callback) {
if (!definition) {
this.reset(state);
if (callback) callback();
return;
}
this._dimensions = (state instanceof Array)
? state.length
: 0;
_wake.call(this);
_setupDefinition.call(this, definition);
_setTarget.call(this, state);
_setCallback.call(this, callback);
};
module.exports = SnapTransition;
});