forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanimation.js
382 lines (341 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {getCurve} from './curve';
import {log} from './log';
import {timer} from './timer';
import {vsyncFor} from './vsync';
const TAG_ = 'Animation';
const NOOP_CALLBACK = function() {};
/**
* The animation class allows construction of arbitrary animation processes.
* The main method is "add" that adds a segment of animation at particular
* time offset (delay) and duration. All animation segments are simply functions
* of type Transition which are iterated from 0 to 1 in animation frames to
* achieve the desired effect.
*/
export class Animation {
/**
* Creates and starts animation with a single segment. Returns AnimationPlayer
* object that can be used to monitor or control animation.
*
* @param {!Transition<?>} transition Transition to animate.
* @param {timeDef} duration Duration in milliseconds.
* @param {(!Curve|string)=} opt_curve Optional curve to use for animation.
* Default is the linear animation.
* @return {!AnimationPlayer}
*/
static animate(transition, duration, opt_curve) {
return new Animation()
.setCurve(opt_curve)
.add(0, transition, 1)
.start(duration);
}
/**
* @param {!Vsync=} opt_vsync
*/
constructor(opt_vsync) {
/** @private @const */
this.vsync_ = opt_vsync || vsyncFor(window);
/** @private {?Curve} */
this.curve_ = null;
/**
* @private @const {!Array<!SegmentDef>}
*/
this.segments_ = [];
}
/**
* Sets the default curve for the animation. Each segment is allowed to have
* its own curve, but this curve will be used if a segment doesn't specify
* its own.
* @param {!Curve|string} curve
* @return {!Animation}
*/
setCurve(curve) {
this.curve_ = getCurve(curve);
return this;
}
/**
* Adds a segment to the animation. Each segment starts at offset (delay)
* and runs for a portion of the overall animation (duration). Note that
* both delay and duration and normtimeDef types which accept values from 0 to 1.
* Optionally, the time is pushed through a curve. If curve is not specified,
* the default animation curve will be used. The specified transition is
* animated over the specified duration from 0 to 1.
*
* @param {normtimeDef} delay
* @param {!Transition<?>} transition
* @param {normtimeDef} duration
* @param {(!Curve|string)=} opt_curve
* @return {!Animation}
*/
add(delay, transition, duration, opt_curve) {
this.segments_.push({delay: delay, func: transition, duration: duration,
curve: getCurve(opt_curve)});
return this;
}
/**
* Starts the animation and returns the AnimationPlayer object that can be
* used to monitor and control the animation.
*
* @param {timeDef} duration Absolute time in milliseconds.
* @return {!AnimationPlayer}
*/
start(duration) {
const player = new AnimationPlayer(this.vsync_, this.segments_, this.curve_,
duration);
player.start_();
return player;
}
}
/**
* AnimationPlayer allows tracking and monitoring of the running animation.
* Most importantly it exposes methods "then" and "thenAlways" that have the
* semantics of a Promise and signal when the animation completed or failed.
* Additionally, it exposes the method "halt" which allows to stop/reset the
* animation.
* @implements {IThenable}
*/
class AnimationPlayer {
/**
* @param {!Vsync} vsync
* @param {!Array<!SegmentDef>} segments
* @param {?Curve} defaultCurve
* @param {timeDef} duration
*/
constructor(vsync, segments, defaultCurve, duration) {
/** @private @const {!Vsync} */
this.vsync_ = vsync;
/** @private @const {!Array<!SegmentRuntimeDef>} */
this.segments_ = [];
for (let i = 0; i < segments.length; i++) {
const segment = segments[i];
this.segments_.push({
delay: segment.delay,
func: segment.func,
duration: segment.duration,
curve: segment.curve || defaultCurve,
started: false,
completed: false
});
}
/** @private @const */
this.duration_ = duration;
/** @private {timeDef} */
this.startTime_ = 0;
/** @private {normtimeDef} */
this.normLinearTime_ = 0;
/** @private {normtimeDef} */
this.normTime_ = 0;
/** @private {boolean} */
this.running_ = false;
/** @private {!Object<string, *>} */
this.state_ = {};
/** @const {function()} */
this.resolve_;
/** @const {function()} */
this.reject_;
/** @private {!Promise} */
this.promise_ = new Promise((resolve, reject) => {
this.resolve_ = resolve;
this.reject_ = reject;
});
/** @const */
this.task_ = this.vsync_.createAnimTask({
mutate: this.stepMutate_.bind(this)
});
}
/**
* Chains to the animation's promise that will resolve when the animation has
* completed or will reject if animation has failed or was interrupted.
* @param {!Function=} opt_resolve
* @param {!Function=} opt_reject
* @return {!Promise}
*/
then(opt_resolve, opt_reject) {
if (!opt_resolve && !opt_reject) {
return this.promise_;
}
return this.promise_.then(opt_resolve, opt_reject);
}
/**
* Callback for regardless whether the animation succeeds or fails.
* @param {!Function=} opt_callback
* @return {!Promise}
*/
thenAlways(opt_callback) {
const callback = opt_callback || NOOP_CALLBACK;
return this.then(callback, callback);
}
/**
* Halts the animation. Depending on the opt_dir value, the following actions
* can be performed:
* 0: No action. The state will be as at the moment of halting (default)
* 1: Final state. Transitionable will be set to state = 1.
* -1: Reset state. Transitionable will be reset to state = 0.
* The animation's promise will be rejected since the transition has been
* interrupted.
* @param {number=} opt_dir
*/
halt(opt_dir) {
this.complete_(/* success */ false, /* dir */ opt_dir || 0);
}
/**
* @private
*/
start_() {
this.startTime_ = timer.now();
this.running_ = true;
if (this.vsync_.canAnimate()) {
this.task_(this.state_);
} else {
log.warn(TAG_, 'cannot animate');
this.complete_(/* success */ false, /* dir */ 0);
}
}
/**
* @param {boolean} success
* @param {number} dir
* @private
*/
complete_(success, dir) {
if (!this.running_) {
return;
}
this.running_ = false;
if (dir != 0) {
// Sort in the completion order.
if (this.segments_.length > 1) {
this.segments_.sort((s1, s2) => {
return (s1.delay + s1.duration) - (s2.delay + s2.duration);
});
}
try {
if (dir > 0) {
// Natural order - all set to 1.
for (let i = 0; i < this.segments_.length; i++) {
this.segments_[i].func(1, true);
}
} else {
// Reverse order - all set to 0.
for (let i = this.segments_.length - 1; i >= 0; i--) {
this.segments_[i].func(0, false);
}
}
} catch (e) {
log.error(TAG_, 'completion failed: ' + e, e);
success = false;
}
}
if (success) {
this.resolve_();
} else {
this.reject_();
}
}
/**
* @param {!Object<string, *>} unusedState
* @private
*/
stepMutate_(unusedState) {
if (!this.running_) {
return;
}
const currentTime = timer.now();
const normLinearTime = Math.min((currentTime - this.startTime_) /
this.duration_, 1);
// Start segments due to be started
for (let i = 0; i < this.segments_.length; i++) {
const segment = this.segments_[i];
if (!segment.started && normLinearTime >= segment.delay) {
segment.started = true;
}
}
// Execute all pending segments.
for (let i = 0; i < this.segments_.length; i++) {
const segment = this.segments_[i];
if (!segment.started || segment.completed) {
continue;
}
this.mutateSegment_(segment, normLinearTime);
}
// Complete or start next cycle.
if (normLinearTime == 1) {
this.complete_(/* success */ true, /* dir */ 0);
} else {
if (this.vsync_.canAnimate()) {
this.task_(this.state_);
} else {
log.warn(TAG_, 'cancel animation');
this.complete_(/* success */ false, /* dir */ 0);
}
}
}
/**
* @param {!SegmentRuntimeDef} segment
* @param {number} totalLinearTime
*/
mutateSegment_(segment, totalLinearTime) {
let normLinearTime;
let normTime;
if (segment.duration > 0) {
normLinearTime = Math.min((totalLinearTime - segment.delay) /
segment.duration, 1);
normTime = normLinearTime;
if (segment.curve && normTime != 1) {
try {
normTime = segment.curve(normLinearTime);
} catch (e) {
log.error(TAG_, 'step curve failed: ' + e, e);
this.complete_(/* success */ false, /* dir */ 0);
return;
}
}
} else {
normLinearTime = 1;
normTime = 1;
}
if (normLinearTime == 1) {
segment.completed = true;
}
try {
segment.func(normTime, segment.completed);
} catch (e) {
log.error(TAG_, 'step mutate failed: ' + e, e);
this.complete_(/* success */ false, /* dir */ 0);
return;
}
}
}
/**
* @typedef {{
* delay: normtimeDef,
* func: !Transition,
* duration: normtimeDef,
* curve: ?Curve
* }}
*/
class SegmentDef {}
/**
* @typedef {{
* delay: normtimeDef,
* func: !Transition,
* duration: normtimeDef,
* curve: ?Curve,
* started: boolean,
* completed: boolean
* }}
*/
class SegmentRuntimeDef {}