-
Notifications
You must be signed in to change notification settings - Fork 7
/
TransitionableTransform.js
216 lines (200 loc) · 6.91 KB
/
TransitionableTransform.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
/* 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 Transitionable = require('./Transitionable');
var Transform = require('famous/core/Transform');
var Utility = require('famous/utilities/Utility');
/**
* A class for transitioning the state of a Transform by transitioning
* its translate, scale, skew and rotate components independently.
*
* @class TransitionableTransform
* @constructor
*
* @param [transform=Transform.identity] {Transform} The initial transform state
*/
function TransitionableTransform(transform) {
this._final = Transform.identity.slice();
this.translate = new Transitionable([0, 0, 0]);
this.rotate = new Transitionable([0, 0, 0]);
this.skew = new Transitionable([0, 0, 0]);
this.scale = new Transitionable([1, 1, 1]);
if (transform) this.set(transform);
}
function _build() {
return Transform.build({
translate: this.translate.get(),
rotate: this.rotate.get(),
skew: this.skew.get(),
scale: this.scale.get()
});
}
/**
* An optimized way of setting only the translation component of a Transform
*
* @method setTranslate
* @chainable
*
* @param translate {Array} New translation state
* @param [transition] {Object} Transition definition
* @param [callback] {Function} Callback
* @return {TransitionableTransform}
*/
TransitionableTransform.prototype.setTranslate = function setTranslate(translate, transition, callback) {
this.translate.set(translate, transition, callback);
this._final = this._final.slice();
this._final[12] = translate[0];
this._final[13] = translate[1];
if (translate[2] !== undefined) this._final[14] = translate[2];
return this;
};
/**
* An optimized way of setting only the scale component of a Transform
*
* @method setScale
* @chainable
*
* @param scale {Array} New scale state
* @param [transition] {Object} Transition definition
* @param [callback] {Function} Callback
* @return {TransitionableTransform}
*/
TransitionableTransform.prototype.setScale = function setScale(scale, transition, callback) {
this.scale.set(scale, transition, callback);
this._final = this._final.slice();
this._final[0] = scale[0];
this._final[5] = scale[1];
if (scale[2] !== undefined) this._final[10] = scale[2];
return this;
};
/**
* An optimized way of setting only the rotational component of a Transform
*
* @method setRotate
* @chainable
*
* @param eulerAngles {Array} Euler angles for new rotation state
* @param [transition] {Object} Transition definition
* @param [callback] {Function} Callback
* @return {TransitionableTransform}
*/
TransitionableTransform.prototype.setRotate = function setRotate(eulerAngles, transition, callback) {
this.rotate.set(eulerAngles, transition, callback);
this._final = _build.call(this);
this._final = Transform.build({
translate: this.translate.get(),
rotate: eulerAngles,
scale: this.scale.get(),
skew: this.skew.get()
});
return this;
};
/**
* An optimized way of setting only the skew component of a Transform
*
* @method setSkew
* @chainable
*
* @param skewAngles {Array} New skew state
* @param [transition] {Object} Transition definition
* @param [callback] {Function} Callback
* @return {TransitionableTransform}
*/
TransitionableTransform.prototype.setSkew = function setSkew(skewAngles, transition, callback) {
this.skew.set(skewAngles, transition, callback);
this._final = Transform.build({
translate: this.translate.get(),
rotate: this.rotate.get(),
scale: this.scale.get(),
skew: skewAngles
});
return this;
};
/**
* Setter for a TransitionableTransform with optional parameters to transition
* between Transforms
*
* @method set
* @chainable
*
* @param transform {Array} New transform state
* @param [transition] {Object} Transition definition
* @param [callback] {Function} Callback
* @return {TransitionableTransform}
*/
TransitionableTransform.prototype.set = function set(transform, transition, callback) {
this._final = transform;
var components = Transform.interpret(transform);
var _callback = callback ? Utility.after(4, callback) : null;
this.translate.set(components.translate, transition, _callback);
this.rotate.set(components.rotate, transition, _callback);
this.skew.set(components.skew, transition, _callback);
this.scale.set(components.scale, transition, _callback);
return this;
};
/**
* Sets the default transition to use for transitioning betwen Transform states
*
* @method setDefaultTransition
*
* @param transition {Object} Transition definition
*/
TransitionableTransform.prototype.setDefaultTransition = function setDefaultTransition(transition) {
this.translate.setDefault(transition);
this.rotate.setDefault(transition);
this.skew.setDefault(transition);
this.scale.setDefault(transition);
};
/**
* Getter. Returns the current state of the Transform
*
* @method get
*
* @return {Transform}
*/
TransitionableTransform.prototype.get = function get() {
if (this.isActive()) {
return _build.call(this);
}
else return this._final;
};
/**
* Get the destination state of the Transform
*
* @method getFinal
*
* @return Transform {Transform}
*/
TransitionableTransform.prototype.getFinal = function getFinal() {
return this._final;
};
/**
* Determine if the TransitionalTransform is currently transitioning
*
* @method isActive
*
* @return {Boolean}
*/
TransitionableTransform.prototype.isActive = function isActive() {
return this.translate.isActive() || this.rotate.isActive() || this.scale.isActive() || this.skew.isActive();
};
/**
* Halts the transition
*
* @method halt
*/
TransitionableTransform.prototype.halt = function halt() {
this._final = this.get();
this.translate.halt();
this.rotate.halt();
this.skew.halt();
this.scale.halt();
};
module.exports = TransitionableTransform;
});