not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / scriptengines / webkit / dashboard / AppleClasses / AppleAnimator.js
blob93b51297f19ca32dff21d7dc662955fb570a974b
1 /*
2  * Copyright 2008 Stefan Buller <hikingpete@cain.afraid.org>
3  *
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.
11  *
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.
16  *
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/>.
19  */
21 function AppleAnimator(duration, interval, start, finish, handler)
23     //properties specified by Apple
24     this.duration = duration;
25     this.interval = interval;
26     this.animations = [];
27     this.timer = 0;
28     this.oncomplete; //no documentation exists for this.
30     //internal members:
31     this.intervalPointer = undefined;
33     var that = this;
35     if (arguments.length === 5) {
36         var animation = new AppleAnimation(start, finish, handler);
37         that.addAnimation(animation); //I hope I can use this already
38     }
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) {
57                 that.stop();
58             } else {
59                 for (var i=0; i<that.animations.length; i++) {
60                     var animation = that.animations[i];
61                     animation.step();
62                     animation.callback(animation, animation.now,
63                             animation.from, animation.to);
64                 }
65             }
66         }
68         this.intervalPointer = setInterval(intervalHandler, this.interval);
69     }
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
78     }
81 function AppleAnimation(start, finish, handler)
83     this.from = start;
84     this.to = finish;
85     this.now = undefined;
86     this.callback = handler;
88     //internal members:
89     //prototype.initialize
90     //this.step
93 AppleAnimation.prototype.initialize = function(duration, interval)
95     this.now = this.from;
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;
103     }
106 function AppleRect(left, top, right, bottom)
108     this.left = left;
109     this.top = top;
110     this.right = right;
111     this.bottom = bottom;
114 function AppleRectAnimation(start, finish, handler)
116     this.from = start;
117     this.to = finish;
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;
127     var diff = {
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
132     }
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)
140     }