JAVASCRIPT   7

Velocity.js

Guest on 18th May 2021 04:44:01 AM

  1. ///
  2. //  Velocity v 1.0
  3. //  Russell Lowke, January 31st
  4. //
  5. //  Copyright (c) Lowke Media
  6. //
  7. //  Permission is hereby granted, free of charge, to any person obtaining a
  8. //  copy of this software and associated documentation files (the "Software"),
  9. //  to deal in the Software without restriction, including without limitation
  10. //  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. //  and/or sell copies of the Software, and to permit persons to whom the
  12. //  Software is furnished to do so, subject to the following conditions:
  13. //
  14. //  The above copyright notice and this permission notice shall be included in
  15. //  all copies or substantial portions of the Software.
  16. //
  17. //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. //  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  23. //  IN THE SOFTWARE.
  24. //
  25. //
  26.  
  27. function Velocity(varName,                      // (string) name of variable on target being modified
  28.                           vel,                              // (number) rate of change (delta/velocity)
  29.                           min,                              // (number) minimum value of variable
  30.                           max,                              // (number) maximum value of variable
  31.                           type)                       // (string) type of effect, e.g. BaseFx.PERSIST
  32. {
  33.     this.varName = varName;             // (string) name of variable on target being modified
  34.     this.vel = vel || 0;                  // (number) rate of change (delta/velocity)
  35.     this.min = min || 0;                  // (number) minimum value of variable
  36.     this.max = max || 0;                  // (number) maximum value of variable
  37.    
  38.     if (type) {                      // override type should be PERSIST, WRAP, REVERSE, END, or REMOVE
  39.         this.type = type;
  40.     }
  41.    
  42.     this.update = function()
  43.     {
  44.         // add velocity
  45.         this.target[this.varName] += this.vel*this.timePassed;
  46.        
  47.         // if outside bounds return true
  48.         if (this.target[this.varName] > this.max ||
  49.             this.target[this.varName] < this.min) {
  50.             return true;
  51.         }
  52.        
  53.         return false;
  54.     }
  55.    
  56.     this.wrap = function()
  57.     {
  58.         if (this.target[this.varName] > this.max) {
  59.             this.target[this.varName] = this.min;
  60.         } else if (this.target[this.varName] < this.min) {
  61.             this.target[this.varName] = this.max;
  62.         }
  63.     }
  64.    
  65.     this.reverse = function()
  66.     {
  67.         if (this.target[this.varName] > this.max) {
  68.             this.target[this.varName] = this.max;
  69.         } else if (this.target[this.varName] < this.min) {
  70.             this.target[this.varName] = this.min;
  71.         }
  72.        
  73.         this.vel = -this.vel;
  74.     }
  75. }
  76.  
  77. Velocity.prototype = new BaseFx("Velocity", BaseFx.END);

Raw Paste


Login or Register to edit or fork this paste. It's free.