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
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
base.require('base.event_target');
base.require('base.raf');
base.require('ui.animation');
base.exportTo('ui', function() {
/**
* Manages execution, queueing and blending of ui.Animations against
* a single target.
*
* Targets must have a cloneAnimationState() method that returns all the
* animatable states of that target.
*
* @constructor
* @extends {base.EventTarget}
*/
function AnimationController() {
base.EventTarget.call(this);
this.target_ = undefined;
this.activeAnimation_ = undefined;
this.tickScheduled_ = false;
}
AnimationController.prototype = {
__proto__: base.EventTarget.prototype,
get target() {
return this.target_;
},
set target(target) {
if (this.activeAnimation_)
throw new Error('Cannot change target while animation is running.');
if (target.cloneAnimationState === undefined ||
typeof target.cloneAnimationState !== 'function')
throw new Error('target must have a cloneAnimationState function');
this.target_ = target;
},
get activeAnimation() {
return this.activeAnimation_;
},
get hasActiveAnimation() {
return !!this.activeAnimation_;
},
queueAnimation: function(animation, opt_now) {
if (this.target_ === undefined)
throw new Error('Cannot queue animations without a target');
var now;
if (opt_now !== undefined)
now = opt_now;
else
now = window.performance.now();
if (this.activeAnimation_) {
// Must tick the animation before stopping it case its about to stop,
// and to update the target with its final sets of edits up to this
// point.
var done = this.activeAnimation_.tick(now, this.target_);
if (done)
this.activeAnimation_ = undefined;
}
if (this.activeAnimation_) {
if (animation.canTakeOverFor(this.activeAnimation_)) {
this.activeAnimation_.didStopEarly(now, this.target_, true);
animation.takeOverFor(this.activeAnimation_, now, this.target_);
} else {
this.activeAnimation_.didStopEarly(now, this.target_, false);
}
}
this.activeAnimation_ = animation;
this.activeAnimation_.start(now, this.target_);
if (this.tickScheduled_)
return;
this.tickScheduled_ = true;
base.requestAnimationFrame(this.tickActiveAnimation_, this);
},
cancelActiveAnimation: function(opt_now) {
if (!this.activeAnimation_)
return;
var now;
if (opt_now !== undefined)
now = opt_now;
else
now = window.performance.now();
this.activeAnimation_.didStopEarly(now, this.target_, false);
this.activeAnimation_ = undefined;
},
tickActiveAnimation_: function(frameBeginTime) {
this.tickScheduled_ = false;
if (!this.activeAnimation_)
return;
if (this.target_ === undefined) {
this.activeAnimation_.didStopEarly(frameBeginTime, this.target_, false);
return;
}
var oldTargetState = this.target_.cloneAnimationState();
var done = this.activeAnimation_.tick(frameBeginTime, this.target_);
if (done)
this.activeAnimation_ = undefined;
if (this.activeAnimation_) {
this.tickScheduled_ = true;
base.requestAnimationFrame(this.tickActiveAnimation_, this);
}
if (oldTargetState) {
var e = new Event('didtick');
e.oldTargetState = oldTargetState;
this.dispatchEvent(e, false, false);
}
}
};
return {
AnimationController: AnimationController
};
});
|