Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/fx/anim/anim.js
1864 views
1
// Copyright 2006 The Closure Library Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS-IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
/**
16
* @fileoverview Basic animation controls.
17
*
18
* @author [email protected] (Erik Arvidsson)
19
*/
20
goog.provide('goog.fx.anim');
21
goog.provide('goog.fx.anim.Animated');
22
23
goog.require('goog.async.AnimationDelay');
24
goog.require('goog.async.Delay');
25
goog.require('goog.object');
26
27
28
29
/**
30
* An interface for programatically animated objects. I.e. rendered in
31
* javascript frame by frame.
32
*
33
* @interface
34
*/
35
goog.fx.anim.Animated = function() {};
36
37
38
/**
39
* Function called when a frame is requested for the animation.
40
*
41
* @param {number} now Current time in milliseconds.
42
*/
43
goog.fx.anim.Animated.prototype.onAnimationFrame;
44
45
46
/**
47
* Default wait timeout for animations (in milliseconds). Only used for timed
48
* animation, which uses a timer (setTimeout) to schedule animation.
49
*
50
* @type {number}
51
* @const
52
*/
53
goog.fx.anim.TIMEOUT = goog.async.AnimationDelay.TIMEOUT;
54
55
56
/**
57
* A map of animations which should be cycled on the global timer.
58
*
59
* @type {!Object<number, goog.fx.anim.Animated>}
60
* @private
61
*/
62
goog.fx.anim.activeAnimations_ = {};
63
64
65
/**
66
* An optional animation window.
67
* @type {Window}
68
* @private
69
*/
70
goog.fx.anim.animationWindow_ = null;
71
72
73
/**
74
* An interval ID for the global timer or event handler uid.
75
* @type {goog.async.Delay|goog.async.AnimationDelay}
76
* @private
77
*/
78
goog.fx.anim.animationDelay_ = null;
79
80
81
/**
82
* Registers an animation to be cycled on the global timer.
83
* @param {goog.fx.anim.Animated} animation The animation to register.
84
*/
85
goog.fx.anim.registerAnimation = function(animation) {
86
var uid = goog.getUid(animation);
87
if (!(uid in goog.fx.anim.activeAnimations_)) {
88
goog.fx.anim.activeAnimations_[uid] = animation;
89
}
90
91
// If the timer is not already started, start it now.
92
goog.fx.anim.requestAnimationFrame_();
93
};
94
95
96
/**
97
* Removes an animation from the list of animations which are cycled on the
98
* global timer.
99
* @param {goog.fx.anim.Animated} animation The animation to unregister.
100
*/
101
goog.fx.anim.unregisterAnimation = function(animation) {
102
var uid = goog.getUid(animation);
103
delete goog.fx.anim.activeAnimations_[uid];
104
105
// If a timer is running and we no longer have any active timers we stop the
106
// timers.
107
if (goog.object.isEmpty(goog.fx.anim.activeAnimations_)) {
108
goog.fx.anim.cancelAnimationFrame_();
109
}
110
};
111
112
113
/**
114
* Tears down this module. Useful for testing.
115
*/
116
// TODO(nicksantos): Wow, this api is pretty broken. This should be fixed.
117
goog.fx.anim.tearDown = function() {
118
goog.fx.anim.animationWindow_ = null;
119
goog.dispose(goog.fx.anim.animationDelay_);
120
goog.fx.anim.animationDelay_ = null;
121
goog.fx.anim.activeAnimations_ = {};
122
};
123
124
125
/**
126
* Registers an animation window. This allows usage of the timing control API
127
* for animations. Note that this window must be visible, as non-visible
128
* windows can potentially stop animating. This window does not necessarily
129
* need to be the window inside which animation occurs, but must remain visible.
130
* See: https://developer.mozilla.org/en/DOM/window.mozRequestAnimationFrame.
131
*
132
* @param {Window} animationWindow The window in which to animate elements.
133
*/
134
goog.fx.anim.setAnimationWindow = function(animationWindow) {
135
// If a timer is currently running, reset it and restart with new functions
136
// after a timeout. This is to avoid mismatching timer UIDs if we change the
137
// animation window during a running animation.
138
//
139
// In practice this cannot happen before some animation window and timer
140
// control functions has already been set.
141
var hasTimer =
142
goog.fx.anim.animationDelay_ && goog.fx.anim.animationDelay_.isActive();
143
144
goog.dispose(goog.fx.anim.animationDelay_);
145
goog.fx.anim.animationDelay_ = null;
146
goog.fx.anim.animationWindow_ = animationWindow;
147
148
// If the timer was running, start it again.
149
if (hasTimer) {
150
goog.fx.anim.requestAnimationFrame_();
151
}
152
};
153
154
155
/**
156
* Requests an animation frame based on the requestAnimationFrame and
157
* cancelRequestAnimationFrame function pair.
158
* @private
159
*/
160
goog.fx.anim.requestAnimationFrame_ = function() {
161
if (!goog.fx.anim.animationDelay_) {
162
// We cannot guarantee that the global window will be one that fires
163
// requestAnimationFrame events (consider off-screen chrome extension
164
// windows). Default to use goog.async.Delay, unless
165
// the client has explicitly set an animation window.
166
if (goog.fx.anim.animationWindow_) {
167
// requestAnimationFrame will call cycleAnimations_ with the current
168
// time in ms, as returned from goog.now().
169
goog.fx.anim.animationDelay_ =
170
new goog.async.AnimationDelay(function(now) {
171
goog.fx.anim.cycleAnimations_(now);
172
}, goog.fx.anim.animationWindow_);
173
} else {
174
goog.fx.anim.animationDelay_ = new goog.async.Delay(function() {
175
goog.fx.anim.cycleAnimations_(goog.now());
176
}, goog.fx.anim.TIMEOUT);
177
}
178
}
179
180
var delay = goog.fx.anim.animationDelay_;
181
if (!delay.isActive()) {
182
delay.start();
183
}
184
};
185
186
187
/**
188
* Cancels an animation frame created by requestAnimationFrame_().
189
* @private
190
*/
191
goog.fx.anim.cancelAnimationFrame_ = function() {
192
if (goog.fx.anim.animationDelay_) {
193
goog.fx.anim.animationDelay_.stop();
194
}
195
};
196
197
198
/**
199
* Cycles through all registered animations.
200
* @param {number} now Current time in milliseconds.
201
* @private
202
*/
203
goog.fx.anim.cycleAnimations_ = function(now) {
204
goog.object.forEach(goog.fx.anim.activeAnimations_, function(anim) {
205
anim.onAnimationFrame(now);
206
});
207
208
if (!goog.object.isEmpty(goog.fx.anim.activeAnimations_)) {
209
goog.fx.anim.requestAnimationFrame_();
210
}
211
};
212
213