JAVASCRIPT   7

TurnFrames.js

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

  1. ///
  2. //  TurnFrames 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 TurnFrames(angularVelocity,                  // (number) angulat velocity through which to turn through frames
  28.                               startAngle,                         // (number) starting angle
  29.                               targetAngle,                        // (number) angle being animated to
  30.                               type)                                     // (string) type of effect, e.g. BaseFx.PERSIST
  31. {
  32.     this.clipSize;                              // (uint) total number of images in clip for 360 degree rotation
  33.     this.frame = 0;                             // (number) float value of frame index
  34.     this.angle = startAngle || 0;               // (number) current angle in degrees, default 0
  35.     this.targetAngle = targetAngle || 0;        // (number) target angle in degrees
  36.     this.angularVelocity = angularVelocity;     // (number) angular velocity in degress per millisecond
  37.     this.imageClip = 0;                         // (ImageClip) this effect must target an image clip
  38.    
  39.     if (type) {                      // override type, should be PERSIST, WRAP, REVERSE, END, or REMOVE
  40.         this.type = type;
  41.     }
  42.    
  43.     this.activate = function()
  44.     {
  45.         // target must be an ImageClip
  46.         if (this.target instanceof ImageClip) {
  47.             this.imageClip = this.target;
  48.            
  49.             // ImageClip's mspf must be 0
  50.             this.imageClip.fps = 0;
  51.             this.imageClip.mspf = 0;
  52.            
  53.         } else {
  54.             throw new Error("TurnFrames must target an ImageClip.");
  55.         }
  56.        
  57.         this.clipSize = this.imageClip.imageList.nImages;
  58.         this.frame = this.imageClip.frameIndex;
  59.     }
  60.    
  61.    
  62.     this.update = function()
  63.     {
  64.         var direction = TurnFrames.incOrDec(this.angle, this.targetAngle, 360);  // +1 or -1
  65.         this.angle = TurnFrames.cleanAngle(this.angle + this.angularVelocity*this.timePassed*direction);
  66.        
  67.         // check if overshot target angle IS THERE EASIER WAY?
  68.         if (TurnFrames.incOrDec(this.angle, this.targetAngle, 360) === 0 - direction) {
  69.             this.angle = this.targetAngle;
  70.         }
  71.        
  72.         // set the frame for the ImageClip
  73.         this.frame = TurnFrames.angleToFrame(this.angle, this.clipSize);
  74.         this.imageClip.frame = Math.round(this.frame);     // WHAT IF Math.floor() TO BE CONSISTENT?
  75.        
  76.         // print("this.angle:" + this.angle + " this.imageClip.frame:" + this.imageClip.frame);
  77.         if (this.angle === this.targetAngle) {
  78.             return true;
  79.         } else {
  80.             return false;
  81.         }
  82.     }
  83.    
  84.     // convert an angle to a "clean" angle between 0 and 360
  85.     TurnFrames.cleanAngle = function(angle)
  86.     {
  87.         angle %= 360;
  88.         if (angle < 0) {
  89.             return 360 + angle;
  90.         }
  91.         return angle;
  92.     }
  93.    
  94.     //
  95.     // convert angle (degrees) to an ImageClip frame number where
  96.     //  the first frame is 0 degrees and the last 360 degrees
  97.     TurnFrames.angleToFrame = function(angle, totalFrames)
  98.     {
  99.         // find angle size of each segment
  100.         var segment = 360.0/totalFrames; 
  101.        
  102.         // augment the angle by half a segment      // NEEDED? WHAT IF Math.floor() USED ABOVE?
  103.         angle += segment/2;
  104.        
  105.         // loop at 360 back to 0 degrees    USE MODULO ?
  106.         if (angle >= 360) { angle -= 360; }
  107.        
  108.         // return the frame
  109.         return Math.floor(angle/segment);
  110.     }
  111.    
  112.     //
  113.     // given a looping sequence in both directions with a range of max,
  114.     // if at n which direction (+ or -) is quickest to get to indDest
  115.     TurnFrames.incOrDec = function(n, dest, max)
  116.     {
  117.         // ensure integers
  118.        
  119.         var inc = 0;
  120.         var dec = 0;
  121.        
  122.         if (n === dest) {
  123.             return 0;
  124.         } else if (n < dest) {
  125.             inc = dest - n;            
  126.             dec = n + (max - dest);
  127.         } else if (n > dest) {
  128.             inc = dest + (max - n);
  129.             dec = n - dest;
  130.         }
  131.        
  132.         if (inc > dec) {
  133.             return -1;
  134.         } else {
  135.             return +1;
  136.         }
  137.     }
  138. }
  139.  
  140. TurnFrames.prototype = new BaseFx("TurnFrames", BaseFx.END);
  141.  
  142.  
  143. /*
  144.             public static function turnInDuration(duration:uint,
  145.                                                                     startAngle:Number,
  146.                                                                     targetAngle:Number):AngleFramesFX
  147.             {    
  148.                   var angle:Number = cleanAngle(targetAngle - startAngle);
  149.                   if (angle > 180) {
  150.                         angle = 360 - angle;
  151.                   }
  152.                   var angularVelocity:Number = angle/duration;
  153.                   return new AngleFramesFX(angularVelocity, startAngle, targetAngle);
  154.             }
  155.  */

Raw Paste


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