| 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 |
1×
1×
2×
3×
3×
38×
38×
38×
38×
38×
38×
38×
48×
48×
48×
3×
3×
38×
48×
3×
3×
3×
45×
38×
38×
3×
38×
38×
38×
2×
2×
2×
2×
46×
44×
44×
38×
2×
2×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
| import Base from '../core/Base';
import Animator from './Animator';
var requestAnimationFrame = window.requestAnimationFrame
|| window.msRequestAnimationFrame
|| window.mozRequestAnimationFrame
|| window.webkitRequestAnimationFrame
|| function (func){ setTimeout(func, 16); };
/**
* Animation is global timeline that schedule all clips. each frame animation will set the time of clips to current and update the states of clips
* @constructor clay.animation.Animation
* @extends clay.core.Base
*
* @example
* var animation = new clay.animation.Animation();
* var node = new clay.Node();
* animation.animate(node.position)
* .when(1000, {
* x: 500,
* y: 500
* })
* .when(2000, {
* x: 100,
* y: 100
* })
* .when(3000, {
* z: 10
* })
* .start('spline');
*/
var Timeline = Base.extend(function () {
return /** @lends clay.animation.Timeline# */{
/**
* stage is an object with render method, each frame if there exists any animating clips, stage.render will be called
* @type {Object}
*/
stage: null,
_clips: [],
_running: false,
_time: 0,
_paused: false,
_pausedTime: 0
};
},
/** @lends clay.animation.Timeline.prototype */
{
/**
* Add animator
* @param {clay.animate.Animator} animator
*/
addAnimator: function (animator) {
animator.animation = this;
var clips = animator.getClips();
for (var i = 0; i < clips.length; i++) {
this.addClip(clips[i]);
}
},
/**
* @param {clay.animation.Clip} clip
*/
addClip: function (clip) {
Eif (this._clips.indexOf(clip) < 0) {
this._clips.push(clip);
}
},
/**
* @param {clay.animation.Clip} clip
*/
removeClip: function (clip) {
var idx = this._clips.indexOf(clip);
if (idx >= 0) {
this._clips.splice(idx, 1);
}
},
/**
* Remove animator
* @param {clay.animate.Animator} animator
*/
removeAnimator: function (animator) {
var clips = animator.getClips();
for (var i = 0; i < clips.length; i++) {
this.removeClip(clips[i]);
}
animator.animation = null;
},
_update: function () {
var time = Date.now() - this._pausedTime;
var delta = time - this._time;
var clips = this._clips;
var len = clips.length;
var deferredEvents = [];
var deferredClips = [];
for (var i = 0; i < len; i++) {
var clip = clips[i];
var e = clip.step(time, delta, false);
// Throw out the events need to be called after
// stage.render, like finish
if (e) {
deferredEvents.push(e);
deferredClips.push(clip);
}
}
// Remove the finished clip
for (var i = 0; i < len;) {
if (clips[i]._needsRemove) {
clips[i] = clips[len-1];
clips.pop();
len--;
} else {
i++;
}
}
len = deferredEvents.length;
for (var i = 0; i < len; i++) {
deferredClips[i].fire(deferredEvents[i]);
}
this._time = time;
this.trigger('frame', delta);
Iif (this.stage && this.stage.render) {
this.stage.render();
}
},
/**
* Start running animation
*/
start: function () {
var self = this;
this._running = true;
this._time = Date.now();
this._pausedTime = 0;
function step() {
if (self._running) {
requestAnimationFrame(step);
if (!self._paused) {
self._update();
}
}
}
requestAnimationFrame(step);
},
/**
* Stop running animation
*/
stop: function () {
this._running = false;
},
/**
* Pause
*/
pause: function () {
Eif (!this._paused) {
this._pauseStart = Date.now();
this._paused = true;
}
},
/**
* Resume
*/
resume: function () {
Eif (this._paused) {
this._pausedTime += Date.now() - this._pauseStart;
this._paused = false;
}
},
/**
* Remove all clips
*/
removeClipsAll: function () {
this._clips = [];
},
/**
* Create a animator
* @param {Object} target
* @param {Object} [options]
* @param {boolean} [options.loop]
* @param {Function} [options.getter]
* @param {Function} [options.setter]
* @param {Function} [options.interpolater]
* @return {clay.animation.Animator}
*/
animate: function (target, options) {
options = options || {};
var animator = new Animator(
target,
options.loop,
options.getter,
options.setter,
options.interpolater
);
animator.animation = this;
return animator;
}
});
export default Timeline;
|