1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
6 * @requires OpenLayers/Console.js
10 * Namespace: OpenLayers.Tween
12 OpenLayers.Tween = OpenLayers.Class({
16 * {int} Interval in milliseconds between 2 steps
22 * {<OpenLayers.Easing>(Function)} Easing equation used for the animation
23 * Defaultly set to OpenLayers.Easing.Expo.easeOut
29 * {Object} Values to start the animation with
35 * {Object} Values to finish the animation with
40 * APIProperty: duration
41 * {int} duration of the tween (number of steps)
46 * APIProperty: callbacks
47 * {Object} An object with start, eachStep and done properties whose values
48 * are functions to be call during the animation. They are passed the
49 * current computed value as argument.
61 * {int} Interval id returned by window.setInterval
67 * {Boolean} Tells if the easing is currently playing
72 * Constructor: OpenLayers.Tween
76 * easing - {<OpenLayers.Easing>(Function)} easing function method to use
78 initialize: function(easing) {
79 this.easing = (easing) ? easing : OpenLayers.Easing.Expo.easeOut;
84 * Plays the Tween, and calls the callback method on each step
87 * begin - {Object} values to start the animation with
88 * finish - {Object} values to finish the animation with
89 * duration - {int} duration of the tween (number of steps)
90 * options - {Object} hash of options (for example callbacks (start, eachStep, done))
92 start: function(begin, finish, duration, options) {
96 this.duration = duration;
97 this.callbacks = options.callbacks;
100 window.clearInterval(this.interval);
101 this.interval = null;
103 if (this.callbacks && this.callbacks.start) {
104 this.callbacks.start.call(this, this.begin);
106 this.interval = window.setInterval(
107 OpenLayers.Function.bind(this.play, this), this.INTERVAL);
112 * Stops the Tween, and calls the done callback
113 * Doesn't do anything if animation is already finished
120 if (this.callbacks && this.callbacks.done) {
121 this.callbacks.done.call(this, this.finish);
123 window.clearInterval(this.interval);
124 this.interval = null;
125 this.playing = false;
130 * Calls the appropriate easing method
134 for (var i in this.begin) {
135 var b = this.begin[i];
136 var f = this.finish[i];
137 if (b == null || f == null || isNaN(b) || isNaN(f)) {
138 OpenLayers.Console.error('invalid value for Tween');
142 value[i] = this.easing.apply(this, [this.time, b, c, this.duration]);
146 if (this.callbacks && this.callbacks.eachStep) {
147 this.callbacks.eachStep.call(this, value);
150 if (this.time > this.duration) {
151 if (this.callbacks && this.callbacks.done) {
152 this.callbacks.done.call(this, this.finish);
153 this.playing = false;
155 window.clearInterval(this.interval);
156 this.interval = null;
161 * Create empty functions for all easing methods.
163 CLASS_NAME: "OpenLayers.Tween"
167 * Namespace: OpenLayers.Easing
170 * Easing Equations by Robert Penner, <http://www.robertpenner.com/easing/>
172 OpenLayers.Easing = {
174 * Create empty functions for all easing methods.
176 CLASS_NAME: "OpenLayers.Easing"
180 * Namespace: OpenLayers.Easing.Linear
182 OpenLayers.Easing.Linear = {
189 * b - {Float} beginning position
190 * c - {Float} total change
191 * d - {Float} duration of the transition
193 easeIn: function(t, b, c, d) {
202 * b - {Float} beginning position
203 * c - {Float} total change
204 * d - {Float} duration of the transition
206 easeOut: function(t, b, c, d) {
211 * Function: easeInOut
215 * b - {Float} beginning position
216 * c - {Float} total change
217 * d - {Float} duration of the transition
219 easeInOut: function(t, b, c, d) {
223 CLASS_NAME: "OpenLayers.Easing.Linear"
227 * Namespace: OpenLayers.Easing.Expo
229 OpenLayers.Easing.Expo = {
236 * b - {Float} beginning position
237 * c - {Float} total change
238 * d - {Float} duration of the transition
240 easeIn: function(t, b, c, d) {
241 return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
249 * b - {Float} beginning position
250 * c - {Float} total change
251 * d - {Float} duration of the transition
253 easeOut: function(t, b, c, d) {
254 return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
258 * Function: easeInOut
262 * b - {Float} beginning position
263 * c - {Float} total change
264 * d - {Float} duration of the transition
266 easeInOut: function(t, b, c, d) {
268 if (t==d) return b+c;
269 if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
270 return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
273 CLASS_NAME: "OpenLayers.Easing.Expo"
277 * Namespace: OpenLayers.Easing.Quad
279 OpenLayers.Easing.Quad = {
286 * b - {Float} beginning position
287 * c - {Float} total change
288 * d - {Float} duration of the transition
290 easeIn: function(t, b, c, d) {
291 return c*(t/=d)*t + b;
299 * b - {Float} beginning position
300 * c - {Float} total change
301 * d - {Float} duration of the transition
303 easeOut: function(t, b, c, d) {
304 return -c *(t/=d)*(t-2) + b;
308 * Function: easeInOut
312 * b - {Float} beginning position
313 * c - {Float} total change
314 * d - {Float} duration of the transition
316 easeInOut: function(t, b, c, d) {
317 if ((t/=d/2) < 1) return c/2*t*t + b;
318 return -c/2 * ((--t)*(t-2) - 1) + b;
321 CLASS_NAME: "OpenLayers.Easing.Quad"