API Docs for: 0.3.0
Show:

Graphic.Sprite Class

The Sprite class provide methods to draw sprites on a context. the aim of the sprites object is to be added to a Layer or to be use directly with a graphical context by invoking the draw method of the Sprite.

If many sprites with same image are added to te scene, they should use only one (shared) Image instance.

A good way to reduce number of files, and so loading time, is to put many sprite images in one file. The imageRect property is a good way to display only a part of the image.

 var mySprite = new TW.Graphic.Sprite({
     image: myImage
 });
 mySprite.draw(canvasContext);

Constructor

Graphic.Sprite

(
  • [params]
)

Parameters:

  • [params] Object optional

    params is given to GraphicObject constructor.

    • [image] Image optional

      image source displayed

    • [imageRect] Object optional
      • [x] Number optional
      • [y] Number optional
      • [width] Number optional
      • [height] Number optional

Methods

draw

(
  • context
)

Inherited from Graphic.GraphicObject but overwritten in modules/Graphic/Sprite.js:73

This method allow you to draw the sprite on a context.

Parameters:

  • context Object

    this parameter must be a valid canvas context, otherwise the behavior of the draw method is unspecified.

notifyParentChange

() protected

This method notify the parent that a change has been done, and that it should clear his cache.

rotate

(
  • angle
)
chainable

This method allow you to rotate the Graphical object around the center point of the GraphicalObject.

Parameters:

  • angle Number

    represent the angle of rotation, it's expressed in degree.

scale

(
  • x
  • y
)
chainable

this method allow you to do a scale on the GraphicObject.

Parameters:

  • x Number

    this is the x scale factor

  • y Number

    this is the y scale factor

setAttr

(
  • attrs
)
chainable

Setter availlable for updating attibuts and correctly clear the caches. You can set all attributes supported by this instance (see the GraphicObject constructor for common available properties)

Parameters:

  • attrs Object

    GraphicObject attributs. See the constructor for more details.

Example:

 object.setAttr({
     width: 20,
     height: 20
 });

 object.setAttr({
     pos: {
         x: 0,
         y: 0
     }
 });

skew

(
  • a
  • b
)
chainable

This method allow you to do a skew transform on the GraphicObject.

Parameters:

  • a Number

    the factor of skew on the y axis

  • b Number

    the factor of skew on the x axis

translate

(
  • x
  • y
)
chainable

This method allow you to translate the GraphicalObject, Internally this method modify the GraphicObject's matrix.

Parameters:

  • x Number

    this is the translation scalar of the x axis.

  • y Number

    this is the translation scalar of the y axis.

Properties

alpha

Number

value of opacity, between 0.0 (fully transparent) and 1.0 (opaque)

centerPoint

Object

default center of all matrix transformations.

values are exprimed depending on the size of the object. 0;0 is the up-left corner.

Sub-properties:

  • x Object
  • y Object

image

Image

image to display.

Note: this property should be modified only with setAttr.

imageRect

Object

rectangle source from image to display.

If you specify it you can used just a subImage of the current image to use. It is useful for the spritesheets for example where you only want to draw a specific area of the image.

Note: this property should be modified only with setAttr.

Sub-properties:

  • x Number
  • y Number
  • width Number
  • height Number

matrix

Matrix

matrix applied to this object before draw.

parent

Layer

parent containing this object.

x

Number

position on X axis

y

Number

position on Y axis

zIndex

Number

zIndex, used to determine the drawing order. Hight zIndex are drawn first.