| | 1 | /* Copyright (c) 2006-2007 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. */ |
|---|
| | 4 | |
|---|
| | 5 | /** |
|---|
| | 6 | * Namespace: OpenLayers.Tween |
|---|
| | 7 | */ |
|---|
| | 8 | OpenLayers.Tween = OpenLayers.Class({ |
|---|
| | 9 | |
|---|
| | 10 | /** |
|---|
| | 11 | * Constant: INTERVAL |
|---|
| | 12 | * {int} Interval in milliseconds between 2 steps |
|---|
| | 13 | */ |
|---|
| | 14 | INTERVAL: 10, |
|---|
| | 15 | |
|---|
| | 16 | /** |
|---|
| | 17 | * Property: easing |
|---|
| | 18 | * {<OpenLayers.Easing>(Function)} Easing equation used for the animation |
|---|
| | 19 | * Defaultly set to OpenLayers.Easing.Expo.easeOut |
|---|
| | 20 | */ |
|---|
| | 21 | easing: null, |
|---|
| | 22 | |
|---|
| | 23 | /** |
|---|
| | 24 | * Property: begin |
|---|
| | 25 | * {Object} Values to start the animation with |
|---|
| | 26 | */ |
|---|
| | 27 | begin: null, |
|---|
| | 28 | |
|---|
| | 29 | /** |
|---|
| | 30 | * Property: finish |
|---|
| | 31 | * {Object} Values to finish the animation with |
|---|
| | 32 | */ |
|---|
| | 33 | finish: null, |
|---|
| | 34 | |
|---|
| | 35 | /** |
|---|
| | 36 | * Property: duration |
|---|
| | 37 | * {int} duration of the tween (number of steps) |
|---|
| | 38 | */ |
|---|
| | 39 | duration: null, |
|---|
| | 40 | |
|---|
| | 41 | /** |
|---|
| | 42 | * Property: callbacks |
|---|
| | 43 | * {Object} An object with start, eachStep and done properties whose values |
|---|
| | 44 | * are functions to be call during the animation. They are passed the |
|---|
| | 45 | * current computed value as argument. |
|---|
| | 46 | */ |
|---|
| | 47 | callbacks: null, |
|---|
| | 48 | |
|---|
| | 49 | /** |
|---|
| | 50 | * Property: time |
|---|
| | 51 | * {int} Step counter |
|---|
| | 52 | */ |
|---|
| | 53 | time: null, |
|---|
| | 54 | |
|---|
| | 55 | /** |
|---|
| | 56 | * Property: interval |
|---|
| | 57 | * {int} Interval id returned by window.setInterval |
|---|
| | 58 | */ |
|---|
| | 59 | interval: null, |
|---|
| | 60 | |
|---|
| | 61 | /** |
|---|
| | 62 | * |
|---|
| | 63 | * Params: |
|---|
| | 64 | * easing - {<OpenLayers.Easing>(Function)} easing function method to use |
|---|
| | 65 | */ |
|---|
| | 66 | initialize: function(easing) { |
|---|
| | 67 | this.easing = (easing) ? easing : OpenLayers.Easing.Expo.easeOut; |
|---|
| | 68 | }, |
|---|
| | 69 | |
|---|
| | 70 | /** |
|---|
| | 71 | * APIMethod: start |
|---|
| | 72 | * Plays the Tween, and calls the callback method on each step |
|---|
| | 73 | * |
|---|
| | 74 | * begin - {Object} values to start the animation with |
|---|
| | 75 | * finish - {Object} values to finish the animation with |
|---|
| | 76 | * duration - {int} duration of the tween (number of steps) |
|---|
| | 77 | * options - {Object} hash of options (for example callbacks (start, eachStep, done)) |
|---|
| | 78 | */ |
|---|
| | 79 | start: function(begin, finish, duration, options) { |
|---|
| | 80 | this.begin = begin; |
|---|
| | 81 | this.finish = finish; |
|---|
| | 82 | this.duration = duration; |
|---|
| | 83 | this.callbacks = options.callbacks; |
|---|
| | 84 | this.time = 0; |
|---|
| | 85 | if (this.interval) { |
|---|
| | 86 | window.clearInterval(this.interval); |
|---|
| | 87 | this.interval = null; |
|---|
| | 88 | } |
|---|
| | 89 | if (this.callbacks && this.callbacks.start) { |
|---|
| | 90 | this.callbacks.start.call(this, this.begin); |
|---|
| | 91 | } |
|---|
| | 92 | this.interval = window.setInterval( |
|---|
| | 93 | OpenLayers.Function.bind(this.play, this), this.INTERVAL); |
|---|
| | 94 | }, |
|---|
| | 95 | |
|---|
| | 96 | /** |
|---|
| | 97 | * APIMethod: stop |
|---|
| | 98 | * Stops the Tween, and calls the finish callback |
|---|
| | 99 | */ |
|---|
| | 100 | stop: function() { |
|---|
| | 101 | if (this.callbacks && this.callbacks.done) { |
|---|
| | 102 | this.callbacks.done.call(this, this.finish); |
|---|
| | 103 | } |
|---|
| | 104 | window.clearInterval(this.interval); |
|---|
| | 105 | this.interval = null; |
|---|
| | 106 | }, |
|---|
| | 107 | |
|---|
| | 108 | /** |
|---|
| | 109 | * Method: play |
|---|
| | 110 | * Calls the appropriate easing method |
|---|
| | 111 | */ |
|---|
| | 112 | play: function() { |
|---|
| | 113 | var value = {}; |
|---|
| | 114 | for (var i in this.begin) { |
|---|
| | 115 | var b = this.begin[i]; |
|---|
| | 116 | var f = this.finish[i]; |
|---|
| | 117 | if (b == null || f == null || isNaN(b) || isNaN(f)) { |
|---|
| | 118 | OpenLayers.Console.error('invalid value for Tween'); |
|---|
| | 119 | } |
|---|
| | 120 | |
|---|
| | 121 | var c = f - b; |
|---|
| | 122 | value[i] = this.easing.apply(this, [this.time, b, c, this.duration]); |
|---|
| | 123 | } |
|---|
| | 124 | this.time++; |
|---|
| | 125 | |
|---|
| | 126 | if (this.callbacks && this.callbacks.eachStep) { |
|---|
| | 127 | this.callbacks.eachStep.call(this, value); |
|---|
| | 128 | } |
|---|
| | 129 | |
|---|
| | 130 | if (this.time > this.duration) { |
|---|
| | 131 | if (this.callbacks && this.callbacks.done) { |
|---|
| | 132 | this.callbacks.done.call(this, this.finish); |
|---|
| | 133 | } |
|---|
| | 134 | window.clearInterval(this.interval); |
|---|
| | 135 | this.interval = null; |
|---|
| | 136 | } |
|---|
| | 137 | }, |
|---|
| | 138 | |
|---|
| | 139 | /** |
|---|
| | 140 | * Create empty functions for all easing methods. |
|---|
| | 141 | */ |
|---|
| | 142 | CLASS_NAME: "OpenLayers.Tween" |
|---|
| | 143 | }); |
|---|
| | 144 | |
|---|
| | 145 | /** |
|---|
| | 146 | * Namespace: OpenLayers.Easing |
|---|
| | 147 | * |
|---|
| | 148 | * Credits: |
|---|
| | 149 | * Easing Equations by Robert Penner, <http://www.robertpenner.com/easing/> |
|---|
| | 150 | */ |
|---|
| | 151 | OpenLayers.Easing = { |
|---|
| | 152 | /** |
|---|
| | 153 | * Create empty functions for all easing methods. |
|---|
| | 154 | */ |
|---|
| | 155 | CLASS_NAME: "OpenLayers.Easing" |
|---|
| | 156 | } |
|---|
| | 157 | |
|---|
| | 158 | /** |
|---|
| | 159 | * Namespace: OpenLayers.Easing.Linear |
|---|
| | 160 | */ |
|---|
| | 161 | OpenLayers.Easing.Linear = { |
|---|
| | 162 | |
|---|
| | 163 | /** |
|---|
| | 164 | * APIFunction: easeIn |
|---|
| | 165 | * |
|---|
| | 166 | * Parameters: |
|---|
| | 167 | * t - {Float} time |
|---|
| | 168 | * b - {Float} beginning position |
|---|
| | 169 | * c - {Float} total change |
|---|
| | 170 | * d - {Float} duration of the transition |
|---|
| | 171 | */ |
|---|
| | 172 | easeIn: function(t, b, c, d) { |
|---|
| | 173 | return c*t/d + b; |
|---|
| | 174 | }, |
|---|
| | 175 | |
|---|
| | 176 | /** |
|---|
| | 177 | * APIFunction: easeOut |
|---|
| | 178 | * |
|---|
| | 179 | * Parameters: |
|---|
| | 180 | * t - {Float} time |
|---|
| | 181 | * b - {Float} beginning position |
|---|
| | 182 | * c - {Float} total change |
|---|
| | 183 | * d - {Float} duration of the transition |
|---|
| | 184 | */ |
|---|
| | 185 | easeOut: function(t, b, c, d) { |
|---|
| | 186 | return c*t/d + b; |
|---|
| | 187 | }, |
|---|
| | 188 | |
|---|
| | 189 | /** |
|---|
| | 190 | * APIFunction: easeInOut |
|---|
| | 191 | * |
|---|
| | 192 | * Parameters: |
|---|
| | 193 | * t - {Float} time |
|---|
| | 194 | * b - {Float} beginning position |
|---|
| | 195 | * c - {Float} total change |
|---|
| | 196 | * d - {Float} duration of the transition |
|---|
| | 197 | */ |
|---|
| | 198 | easeInOut: function(t, b, c, d) { |
|---|
| | 199 | return c*t/d + b; |
|---|
| | 200 | }, |
|---|
| | 201 | |
|---|
| | 202 | CLASS_NAME: "OpenLayers.Easing.Linear" |
|---|
| | 203 | }; |
|---|
| | 204 | |
|---|
| | 205 | /** |
|---|
| | 206 | * Namespace: OpenLayers.Easing.Expo |
|---|
| | 207 | */ |
|---|
| | 208 | OpenLayers.Easing.Expo = { |
|---|
| | 209 | |
|---|
| | 210 | /** |
|---|
| | 211 | * APIFunction: easeIn |
|---|
| | 212 | * |
|---|
| | 213 | * Parameters: |
|---|
| | 214 | * t - {Float} time |
|---|
| | 215 | * b - {Float} beginning position |
|---|
| | 216 | * c - {Float} total change |
|---|
| | 217 | * d - {Float} duration of the transition |
|---|
| | 218 | */ |
|---|
| | 219 | easeIn: function(t, b, c, d) { |
|---|
| | 220 | return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b; |
|---|
| | 221 | }, |
|---|
| | 222 | |
|---|
| | 223 | /** |
|---|
| | 224 | * APIFunction: easeOut |
|---|
| | 225 | * |
|---|
| | 226 | * Parameters: |
|---|
| | 227 | * t - {Float} time |
|---|
| | 228 | * b - {Float} beginning position |
|---|
| | 229 | * c - {Float} total change |
|---|
| | 230 | * d - {Float} duration of the transition |
|---|
| | 231 | */ |
|---|
| | 232 | easeOut: function(t, b, c, d) { |
|---|
| | 233 | return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b; |
|---|
| | 234 | }, |
|---|
| | 235 | |
|---|
| | 236 | /** |
|---|
| | 237 | * APIFunction: easeInOut |
|---|
| | 238 | * |
|---|
| | 239 | * Parameters: |
|---|
| | 240 | * t - {Float} time |
|---|
| | 241 | * b - {Float} beginning position |
|---|
| | 242 | * c - {Float} total change |
|---|
| | 243 | * d - {Float} duration of the transition |
|---|
| | 244 | */ |
|---|
| | 245 | easeInOut: function(t, b, c, d) { |
|---|
| | 246 | if (t==0) return b; |
|---|
| | 247 | if (t==d) return b+c; |
|---|
| | 248 | if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b; |
|---|
| | 249 | return c/2 * (-Math.pow(2, -10 * --t) + 2) + b; |
|---|
| | 250 | }, |
|---|
| | 251 | |
|---|
| | 252 | CLASS_NAME: "OpenLayers.Easing.Expo" |
|---|
| | 253 | }; |