| | 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 - {Object} begin value |
|---|
| | 30 | * finish - {Object} finish value (should have the same properties as begin) |
|---|
| | 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 | for (var i in this.begin) { |
|---|
| | 61 | var b = this.begin[i]; |
|---|
| | 62 | var f = this.finish[i]; |
|---|
| | 63 | if (b == null || f == null || isNaN(b) || isNaN(f)) { |
|---|
| | 64 | OpenLayers.Console.error('invalid value for Tween'); |
|---|
| | 65 | } |
|---|
| | 66 | |
|---|
| | 67 | var c = f - b; |
|---|
| | 68 | value[i] = this.easing.apply(this, [this.time, b, c, this.duration]); |
|---|
| | 69 | } |
|---|
| | 70 | this.time++; |
|---|
| | 71 | callback.call(this, value); |
|---|
| | 72 | }, |
|---|
| | 73 | |
|---|
| | 74 | /** |
|---|
| | 75 | * Create empty functions for all easing methods. |
|---|
| | 76 | */ |
|---|
| | 77 | CLASS_NAME: "OpenLayers.Tween" |
|---|
| | 78 | }); |
|---|
| | 79 | |
|---|
| | 80 | /** |
|---|
| | 81 | * Namespace: OpenLayers.Easing |
|---|
| | 82 | * |
|---|
| | 83 | * Credits: |
|---|
| | 84 | * Easing Equations by Robert Penner, <http://www.robertpenner.com/easing/> |
|---|
| | 85 | */ |
|---|
| | 86 | OpenLayers.Easing = { |
|---|
| | 87 | /** |
|---|
| | 88 | * Create empty functions for all easing methods. |
|---|
| | 89 | */ |
|---|
| | 90 | CLASS_NAME: "OpenLayers.Easing" |
|---|
| | 91 | } |
|---|
| | 92 | |
|---|
| | 93 | /** |
|---|
| | 94 | * Namespace: OpenLayers.Easing.Linear |
|---|
| | 95 | */ |
|---|
| | 96 | OpenLayers.Easing.Linear = { |
|---|
| | 97 | |
|---|
| | 98 | /** |
|---|
| | 99 | * APIFunction: easeIn |
|---|
| | 100 | * |
|---|
| | 101 | * Parameters: |
|---|
| | 102 | * t - {Float} time |
|---|
| | 103 | * b - {Float} beginning position |
|---|
| | 104 | * c - {Float} total change |
|---|
| | 105 | * d - {Float} duration of the transition |
|---|
| | 106 | */ |
|---|
| | 107 | easeIn: function(t, b, c, d) { |
|---|
| | 108 | return c*t/d + b; |
|---|
| | 109 | }, |
|---|
| | 110 | |
|---|
| | 111 | /** |
|---|
| | 112 | * APIFunction: easeOut |
|---|
| | 113 | * |
|---|
| | 114 | * Parameters: |
|---|
| | 115 | * t - {Float} time |
|---|
| | 116 | * b - {Float} beginning position |
|---|
| | 117 | * c - {Float} total change |
|---|
| | 118 | * d - {Float} duration of the transition |
|---|
| | 119 | */ |
|---|
| | 120 | easeOut: function(t, b, c, d) { |
|---|
| | 121 | return c*t/d + b; |
|---|
| | 122 | }, |
|---|
| | 123 | |
|---|
| | 124 | /** |
|---|
| | 125 | * APIFunction: easeInOut |
|---|
| | 126 | * |
|---|
| | 127 | * Parameters: |
|---|
| | 128 | * t - {Float} time |
|---|
| | 129 | * b - {Float} beginning position |
|---|
| | 130 | * c - {Float} total change |
|---|
| | 131 | * d - {Float} duration of the transition |
|---|
| | 132 | */ |
|---|
| | 133 | easeInOut: function(t, b, c, d) { |
|---|
| | 134 | return c*t/d + b; |
|---|
| | 135 | }, |
|---|
| | 136 | |
|---|
| | 137 | CLASS_NAME: "OpenLayers.Easing.Linear" |
|---|
| | 138 | }; |
|---|
| | 139 | |
|---|
| | 140 | /** |
|---|
| | 141 | * Namespace: OpenLayers.Easing.Expo |
|---|
| | 142 | */ |
|---|
| | 143 | OpenLayers.Easing.Expo = { |
|---|
| | 144 | |
|---|
| | 145 | /** |
|---|
| | 146 | * APIFunction: easeIn |
|---|
| | 147 | * |
|---|
| | 148 | * Parameters: |
|---|
| | 149 | * t - {Float} time |
|---|
| | 150 | * b - {Float} beginning position |
|---|
| | 151 | * c - {Float} total change |
|---|
| | 152 | * d - {Float} duration of the transition |
|---|
| | 153 | */ |
|---|
| | 154 | easeIn: function(t, b, c, d) { |
|---|
| | 155 | return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b; |
|---|
| | 156 | }, |
|---|
| | 157 | |
|---|
| | 158 | /** |
|---|
| | 159 | * APIFunction: easeOut |
|---|
| | 160 | * |
|---|
| | 161 | * Parameters: |
|---|
| | 162 | * t - {Float} time |
|---|
| | 163 | * b - {Float} beginning position |
|---|
| | 164 | * c - {Float} total change |
|---|
| | 165 | * d - {Float} duration of the transition |
|---|
| | 166 | */ |
|---|
| | 167 | easeOut: function(t, b, c, d) { |
|---|
| | 168 | return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b; |
|---|
| | 169 | }, |
|---|
| | 170 | |
|---|
| | 171 | /** |
|---|
| | 172 | * APIFunction: easeInOut |
|---|
| | 173 | * |
|---|
| | 174 | * Parameters: |
|---|
| | 175 | * t - {Float} time |
|---|
| | 176 | * b - {Float} beginning position |
|---|
| | 177 | * c - {Float} total change |
|---|
| | 178 | * d - {Float} duration of the transition |
|---|
| | 179 | */ |
|---|
| | 180 | easeInOut: function(t, b, c, d) { |
|---|
| | 181 | if (t==0) return b; |
|---|
| | 182 | if (t==d) return b+c; |
|---|
| | 183 | if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b; |
|---|
| | 184 | return c/2 * (-Math.pow(2, -10 * --t) + 2) + b; |
|---|
| | 185 | }, |
|---|
| | 186 | |
|---|
| | 187 | CLASS_NAME: "OpenLayers.Easing.Expo" |
|---|
| | 188 | }; |