daily pastebin goal
66%
SHARE
TWEET

Anim.js

a guest Dec 9th, 2016 30 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var AnimMode =
  2. {
  3.     FORWARD:  1,
  4.     BACKWARD: -1,
  5.     PINGPONG: 0
  6. };
  7.  
  8. function Anim(img)
  9. {
  10.     // Public stuff. Customise for each instance.
  11.     this.spr = new Sprite(img);
  12.     this.frame =
  13.     {
  14.         // Size of a frame as a factor of the whole image.
  15.         size: createVector(1, 1),
  16.         // You'll have to specify frame count automatically,
  17.         // since the whole image might not be used if there
  18.         // are multiple rows and columns.
  19.         count: 1,
  20.         // Probably do not touch this.
  21.         curr: 0,
  22.         // Probably do not touch this.
  23.         prev: 0,
  24.         // If you want to reuse the same frame multiple times,
  25.         // set this to an array, like [0, 1, 0, 2] for example.
  26.         order: null
  27.     };
  28.     this.speed   = 1;
  29.     this.playing = true;
  30.     this.repeat  = true;
  31.     this.mode    = AnimMode.FORWARD;
  32.    
  33.     // Internal variables. Don't touch.
  34.     this.timer = 0;
  35.     this.backward = false;
  36.    
  37.     this.draw = function(dt)
  38.     {
  39.         // Frames per column and row.
  40.         var countPer = createVector
  41.         (
  42.             Math.round(1.0 / this.frame.size.x),
  43.             Math.round(1.0 / this.frame.size.y)
  44.         );
  45.  
  46.         // Actual count.
  47.         var count;
  48.         if (this.frame.order === null)
  49.             count = Math.min(countPer.x * countPer.y, this.frame.count);
  50.         else
  51.             count = this.frame.order.length;
  52.        
  53.         // Tick away.
  54.         if (this.playing)
  55.             this.timer += dt;
  56.  
  57.         var time = this.timer * this.speed;
  58.        
  59.         this.frame.prev = this.frame.curr;
  60.  
  61.         // Current frame index.
  62.         var f = Math.floor(Math.max(0, Math.floor(time % count)));
  63.  
  64.         if (this.mode == AnimMode.BACKWARD || this.backward)
  65.             f = count - 1 - f;
  66.  
  67.         if (this.frame.order !== null)
  68.             f = this.frame.order[f];
  69.  
  70.         this.frame.curr = f;
  71.  
  72.         // Check if the end has been reached in case of pingpong.
  73.         if (this.mode == AnimMode.PINGPONG && f != this.frame.prev &&
  74.             ((this.backward && this.frame.prev == 0) || (!this.backward && this.frame.prev == (count - 1))))
  75.         {
  76.             // Go to the new desired frame.
  77.             f = this.frame.curr = (this.backward) ? 1 : (count - 2);
  78.            
  79.             // Update this so that the above check does not hold true again.
  80.             this.frame.prev = (this.backward) ? (count - 1) : 0;
  81.            
  82.             // Reverse.
  83.             this.backward = !this.backward;
  84.            
  85.             // Reset timer to match up with the new frame.
  86.             this.timer = (count - 2) / this.speed;
  87.         }
  88.  
  89.         // Index to position.
  90.         this.spr.clp.pos = createVector
  91.         (
  92.             (f % countPer.x) / countPer.x,
  93.             Math.floor(f / countPer.x) / countPer.y
  94.         );
  95.         this.spr.clp.size = this.frame.size;
  96.        
  97.         this.spr.draw();
  98.     }
  99.    
  100.     this.reset = function()
  101.     {
  102.         this.frame.curr = this.frame.prev = 0;
  103.         this.timer = 0;
  104.         this.backward = false;
  105.     };
  106. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top