| | 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 | * Interval in milliseconds between 2 steps |
|---|
| | 13 | */ |
|---|
| | 14 | INTERVAL: 10, |
|---|
| | 15 | |
|---|
| | 16 | /** |
|---|
| | 17 | * |
|---|
| | 18 | * Params: |
|---|
| | 19 | * easing - {<OpenLayers.Easing>(Function)} easing function method to use |
|---|
| | 20 | */ |
|---|
| | 21 | initialize: function(easing) { |
|---|
| | 22 | this.easing = easing; |
|---|
| | 23 | }, |
|---|
| | 24 | |
|---|
| | 25 | /** |
|---|
| | 26 | * APIMethod: start |
|---|
| | 27 | * Plays the Tween, and calls the callback method on each step |
|---|
| | 28 | * |
|---|
| | 29 | * begin - {int} begin value |
|---|
| | 30 | * finish - {int} finish value |
|---|
| | 31 | * duration - {int} duration of the tween (number of steps) |
|---|
| | 32 | * callback - {Function} callback called on each step |
|---|
| | 33 | */ |
|---|
| | 34 | start: function(begin, finish, duration, callback) { |
|---|
| | 35 | this.begin = begin; |
|---|
| | 36 | this.finish = finish; |
|---|
| | 37 | this.duration = duration; |
|---|
| | 38 | this.time = 0; |
|---|
| | 39 | if (this.interval) { |
|---|
| | 40 | window.clearInterval(this.interval); |
|---|
| | 41 | this.interval = null; |
|---|
| | 42 | } |
|---|
| | 43 | this.interval = window.setInterval( |
|---|
| | 44 | OpenLayers.Function.bind(this.ease, this, callback), this.INTERVAL); |
|---|
| | 45 | }, |
|---|
| | 46 | |
|---|
| | 47 | /** |
|---|
| | 48 | * Method: ease |
|---|
| | 49 | * Calls the appropriate easing method |
|---|
| | 50 | * |
|---|
| | 51 | * callback - {Function} callback |
|---|
| | 52 | */ |
|---|
| | 53 | ease: function(callback) { |
|---|
| | 54 | if (this.time >= this.duration) { |
|---|
| | 55 | window.clearInterval(this.interval); |
|---|
| | 56 | this.interval = null; |
|---|
| | 57 | } |
|---|
| | 58 | |
|---|
| | 59 | var value = {}; |
|---|
| | 60 | |
|---|
| | 61 | if (typeof this.begin != 'object') { |
|---|
| | 62 | this.begin = [this.begin]; |
|---|
| | 63 | this.finish = [this.finish]; |
|---|
| | 64 | } |
|---|
| | 65 | for (var i in this.begin) { |
|---|
| | 66 | var b = this.begin[i]; |
|---|
| | 67 | var f = this.finish[i]; |
|---|
| | 68 | if (b == null || f == null || isNaN(b) || isNaN(f)) { |
|---|
| | 69 | OpenLayers.Console.error('invalid value for Tween'); |
|---|
| | 70 | } |
|---|
| | 71 | |
|---|
| | 72 | var c = f - b; |
|---|
| | 73 | value[i] = this.easing.apply(this, [this.time, b, c, this.duration]); |
|---|
| | 74 | } |
|---|
| | 75 | this.time++; |
|---|
| | 76 | callback.call(this, value); |
|---|
| | 77 | }, |
|---|
| | 78 | |
|---|
| | 79 | /** |
|---|
| | 80 | * Create empty functions for all easing methods. |
|---|
| | 81 | */ |
|---|
| | 82 | CLASS_NAME: "OpenLayers.Tween" |
|---|
| | 83 | }); |
|---|
| | 84 | |
|---|
| | 85 | /** |
|---|
| | 86 | * Namespace: OpenLayers.Easing |
|---|
| | 87 | * |
|---|
| | 88 | * Credits: |
|---|
| | 89 | * Easing Equations by Robert Penner, <http://www.robertpenner.com/easing/> |
|---|
| | 90 | */ |
|---|
| | 91 | OpenLayers.Easing = { |
|---|
| | 92 | /** |
|---|
| | 93 | * Create empty functions for all easing methods. |
|---|
| | 94 | */ |
|---|
| | 95 | CLASS_NAME: "OpenLayers.Easing" |
|---|
| | 96 | } |
|---|
| | 97 | |
|---|
| | 98 | /** |
|---|
| | 99 | * Namespace: OpenLayers.Easing.Linear |
|---|
| | 100 | */ |
|---|
| | 101 | OpenLayers.Easing.Linear = { |
|---|
| | 102 | |
|---|
| | 103 | /** |
|---|
| | 104 | * APIFunction: easeIn |
|---|
| | 105 | * |
|---|
| | 106 | * Parameters: |
|---|
| | 107 | * t - {Float} time |
|---|
| | 108 | * b - {Float} beginning position |
|---|
| | 109 | * c - {Float} total change |
|---|
| | 110 | * d - {Float} duration of the transition |
|---|
| | 111 | */ |
|---|
| | 112 | easeIn: function(t, b, c, d) { |
|---|
| | 113 | return c*t/d + b; |
|---|
| | 114 | }, |
|---|
| | 115 | |
|---|
| | 116 | /** |
|---|
| | 117 | * APIFunction: easeOut |
|---|
| | 118 | * |
|---|
| | 119 | * Parameters: |
|---|
| | 120 | * t - {Float} time |
|---|
| | 121 | * b - {Float} beginning position |
|---|
| | 122 | * c - {Float} total change |
|---|
| | 123 | * d - {Float} duration of the transition |
|---|
| | 124 | */ |
|---|
| | 125 | easeOut: function(t, b, c, d) { |
|---|
| | 126 | return c*t/d + b; |
|---|
| | 127 | }, |
|---|
| | 128 | |
|---|
| | 129 | /** |
|---|
| | 130 | * APIFunction: easeInOut |
|---|
| | 131 | * |
|---|
| | 132 | * Parameters: |
|---|
| | 133 | * t - {Float} time |
|---|
| | 134 | * b - {Float} beginning position |
|---|
| | 135 | * c - {Float} total change |
|---|
| | 136 | * d - {Float} duration of the transition |
|---|
| | 137 | */ |
|---|
| | 138 | easeInOut: function(t, b, c, d) { |
|---|
| | 139 | return c*t/d + b; |
|---|
| | 140 | }, |
|---|
| | 141 | |
|---|
| | 142 | CLASS_NAME: "OpenLayers.Easing.Linear" |
|---|
| | 143 | }; |
|---|
| | 144 | |
|---|
| | 145 | /** |
|---|
| | 146 | * Namespace: OpenLayers.Easing.Expo |
|---|
| | 147 | */ |
|---|
| | 148 | OpenLayers.Easing.Expo = { |
|---|
| | 149 | |
|---|
| | 150 | /** |
|---|
| | 151 | * APIFunction: easeIn |
|---|
| | 152 | * |
|---|
| | 153 | * Parameters: |
|---|
| | 154 | * t - {Float} time |
|---|
| | 155 | * b - {Float} beginning position |
|---|
| | 156 | * c - {Float} total change |
|---|
| | 157 | * d - {Float} duration of the transition |
|---|
| | 158 | */ |
|---|
| | 159 | easeIn: function(t, b, c, d) { |
|---|
| | 160 | return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b; |
|---|
| | 161 | }, |
|---|
| | 162 | |
|---|
| | 163 | /** |
|---|
| | 164 | * APIFunction: easeOut |
|---|
| | 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 | easeOut: function(t, b, c, d) { |
|---|
| | 173 | return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b; |
|---|
| | 174 | }, |
|---|
| | 175 | |
|---|
| | 176 | /** |
|---|
| | 177 | * APIFunction: easeInOut |
|---|
| | 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 | easeInOut: function(t, b, c, d) { |
|---|
| | 186 | if (t==0) return b; |
|---|
| | 187 | if (t==d) return b+c; |
|---|
| | 188 | if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b; |
|---|
| | 189 | return c/2 * (-Math.pow(2, -10 * --t) + 2) + b; |
|---|
| | 190 | }, |
|---|
| | 191 | |
|---|
| | 192 | CLASS_NAME: "OpenLayers.Easing.Expo" |
|---|
| | 193 | }; |