Class: Sprite

Sprite()

new Sprite()

Represents a visual object that can be drawn on the canvas. It can render an entire image or a specific rectangular portion of an image (a frame from a sprite sheet).
Source:

Members

anchor :Object

- Anchor point for rotation and scaling (0-1 scale, e.g., {x:0.5, y:0.5} is center).
Type:
  • Object
Source:

opacity :number

- Opacity of the sprite (0 = transparent, 1 = opaque).
Type:
  • number
Source:

rotation :number

- Rotation angle in radians. Rotation is around the sprite's anchor point.
Type:
  • number
Source:

scaleX :number

- Horizontal scale factor.
Type:
  • number
Source:

scaleY :number

- Vertical scale factor.
Type:
  • number
Source:

visible :boolean

- Whether the sprite should be rendered.
Type:
  • boolean
Source:

Methods

render(context)

Renders the sprite on the canvas context. Takes into account visibility, opacity, rotation, and scaling.
Parameters:
Name Type Description
context CanvasRenderingContext2D The 2D rendering context of the canvas.
Source:

setFrame(sx, sy, sWidth, sHeight)

Sets the source frame for the sprite, useful for sprite sheet animations.
Parameters:
Name Type Description
sx number The x-coordinate of the top-left corner of the frame in the source image.
sy number The y-coordinate of the top-left corner of the frame in the source image.
sWidth number The width of the frame in the source image.
sHeight number The height of the frame in the source image.
Source:

update(deltaTime)

Updates the sprite's state. Placeholder for now, could be used for animations internal to the sprite.
Parameters:
Name Type Description
deltaTime number The time elapsed since the last frame, in seconds.
Source:

Sprite(image, xopt, yopt, widthopt, heightopt, sxopt, syopt, sWidthopt, sHeightopt)

new Sprite(image, xopt, yopt, widthopt, heightopt, sxopt, syopt, sWidthopt, sHeightopt)

Creates an instance of Sprite.
Parameters:
Name Type Attributes Default Description
image HTMLImageElement The HTMLImageElement to be rendered. Should be pre-loaded.
x number <optional>
0 The x-coordinate for the sprite's top-left corner on the canvas.
y number <optional>
0 The y-coordinate for the sprite's top-left corner on the canvas.
width number <optional>
The width to draw the sprite on the canvas. Defaults to source frame width or image width.
height number <optional>
The height to draw the sprite on the canvas. Defaults to source frame height or image height.
sx number <optional>
0 The x-coordinate of the top-left corner of the sub-rectangle (frame) in the source image.
sy number <optional>
0 The y-coordinate of the top-left corner of the sub-rectangle (frame) in the source image.
sWidth number <optional>
The width of the sub-rectangle (frame) in the source image. Defaults to image width.
sHeight number <optional>
The height of the sub-rectangle (frame) in the source image. Defaults to image height.
Source:

Members

anchor :Object

- Anchor point for rotation and scaling (0-1 scale, e.g., {x:0.5, y:0.5} is center).
Type:
  • Object
Source:

opacity :number

- Opacity of the sprite (0 = transparent, 1 = opaque).
Type:
  • number
Source:

rotation :number

- Rotation angle in radians. Rotation is around the sprite's anchor point.
Type:
  • number
Source:

scaleX :number

- Horizontal scale factor.
Type:
  • number
Source:

scaleY :number

- Vertical scale factor.
Type:
  • number
Source:

visible :boolean

- Whether the sprite should be rendered.
Type:
  • boolean
Source:

Methods

render(context)

Renders the sprite on the canvas context. Takes into account visibility, opacity, rotation, and scaling.
Parameters:
Name Type Description
context CanvasRenderingContext2D The 2D rendering context of the canvas.
Source:

setFrame(sx, sy, sWidth, sHeight)

Sets the source frame for the sprite, useful for sprite sheet animations.
Parameters:
Name Type Description
sx number The x-coordinate of the top-left corner of the frame in the source image.
sy number The y-coordinate of the top-left corner of the frame in the source image.
sWidth number The width of the frame in the source image.
sHeight number The height of the frame in the source image.
Source:

update(deltaTime)

Updates the sprite's state. Placeholder for now, could be used for animations internal to the sprite.
Parameters:
Name Type Description
deltaTime number The time elapsed since the last frame, in seconds.
Source: