TEXT   10

ImageClip.js

Guest on 18th May 2021 04:34:07 AM

  1. ///
  2. //  ImageClip v 1.0
  3. //  Russell Lowke, Febrauary 28th
  4. //
  5. //  Copyright (c)  Lowke Media
  6. //  see http://www.lowkemedia.com for more information
  7. //
  8. //  Permission is hereby granted, free of charge, to any person obtaining a
  9. //  copy of this software and associated documentation files (the "Software"),
  10. //  to deal in the Software without restriction, including without limitation
  11. //  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. //  and/or sell copies of the Software, and to permit persons to whom the
  13. //  Software is furnished to do so, subject to the following conditions:
  14. //
  15. //  The above copyright notice and this permission notice shall be included in
  16. //  all copies or substantial portions of the Software.
  17. //
  18. //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. //  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  24. //  IN THE SOFTWARE.
  25. //
  26. //
  27.  
  28.  
  29. ///
  30. // convert frames per second (FPS) to miliseconds per frame (MSPF)
  31. //
  32. ImageClip.fpsToMSPF = function(fps)
  33. {
  34.     if (fps === 0 || isNaN(fps)) {
  35.         return 0;   // avoid divide by zero
  36.     } else {
  37.         return (1000/fps);
  38.     }
  39. }
  40.    
  41.    
  42. function ImageClip(images,                          // (array) array of images for this clip
  43.                    fps,                             // (number) frames per second image list to be played at
  44.                    playEveryFrame)                  // (boolean) if true no frames are skipped
  45. {
  46.     // public vars
  47.     this.images = images;                           // (array) array of images for this clip
  48.     this.frame = 0;                                 // (number) value of frame number (index)
  49.     this.frameIndex = 0;                            // (uint) image index now displayed
  50.     this.updateTime;                                // (uint) time of update
  51.     this.timePassed;                                // (uint) time since last update
  52.     this.playEveryFrame = playEveryFrame || false;  // (boolean) if true no frames are skipped
  53.     if (this.images) {
  54.         this.image = this.images[0];                // (image) set image to first image in images
  55.     }
  56.    
  57.     // private vars
  58.     var _fps = fps;                                 // (number) frames per second image list to be played at
  59.     var _mspf = ImageClip.fpsToMSPF(_fps);          // (number) miliseconds per frame
  60.    
  61.    
  62.     ///
  63.     // update this ImageClip into canvas context
  64.     //
  65.     this.draw = function(ctx)                       // (context) canvas context being drawn into
  66.     {    
  67.         var updateTime = new Date().getTime();
  68.        
  69.         // take care of 1st draw
  70.         if (this.updateTime === undefined) {
  71.             this.updateTime = updateTime;
  72.         }
  73.        
  74.         // calculate time passed
  75.         this.timePassed = updateTime - this.updateTime;
  76.        
  77.         // record time of update
  78.         this.updateTime = updateTime;
  79.        
  80.         // handle mspf
  81.         if (_mspf) {
  82.        
  83.             // account for change
  84.             var change = this.timePassed/_mspf;
  85.             if (this.playEveryFrame && Math.abs(change) > 1) {
  86.                 if (change > 0) {
  87.                     change = +1;
  88.                 } else {
  89.                     change = -1;
  90.                 }
  91.             }
  92.             this.frame += change;
  93.            
  94.             // keep within bounds
  95.             this.frame %= this.images.length;
  96.             if (this.frame < 0) {
  97.                 this.frame += this.images.length;
  98.             }
  99.         }
  100.        
  101.         // find current frame
  102.         if (this.images) {
  103.             this.frameIndex = Math.floor(this.frame);
  104.             this.image = this.images[this.frameIndex];
  105.         }
  106.        
  107.         ImageClip.prototype.draw.call(this, ctx);
  108.     }
  109.    
  110.     //
  111.     // getters and setters
  112.    
  113.     this.setFPS = function(fps) {
  114.         _fps = fps;
  115.         _mspf = ImageClip.fpsToMSPF(_fps);
  116.     }
  117.  
  118.     this.getFPS = function() {
  119.         return _fps;
  120.     }
  121.  
  122. }
  123.  
  124. ImageClip.prototype = new Bitmap();                 // ImageClip class extends Bitmap

Raw Paste


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