2
* Copyright 2008 Stefan Buller <hikingpete@cain.afraid.org>
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2.1 of the License, or (at your option) version 3, or any
8
* later version accepted by the membership of KDE e.V. (or its
9
* successor approved by the membership of KDE e.V.), which shall
10
* act as a proxy defined in Section 6 of version 3 of the license.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
21
function AppleAnimator(duration, interval, start, finish, handler)
23
//properties specified by Apple
24
this.duration = duration;
25
this.interval = interval;
28
this.oncomplete; //no documentation exists for this.
31
this.intervalPointer = undefined;
35
if (arguments.length === 5) {
36
var animation = new AppleAnimation(start, finish, handler);
37
that.addAnimation(animation); //I hope I can use this already
41
AppleAnimator.prototype.addAnimation = function (animation) {
42
//I'm unsure what the behaviour of this function should be if
43
//AppleAnimator.start() has already been called.
44
animation.initialize(this.duration, this.interval);
45
this.animations.push(animation);
48
AppleAnimator.prototype.start = function ()
50
if (this.intervalPointer == undefined) {
52
var that = this; //for the closure (may not be necessary?)
53
function intervalHandler() {
54
//setInterval doesn't pass 'this', so we need a closure instead.
55
that.timer += that.interval;
56
if (that.timer > that.duration) {
59
for (var i=0; i<that.animations.length; i++) {
60
var animation = that.animations[i];
62
animation.callback(animation, animation.now,
63
animation.from, animation.to);
68
this.intervalPointer = setInterval(intervalHandler, this.interval);
72
AppleAnimator.prototype.stop = function ()
74
if (this.intervalPointer != undefined) {
75
clearInterval(this.intervalPointer);
76
delete this.intervalPointer;
77
//Maybe this.timer should be set to 0, and animations re-initialized
81
function AppleAnimation(start, finish, handler)
86
this.callback = handler;
89
//prototype.initialize
93
AppleAnimation.prototype.initialize = function(duration, interval)
97
var frac = interval / duration;
98
var diff = this.to - this.from; //Oh memory/speed trade-off (premature optimization?)
100
this.step = function () {
101
//This is a closure, and don't forget it.
102
this.now += diff * frac;
106
function AppleRect(left, top, right, bottom)
111
this.bottom = bottom;
114
function AppleRectAnimation(start, finish, handler)
118
this.now = undefined;
119
this.callback = handler;
122
AppleRectAnimation.prototype.initialize = function(duration, interval)
124
this.now = this.from;
126
var frac = interval / duration;
128
left: this.to.left - this.from.left,
129
top: this.to.top - this.from.top,
130
right: this.to.right - this.from.right,
131
bottom: this.to.bottom - this.from.bottom
134
this.step = function () {
135
//This is a closure, and don't forget it.
136
this.now = new AppleRect(this.now.left + frac * diff.left,
137
this.now.top + frac * diff.top,
138
this.now.right + frac * diff.right,
139
this.now.bottom + frac * diff.right)