-
Notifications
You must be signed in to change notification settings - Fork 6
/
animation.js
325 lines (301 loc) · 10.2 KB
/
animation.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
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
"use strict";
var paper = require("./getPaper");
var Tween = require("./tween");
var frameManager = require("./frameManager");
var easing = require("./easing");
/**
* Animation class. Default settings are :
*
* ````
* var defaults = {
* duration: 400,
* easing: "linear",
* complete: undefined,
* step: undefined,
* delay: 0,
* repeat: 0
* };
* ````
* @class Animation
* @constructor
* @param {Object} item a paper.js Item instance, which will be animated.
* @param {Object} properties properties to animate
* @param {Object} settings
* @param {Number} settings.duration Duration of the animation, in ms
* @param {Number} settings.delay delay before running the animation, in ms
* @param {String} settings.easing
* @param {Function} settings.complete Called when the animation is over, in `.end()`. The item is passed as this, the animation as 1st argument
* @param {Function} settings.step Called on each `.tick()`
* @param {Mixed} settings.repeat function or true or an integer. The animation will repeat as long as function returns `true`, `true` or `repeat` > 0, decrementing by 1 each time.
*/
function Animation(item, properties, settings, _continue) {
var self = this;
/**
* True if the animation is stopped
* @property {Bool} stopped
*/
self.stopped = false;
/**
* Time when the Animation is created
* @property {Timestamp} startTime
* @readonly
*/
self.startTime = new Date().getTime();
/**
* Settings, after being normalized in {{#crossLink "_initializeSettings"}}{{/crossLink}}
* @property {Object} settings
*/
self.settings = _initializeSettings(settings);
/**
* The animated `paper.Item`
* @property {Object} item
* @readonly
*/
self.item = item;
/**
* If provided, use parentItem to use .data and .onFrame. If not, use self.item;
* @property {Object} itemForAnimations
* @readonly
*/
self.itemForAnimations = self.settings.parentItem || self.item;
/**
* Repeat parameter.
* If Function, the animation is repeated as long as the function returns `true`.
* If `true`, the animation is repeated until `.end(true)` is called.
* If `repeat` is an integer, the animation is repeated until `repeat` is <= 0.
* Default `0`
* @property {Mixed} repeat
*/
self.repeat = self.settings.repeat || 0;
if (typeof self.settings.repeat === "function") {
var _repeatCallback = self.settings.repeat;
self.repeatCallback = function() {
if (!!_repeatCallback(item, self)) {
return new Animation(item, properties, settings, _continue);
}
return null;
};
} else {
if (self.repeat === true || self.repeat > 0) {
self.repeatCallback = function(newRepeat) {
settings.repeat = newRepeat;
// used for the repeat feature
return new Animation(item, properties, settings, _continue);
};
}
}
/**
* {{#crossLink "Tween"}}{{/crossLink}}s used by the Animation.
* @property {Array} tweens
*/
self.tweens = [];
/**
* If the Animation is in `onFrame` mode :
* Identifier of the {{#crossLink "frameMamanger"}}{{/crossLink}} callback called on every tick.
* @property {String} ticker
* @readonly
*/
self.ticker = null;
/**
* Callback used when queueing animations.
* @property {Function} _continue
* @readonly
* @private
*/
self._continue = _continue;
// store the reference to the animation in the item's data
if (typeof self.itemForAnimations.data === "undefined") {
self.itemForAnimations.data = {};
}
if (typeof self.itemForAnimations.data._animatePaperAnims === "undefined") {
self.itemForAnimations.data._animatePaperAnims = [];
}
/**
* Index of the animation in the item's queue.
* @property {Number} _dataIndex
* @readonly
* @private
*/
self._dataIndex = self.itemForAnimations.data._animatePaperAnims.length;
self.itemForAnimations.data._animatePaperAnims[self._dataIndex] = self;
for (var i in properties) {
if (properties.hasOwnProperty(i)) {
self.tweens.push(new Tween(i, properties[i], self));
}
}
if (self.settings.mode === "onFrame") {
self.ticker = frameManager.add(self.itemForAnimations, "_animate" + self.startTime + (Math.floor(Math.random() * (1000 - 1)) + 1), function() {
self.tick();
});
}
}
/**
* Called on each step of the animation.
*
* @method tick
*/
Animation.prototype.tick = function() {
var self = this;
if (!!self.stopped) return false;
var currentTime = new Date().getTime();
if( self.startTime + self.settings.delay > currentTime ){
return false;
}
var remaining = Math.max(0, self.startTime + self.settings.delay + self.settings.duration - currentTime);
var temp = remaining / self.settings.duration || 0;
var percent = 1 - temp;
for (var i = 0, l = self.tweens.length; i < l; i++) {
self.tweens[i].run(percent);
}
if (typeof self.settings.step !== "undefined") {
self.settings.step.call(self.item, {
percent: percent,
remaining: remaining
});
}
if (typeof self.settings.parentItem !== "undefined") {
self.settings.parentItem.project.view.draw();
} else {
self.item.project.view.draw();
}
// if the Animation is in timeout mode, we must force a View update
if (self.settings.mode === "timeout") {
//
}
if (percent < 1 && l) {
return remaining;
} else {
self.end();
return false;
}
};
/**
* Interrupts the animation. If `goToEnd` is true, all the properties are set to their final value.
* @method stop
* @param {Bool} goToEnd
* @param {Bool} forceEnd to prevent loops
*/
Animation.prototype.stop = function(goToEnd, forceEnd) {
var self = this;
var i = 0;
var l = goToEnd ? self.tweens.length : 0;
if (!!self.stopped) return self;
self.stopped = true;
for (; i < l; i++) {
self.tweens[i].run(1);
}
if (!!goToEnd) {
// stop further animation
if (!!self._continue) self._continue = null;
self.end(forceEnd);
}
};
/**
* Called when the animations ends, naturally or using `.stop(true)`.
* @method end
*/
Animation.prototype.end = function(forceEnd) {
var self = this;
if (self.settings.mode === "onFrame") {
frameManager.remove(self.itemForAnimations, self.ticker);
}
if (typeof self.settings.complete !== "undefined") {
self.settings.complete.call(self.item, this);
}
// if the Animation is in timeout mode, we must force a View update
if (self.settings.mode === "timeout") {
//
}
if (typeof self._continue === "function") {
self._continue.call(self.item);
}
// remove all references to the animation
self.itemForAnimations.data._animatePaperAnims[self._dataIndex] = null;
if (!!forceEnd || typeof self.repeatCallback !== "function") {
self = null;
} else {
// repeat
var newRepeat = self.repeat;
if (self.repeat !== true) {
newRepeat = self.repeat - 1;
}
return self.repeatCallback(newRepeat);
}
};
/**
* Normalizes existing values from an Animation settings argument
* and provides default values if needed.
*
* @method _initializeSettings
* @param {mixed} settings a `settings` object or undefined
* @private
*/
function _initializeSettings(settings) {
var defaults = {
duration: 400,
delay: 0,
repeat: 0,
easing: "linear",
complete: undefined,
step: undefined,
mode: "onFrame"
};
if (typeof settings === "undefined") {
settings = {};
}
// .duration must exist, and be a positive Number
if (typeof settings.duration === "undefined") {
settings.duration = defaults.duration;
} else {
settings.duration = Number(settings.duration);
if (settings.duration < 1) {
settings.duration = defaults.duration;
}
}
// .delay must exist, and be a positive Number
if (typeof settings.delay === "undefined") {
settings.delay = defaults.delay;
} else {
settings.delay = Number(settings.delay);
if (settings.delay < 1) {
settings.delay = defaults.delay;
}
}
// .repeat must exist, and be a positive Number or true
if (typeof settings.repeat === "undefined") {
settings.repeat = defaults.repeat;
}
else if (typeof settings.repeat === "function") {
// ok
} else {
if (settings.repeat !== true) {
settings.repeat = Number(settings.repeat);
if (settings.repeat < 0) {
settings.repeat = defaults.repeat;
}
}
}
// .easing must be defined in `easing`
if (typeof settings.easing === "undefined") {
settings.easing = defaults.easing;
}
if (typeof easing[settings.easing] !== "undefined" && easing.hasOwnProperty(settings.easing)) {
settings.easingFunction = easing[settings.easing];
} else {
settings.easing = defaults.easing;
settings.easingFunction = easing[defaults.easing];
}
// callbacks must be functions
if (typeof settings.complete !== "function") {
settings.complete = undefined;
}
if (typeof settings.step !== "function") {
settings.step = undefined;
}
// .mode must be either "onFrame" or "timeout"
if (["onFrame", "timeout"].indexOf(settings.mode) === -1) {
settings.mode = defaults.mode;
}
return settings;
}
module.exports = Animation;