home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 March / PCpro_2006_03.ISO / files / freeware / greasemonkey-0.6.4-fx.xpi / chrome / chromeFiles / content / accelimation.js next >
Encoding:
JavaScript  |  2005-11-30  |  4.5 KB  |  129 lines

  1. //=====================================================================
  2. // Accel[erated] [an]imation object
  3. // change a property of an object over time in an accelerated fashion
  4. //=====================================================================
  5. // obj  : reference to the object whose property you'd like to animate
  6. // prop : property you would like to change eg: "left"
  7. // to   : final value of prop
  8. // time : time the animation should take to run
  9. // zip    : optional. specify the zippiness of the acceleration. pick a 
  10. //          number between -1 and 1 where -1 is full decelerated, 1 is 
  11. //          full accelerated, and 0 is linear (no acceleration). default
  12. //          is 0.
  13. // unit    : optional. specify the units for use with prop. default is 
  14. //          "px".
  15. //=====================================================================
  16.  
  17. function Accelimation(obj, prop, to, time, zip, unit) {
  18.     if (typeof zip  == "undefined") zip  = 0;
  19.     if (typeof unit == "undefined") unit = "px";
  20.  
  21.     if (zip > 2 || zip <= 0)
  22.         throw new Error("Illegal value for zip. Must be less than or equal to 2 and greater than 0.");
  23.  
  24.     this.obj    = obj;
  25.     this.prop    = prop;
  26.     this.x1        = to;
  27.     this.dt        = time;
  28.     this.zip    = zip;
  29.     this.unit    = unit;
  30.  
  31.     this.x0        = parseInt(this.obj[this.prop]);
  32.     this.D        = this.x1 - this.x0;
  33.     this.A        = this.D / Math.abs(Math.pow(time, this.zip));
  34.     this.id        = Accelimation.instances.length;
  35.     this.onend    = null;
  36. }
  37.  
  38.  
  39.  
  40. //=====================================================================
  41. // public methods
  42. //=====================================================================
  43.  
  44. // after you create an accelimation, you call this to start it-a runnin'
  45. Accelimation.prototype.start = function() {
  46.     this.t0 = new Date().getTime();
  47.     this.t1 = this.t0 + this.dt;
  48.     var dx    = this.x1 - this.x0;
  49.     Accelimation._add(this);
  50. }
  51.  
  52. // and if you need to stop it early for some reason...
  53. Accelimation.prototype.stop = function() {
  54.     Accelimation._remove(this);
  55. }
  56.  
  57.  
  58.  
  59. //=====================================================================
  60. // private methods
  61. //=====================================================================
  62.  
  63. // paints one frame. gets called by Accelimation._paintAll.
  64. Accelimation.prototype._paint = function(time) {
  65.     if (time < this.t1) {
  66.         var elapsed = time - this.t0;
  67.         this.obj[this.prop] = Math.abs(Math.pow(elapsed, this.zip)) * this.A + this.x0 + this.unit;
  68.     }
  69.     else this._end();
  70. }
  71.  
  72. // ends the animation
  73. Accelimation.prototype._end = function() {
  74.     Accelimation._remove(this);
  75.     this.obj[this.prop] = this.x1 + this.unit;
  76.     this.onend();
  77. }
  78.  
  79.  
  80.  
  81.  
  82. //=====================================================================
  83. // static methods (all private)
  84. //=====================================================================
  85.  
  86. // add a function to the list of ones to call periodically
  87. Accelimation._add = function(o) {
  88.     var index = this.instances.length;
  89.     this.instances[index] = o;
  90.     // if this is the first one, start the engine
  91.     if (this.instances.length == 1) {
  92.         this.timerID = window.setInterval("Accelimation._paintAll()", this.targetRes);
  93.     }
  94. }
  95.  
  96. // remove a function from the list
  97. Accelimation._remove = function(o) {
  98.     for (var i = 0; i < this.instances.length; i++) {
  99.         if (o == this.instances[i]) {
  100.             this.instances = this.instances.slice(0,i).concat( this.instances.slice(i+1) );
  101.             break;
  102.         }
  103.     }
  104.     // if that was the last one, stop the engine
  105.     if (this.instances.length == 0) {
  106.         window.clearInterval(this.timerID);
  107.         this.timerID = null;
  108.     }
  109. }
  110.  
  111. // "engine" - call each function in the list every so often
  112. Accelimation._paintAll = function() {
  113.     var now = new Date().getTime();
  114.     for (var i = 0; i < this.instances.length; i++) {
  115.         // small chance that this accelimation could get dropped in the queue in the middle
  116.         // of a run. That means that it could have a t0 greater than "now", which means that
  117.         // elapsed would be negative.
  118.         this.instances[i]._paint(Math.max(now, this.instances[i].t0));
  119.     }
  120. }
  121.  
  122.  
  123. //=====================================================================
  124. // static properties
  125. //=====================================================================
  126.  
  127. Accelimation.instances = [];
  128. Accelimation.targetRes = 10;
  129. Accelimation.timerID = null;