JAVASCRIPT   5

Sprite.js

Guest on 18th May 2021 04:33:05 AM

  1. ///
  2. //  Sprite v 1.0
  3. //  Russell Lowke, Febrauary 26th
  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. function Sprite()
  29. {  
  30.     this.visible = true;                            // (boolean) if false sprite is not drawn on update()
  31.     this.parent;                                    // (Sprite) parent object for this Sprite
  32.     this.x = 0;                                     // (number) x location
  33.     this.y = 0;                                     // (number) y location
  34.     this.width = 0;                                 // (number) width of Sprite, usually set by extending class
  35.     this.height = 0;                                // (number) height of Sprite, usually set by extending class
  36.     this.regX = 0;                                  // (number) registration x location, around which Sprite scales and rotates
  37.     this.regY = 0;                                  // (number) registration y location, around which Sprite scales and rotates
  38.     this.alpha = 1;                                 // (number) alpha transparency, 1 = opaque, 0 = transparent
  39.     this.rotation = 0;                              // (number) in degrees, 0 to 360
  40.     this.scaleX = 1;                                // (number) scale in x, 1 = 100%
  41.     this.scaleY = 1;                                // (number) scale in y, 1 = 100%
  42.     this.children;                                  // (Array) display list of children Sprites attached to this Sprite
  43.    
  44.     this.update = function(ctx)                     // (context) canvas context being drawn into
  45.     {
  46.         // if Sprite not visible exit the update
  47.         if (! this.visible) {
  48.             return;
  49.         }
  50.        
  51.         ctx.save();
  52.        
  53.         // translate x and y
  54.         ctx.translate(this.x, this.y);
  55.            
  56.         // handle alpha
  57.         if (this.alpha !== 1) {
  58.             ctx.globalAlpha = this.alpha;
  59.         }
  60.            
  61.         // handle scale
  62.         if (this.scaleX !== 1 || this.scaleY !== 1) {
  63.             ctx.scale(this.scaleX, this.scaleY);
  64.         }
  65.            
  66.         // handle rotation
  67.         if (this.rotation) {
  68.             ctx.rotate(Sprite.degreesToRadians(this.rotation));     // KEEP this.rotation in radians ??
  69.         }
  70.        
  71.         // draw sprite content
  72.         this.draw(ctx);
  73.        
  74.         // update Sprite children on the display list
  75.         if (this.children) {
  76.             for (var i = 0; i < this.children.length; ++i) {
  77.                 this.children[i].update(ctx);
  78.             }
  79.         }
  80.        
  81.         ctx.restore();
  82.     };
  83.    
  84.     ///
  85.     // draw function for override by extending classes
  86.     //
  87.     this.draw = function(ctx)               // (context) canvas context being drawn into
  88.     {
  89.         // Sprite is extended for draw function
  90.         //   A Sprite alone is a container and has nothing to draw
  91.     }
  92.    
  93.     ///
  94.     // Adds a child to the top of the display list.
  95.     // Returns the child that was added.
  96.     //
  97.     this.addChild = function(child)         // (Sprite) sprite to be added to displayList
  98.     {
  99.         if (! this.children) {
  100.             this.children = new Array();
  101.         }
  102.         if (child.parent) {
  103.             child.parent.removeChild(child);
  104.         }
  105.         child.parent = this;
  106.         this.children.push(child);
  107.         return child;
  108.     }  
  109.    
  110.     ///
  111.     // Adds a child to the display list at the specified index, bumping children at equal or greater indexes up one.
  112.     // The index must be between 0 and numChildren.
  113.     // Returns the child that was added.
  114.     //
  115.     this.addChildAt = function(child,       // (Sprite) child to be added to displayList
  116.                                index)       // (uint) index to add the child at
  117.     {
  118.         if (child.parent) {
  119.             child.parent.removeChild(child);
  120.         }
  121.         child.parent = this;
  122.         this.children.splice(index, 0, child);
  123.         return child;
  124.     }
  125.    
  126.    
  127.     ///
  128.     // Removes the specified child from the display list.
  129.     // Note that it is faster to use removeChildAt() if the index is already known.
  130.     // Returns true if the child was removed, or false if it was not in the display list.
  131.     //
  132.     this.removeChild = function(child)      // (Sprite) child to be removed from displayList
  133.     {
  134.         return this.removeChildAt(this.children.indexOf(child));
  135.     }
  136.    
  137.     ///
  138.     // Removes the child at the specified index from the display list, and sets its parent to null.
  139.     // Returns true if the child was removed, or false if the index was out of range.
  140.     //
  141.     this.removeChildAt = function(index)    // (uint) index of child to remove
  142.     {
  143.         if (index < 0 || index > this.children.length - 1) {
  144.             return false;
  145.         }
  146.         var child = this.children[index];
  147.         child.parent = undefined;
  148.         this.children.splice(index, 1);
  149.         return true;
  150.     }
  151.    
  152.     ///
  153.     // Removes all children from the display list.
  154.     //
  155.     this.removeAllChildren = function()
  156.     {
  157.         while (this.children.length) {
  158.             this.removeChildAt(0);
  159.         }
  160.     }
  161.    
  162.     ///
  163.     // convenience function for setting regX and regY
  164.     //
  165.     this.setReg = function(align) {         // (string) alignment setting, typically Sprite.CENTER
  166.        
  167.         switch (align) {
  168.             case Sprite.TOP_LEFT:       this.regX = 0;              this.regY = 0;              return;
  169.             case Sprite.TOP_CENTER:     this.regX = this.width/2;   this.regY = 0;              return;
  170.             case Sprite.TOP_RIGHT:      this.regX = this.width;     this.regY = 0;              return;
  171.             case Sprite.MID_LEFT:       this.regX = 0;              this.regY = this.height/2;  return;
  172.             case Sprite.CENTER:         this.regX = this.width/2;   this.regY = this.height/2;  return;
  173.             case Sprite.MID_RIGHT:      this.regX = this.width;     this.regY = this.height/2;  return;
  174.             case Sprite.BOTTOM_LEFT:    this.regX = 0;              this.regY = this.height;    return;
  175.             case Sprite.BOTTOM_CENTER:  this.regX = this.width/2;   this.regY = this.height;    return;
  176.             case Sprite.BOTTOM_RIGHT:   this.regX = this.width;     this.regY = this.height;    return;
  177.         }
  178.        
  179.         throw new Error("setReg() can't understand align parameter of \"" + align + "\"");
  180.     }
  181.    
  182.     ///
  183.     // convenience function that sets both scaleX and scaleY
  184.     //
  185.     this.setScale = function(scale) {       // (number) percentage scale to draw
  186.         this.scaleX = scale;
  187.         this.scaleY = scale;
  188.     }
  189. }
  190.  
  191. //
  192. // static helper functions
  193.  
  194.  
  195. ///
  196. // Convert radians to degrees.
  197. // degrees = radians*180/PI
  198. //
  199. Sprite.radiansToDegrees = function(degrees) {
  200.     // precalculate 180/PI = 57.295779513082289
  201.     return degrees*57.295779513082289;
  202. }
  203.    
  204. ///
  205. // Convert degrees to radians.
  206. // radians = degrees*PI/180
  207. //
  208. Sprite.degreesToRadians = function(degrees) {
  209.     // precalculate PI/180 = 0.01745329251994
  210.     return degrees*0.01745329251994;
  211. }
  212.    
  213.    
  214. // align constants for setReg() method
  215. Sprite.TOP_LEFT         = "top left";
  216. Sprite.TOP_CENTER       = "top center";
  217. Sprite.TOP_RIGHT        = "top right";
  218. Sprite.MID_LEFT         = "mid left";
  219. Sprite.CENTER           = "center";
  220. Sprite.MID_RIGHT        = "mid right";
  221. Sprite.BOTTOM_LEFT      = "bottom left";
  222. Sprite.BOTTOM_CENTER    = "bottom center";
  223. Sprite.BOTTOM_RIGHT     = "bottom right";
  224.  
  225.    
  226.     /*
  227.    
  228.     // ADD this.shadow parameter?
  229.     //
  230.  
  231.     /*
  232.     p.applyShadow = function(ctx, shadow) {
  233.         ctx.shadowColor = shadow.color;
  234.         ctx.shadowOffsetX = shadow.offsetX;
  235.         ctx.shadowOffsetY = shadow.offsetY;
  236.         ctx.shadowBlur = shadow.blur;
  237.     }
  238.      */
  239.      /*
  240.  
  241.     p.toString = function() {
  242.         return "[Container (name="+  this.name +")]";
  243.     }
  244.    
  245.     p.toString = function() {
  246.         return "[DisplayObject (name="+  this.name +")]";
  247.     }
  248.     */

Raw Paste


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