|
| 1 | +/* |
| 2 | +Copyright (c) 2005-2008 Apple Inc. All Rights Reserved. |
| 3 | +
|
| 4 | +IMPORTANT: This Apple software and the associated images located in |
| 5 | +/System/Library/WidgetResources/AppleClasses/ (collectively "Apple Software") |
| 6 | +are supplied to you by Apple Inc. (“Apple”) in consideration of your |
| 7 | +agreement to the following terms. Your use, installation and/or redistribution |
| 8 | +of this Apple Software constitutes acceptance of these terms. If you do not |
| 9 | +agree with these terms, please do not use, install, or redistribute this Apple |
| 10 | +Software. |
| 11 | +
|
| 12 | +In consideration of your agreement to abide by the following terms, and subject |
| 13 | +to these terms, Apple grants you a personal, non-exclusive license, under |
| 14 | +Apple’s copyrights in the Apple Software, to use, reproduce, and redistribute |
| 15 | +the Apple Software, in text form (for JavaScript files) or binary form (for |
| 16 | +associated images), for the sole purpose of creating Dashboard widgets for Mac |
| 17 | +OS X. |
| 18 | +
|
| 19 | +If you redistribute the Apple Software, you must retain this entire notice and |
| 20 | +the warranty disclaimers and limitation of liability provisions (last two |
| 21 | +paragraphs below) in all such redistributions of the Apple Software. |
| 22 | +
|
| 23 | +You may not use the name, trademarks, service marks or logos of Apple to endorse |
| 24 | +or promote products that include the Apple Software without the prior written |
| 25 | +permission of Apple. Except as expressly stated in this notice, no other rights |
| 26 | +or licenses, express or implied, are granted by Apple herein, including but not |
| 27 | +limited to any patent rights that may be infringed by your products that |
| 28 | +incorporate the Apple Software or by other works in which the Apple Software may |
| 29 | +be incorporated. |
| 30 | +
|
| 31 | +The Apple Software is provided on an "AS IS" basis. APPLE MAKES NO WARRANTIES, |
| 32 | +EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF |
| 33 | +NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, |
| 34 | +REGARDING THE APPPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION |
| 35 | +WITH YOUR PRODUCTS. |
| 36 | +
|
| 37 | +IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR |
| 38 | +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 39 | +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 40 | +ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, AND/OR DISTRIBUTION OF THE |
| 41 | +APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT |
| 42 | +(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN |
| 43 | +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 44 | +*/ |
| 45 | + |
| 46 | +function AppleAnimator (duration, interval, optionalFrom, optionalTo, optionalCallback) |
| 47 | +{ |
| 48 | + this.startTime = 0; |
| 49 | + this.duration = duration; |
| 50 | + this.interval = interval; |
| 51 | + this.animations = new Array; |
| 52 | + this.timer = null; |
| 53 | + this.oncomplete = null; |
| 54 | + |
| 55 | + this._firstTime = true; |
| 56 | + |
| 57 | + var self = this; |
| 58 | + |
| 59 | + this.animate = function (self) { |
| 60 | + |
| 61 | + function limit_3 (a, b, c) { |
| 62 | + return a < b ? b : (a > c ? c : a); |
| 63 | + } |
| 64 | + |
| 65 | + var T, time; |
| 66 | + var ease; |
| 67 | + var time = (new Date).getTime(); |
| 68 | + var dur = self.duration; |
| 69 | + var done; |
| 70 | + |
| 71 | + T = limit_3(time - self.startTime, 0, dur); |
| 72 | + time = T / dur; |
| 73 | + ease = 0.5 - (0.5 * Math.cos(Math.PI * time)); |
| 74 | + |
| 75 | + done = T >= dur; |
| 76 | + |
| 77 | + var array = self.animations; |
| 78 | + var c = array.length; |
| 79 | + var first = self._firstTime; |
| 80 | + |
| 81 | + for (var i = 0; i < c; ++i) |
| 82 | + { |
| 83 | + array[i].doFrame (self, ease, first, done, time); |
| 84 | + } |
| 85 | + |
| 86 | + if (done) |
| 87 | + { |
| 88 | + self.stop(); |
| 89 | + if (self.oncomplete != null) |
| 90 | + { |
| 91 | + self.oncomplete(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + self._firstTime = false; |
| 96 | + } |
| 97 | + |
| 98 | + if (optionalFrom !== undefined && optionalTo !== undefined && optionalCallback !== undefined) |
| 99 | + { |
| 100 | + this.addAnimation(new AppleAnimation (optionalFrom, optionalTo, optionalCallback)); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +AppleAnimator.prototype.start = function () { |
| 105 | + if (this.timer == null) |
| 106 | + { |
| 107 | + var timeNow = (new Date).getTime(); |
| 108 | + var interval = this.interval; |
| 109 | + |
| 110 | + this.startTime = timeNow - interval; // see it back one frame |
| 111 | + |
| 112 | + this.timer = setInterval (this.animate, interval, this); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +AppleAnimator.prototype.stop = function () { |
| 117 | + if (this.timer != null) |
| 118 | + { |
| 119 | + clearInterval(this.timer); |
| 120 | + this.timer = null; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +AppleAnimator.prototype.addAnimation = function (animation) { |
| 125 | + |
| 126 | + this.animations[this.animations.length] = animation; |
| 127 | +} |
| 128 | + |
| 129 | +// |
| 130 | +// Animation class |
| 131 | +// |
| 132 | + |
| 133 | +function AppleAnimation (from, to, callback) |
| 134 | +{ |
| 135 | + this.from = from; |
| 136 | + this.to = to; |
| 137 | + this.callback = callback; |
| 138 | + |
| 139 | + this.now = from; |
| 140 | + this.ease = 0; |
| 141 | + this.time = 0; |
| 142 | +} |
| 143 | + |
| 144 | +AppleAnimation.prototype.doFrame = function (animation, ease, first, done, time) { |
| 145 | + |
| 146 | + var now; |
| 147 | + |
| 148 | + if (done) |
| 149 | + now = this.to; |
| 150 | + else |
| 151 | + now = this.from + (this.to - this.from) * ease; |
| 152 | + |
| 153 | + this.now = now; |
| 154 | + this.ease = ease; |
| 155 | + this.time = time; |
| 156 | + this.callback (animation, now, first, done); |
| 157 | +} |
| 158 | + |
| 159 | +// |
| 160 | +// RectAnimation class |
| 161 | +// |
| 162 | + |
| 163 | +function AppleRectAnimation (from, to, callback) |
| 164 | +{ |
| 165 | + this.from = from; |
| 166 | + this.to = to; |
| 167 | + this.callback = callback; |
| 168 | + |
| 169 | + this.now = from; |
| 170 | + this.ease = 0; |
| 171 | + this.time = 0; |
| 172 | +} |
| 173 | + |
| 174 | +AppleRectAnimation.prototype = new AppleAnimation (0, 0, null); |
| 175 | + |
| 176 | +AppleRectAnimation.prototype.doFrame = function (animation, ease, first, done, time) { |
| 177 | + |
| 178 | + var now; |
| 179 | + |
| 180 | + function computeNextRectangle (from, to, ease) |
| 181 | + { |
| 182 | + return addRects (from, timesRect (minusRects(to, from), ease)); |
| 183 | + } |
| 184 | + |
| 185 | + if (done) |
| 186 | + now = this.to; |
| 187 | + else |
| 188 | + now = AppleRect.add (this.from, AppleRect.multiply (AppleRect.subtract (this.to, this.from), ease)); |
| 189 | + |
| 190 | + //computeNextRectangle (this.from, this.to, ease); |
| 191 | + this.now = now; |
| 192 | + this.ease = ease; |
| 193 | + this.time = time; |
| 194 | + this.callback (animation, now, first, done); |
| 195 | +} |
| 196 | + |
| 197 | +// |
| 198 | +// AppleRect class |
| 199 | +// |
| 200 | + |
| 201 | +function AppleRect (left, top, right, bottom) { |
| 202 | + this.left = left; |
| 203 | + this.top = top; |
| 204 | + this.right = right; |
| 205 | + this.bottom = bottom; |
| 206 | +} |
| 207 | + |
| 208 | + |
| 209 | +AppleRect.add = function (a, b) { |
| 210 | + return new AppleRect (a.left + b.left, |
| 211 | + a.top + b.top, |
| 212 | + a.right + b.right, |
| 213 | + a.bottom + b.bottom); |
| 214 | +} |
| 215 | + |
| 216 | +AppleRect.subtract = function (a, b) { |
| 217 | + return new AppleRect (a.left - b.left, |
| 218 | + a.top - b.top, |
| 219 | + a.right - b.right, |
| 220 | + a.bottom - b.bottom); |
| 221 | +} |
| 222 | + |
| 223 | +AppleRect.multiply = function (a, multiplier) { |
| 224 | + return new AppleRect (a.left * multiplier, |
| 225 | + a.top * multiplier, |
| 226 | + a.right * multiplier, |
| 227 | + a.bottom * multiplier); |
| 228 | +} |
| 229 | + |
| 230 | +// generic applier, pass methods like Math.floor/round/ceil |
| 231 | +AppleRect.prototype.apply = function (func) { |
| 232 | + this.left = func(this.left); |
| 233 | + this.top = func(this.top); |
| 234 | + this.right = func(this.right); |
| 235 | + this.bottom = func(this.bottom); |
| 236 | + return this; |
| 237 | +} |
| 238 | + |
| 239 | +AppleRect.prototype.toString = function () { |
| 240 | + return "{left:" + this.left + ", top:" + this.top + ", right:" + this.right + ", bottom:" + this.bottom + "}"; |
| 241 | +} |
0 commit comments