Shaku

Shaku JS

Back To Table of Content

Sprite

Sprite

Sprite class.

Kind: global class

new Sprite(texture, [sourceRectangle])

Create the sprite object.

Param Type Description
texture TextureAssetBase Sprite texture.
[sourceRectangle] Rectangle Optional source rectangle.

sprite.texture : TextureAssetBase

Sprite’s texture.

Kind: instance property of Sprite

sprite.position : Vector2 | Vector3

Sprite position. If Vector3 is provided, the z value will be passed to vertices position in shader code.

Kind: instance property of Sprite

sprite.size : Vector2 | Vector3

Sprite size. If Vector3 is provided, the z value will be passed to the bottom vertices position in shader code, as position.z + size.z.

Kind: instance property of Sprite

sprite.sourceRectangle : Rectangle

Sprite source rectangle in texture. Null will take entire texture.

Kind: instance property of Sprite

sprite.rotation : Number

Sprite rotation in radians.

Kind: instance property of Sprite

sprite.origin : Vector2

Sprite origin point.

Kind: instance property of Sprite

sprite.skew : Vector2

Skew the sprite corners on X and Y axis, around the origin point.

Kind: instance property of Sprite

sprite.color : Color | Array.<Color>

Sprite color. If array is set, will assign each color to different vertex, starting from top-left.

Kind: instance property of Sprite

sprite.flipX ⇒ Boolean

Check if this sprite is flipped around X axis. This is just a sugarcoat that returns if size.x < 0.

Kind: instance property of Sprite
Returns: Boolean - If sprite is flipped on X axis.

sprite.flipX

Flip sprite around X axis. This is just a sugarcoat that set size.x to negative or positive value, without changing its scale.

Kind: instance property of Sprite

Param Type Description
flip Boolean Should we flip the sprite around X axis. If undefined, will take the negative of flipX current value, ie will toggle flipping.

sprite.flipY ⇒ Boolean

Check if this sprite is flipped around y axis. This is just a sugarcoat that returns if size.y < 0.

Kind: instance property of Sprite
Returns: Boolean - If sprite is flipped on Y axis.

sprite.flipY

Flip sprite around Y axis. This is just a sugarcoat that set size.y to negative or positive value, without changing its scale.

Kind: instance property of Sprite

Param Type Description
flip Boolean Should we flip the sprite around Y axis. If undefined, will take the negative of flipY current value, ie will toggle flipping.

sprite.setToSourceRectangleSize() ⇒ Sprite

Set size to source rectangle size.

Kind: instance method of Sprite
Returns: Sprite - this.

sprite.setToTextureSize() ⇒ Sprite

Set size to texture size.

Kind: instance method of Sprite
Returns: Sprite - this.

sprite.setSourceFromSpritesheet(texture, index, spritesCount, [margin], [setSize])

Set the source Rectangle automatically from spritesheet. This method get sprite index in sheet and how many sprites there are in total, and calculate the desired offset and size in source Rectangle based on it + source image size.

Kind: instance method of Sprite

Param Type Description
texture TextureAssetBase Texture to set source rectangle from.
index Vector2 Sprite index in spritesheet.
spritesCount Vector2 How many sprites there are in spritesheet in total.
[margin] Number How many pixels to trim from the tile (default is 0).
[setSize] Boolean If true will also set width and height based on source rectangle (default is true).

sprite.clone() ⇒ Sprite

Clone this sprite.

Kind: instance method of Sprite
Returns: Sprite - cloned sprite.

Sprite.build(texture, position, size, sourceRectangle, color, [rotation], [origin], [skew]) ⇒ Sprite

Build a sprite from params.

Kind: static method of Sprite
Returns: Sprite - New sprite instance.

Param Type Description
texture TextureAssetBase Sprite texture.
position Vector2 | Vector3 Drawing position (at origin). If vector3 is provided, will pass z value to the shader code position attribute.
size Vector2 | Vector3 | Number Drawing size. If vector3 is provided, will pass z value to the shader code position attribute for the bottom vertices, as position.z + size.z.
sourceRectangle Rectangle Source rectangle, or undefined to use the entire texture.
color Color | Array.<Color> | undefined Tint color, or undefined to not change color. If array is set, will assign each color to different vertex, starting from top-left.
[rotation] Number Rotate sprite.
[origin] Vector2 Drawing origin. This will be the point at ‘position’ and rotation origin.
[skew] Vector2 Skew the drawing corners on X and Y axis, around the origin point.