SpriteRenderer

The SpriteRenderer component renders an image (sprite) to the screen. It supports slicing, scaling, rotation, flipping, opacity, color masking, tinting, and tiling. It uses the entity’s Transform for position. See Rendering for an overview.

Options

OptionTypeDefaultDescription
imageHTMLImageElement | string—The image to render, or an asset URL/name string.
layerstring"Default"The render layer.
sliceSlice—Region of the image to draw ({ x, y, width, height }).
widthnumber—Overrides the rendered width.
heightnumber—Overrides the rendered height.
scaleVector2(1, 1)Scales the image.
offsetVector2(0, 0)Offset from the entity’s position.
rotationnumber0Rotation in radians.
flipHorizontallybooleanfalseFlips the image horizontally.
flipVerticallybooleanfalseFlips the image vertically.
opacitynumber1Opacity between 0 and 1.
tintColorstring—Color used to tint the image.
maskColorstring—Mask color applied to the image.
maskColorMixnumber—Mask color opacity between 0 and 1.
tiledVector2—Tiles the image across the given number of columns and rows.
smoothbooleanfalseSmooths pixels. Not recommended for pixel art.

Example

import { Transform, SpriteRenderer } from "angry-pixel";

this.entityManager.createEntity([
    new Transform(),
    new SpriteRenderer({
        image: this.assetManager.getImage("player.png"),
        layer: "Default",
    }),
]);

The image is loaded through the asset manager, typically in the scene’s loadAssets method. Animated sprites are driven by the Animator component.