SpriteRenderer

The SpriteRenderer component renders 2D images (sprites) to the screen.
It offers features such as image slicing, scaling, rotation, flipping, opacity, color masking, tinting, and assignment to specific render layers.
Images can be rendered with custom dimensions, positioned with offsets, and even tiled across an area.

Properties

PropertyTypeDescription
layerstringRender layer where the sprite will be drawn.
imageHTMLImageElement | stringImage to render. Can be an image object or a file path string.
sliceSliceCrops the image using coordinates starting from the top-left corner.
smoothbooleanIf true, applies pixel smoothing (not recommended for pixel art).
offsetVector2Offset on the X and Y axes relative to the entity’s center.
flipHorizontallybooleanIf true, flips the image horizontally.
flipVerticallybooleanIf true, flips the image vertically.
rotationnumberImage rotation in radians.
opacitynumberImage opacity, value between 0 and 1.
maskColorstringMask color to apply over the image.
maskColorMixnumberMask color mix intensity, value between 0 and 1.
tintColorstringColor used to tint the image.
scaleVector2Scale of the image on the X and Y axes.
widthnumberCustom width for the image (overrides the original width).
heightnumberCustom height for the image (overrides the original height).
tiledVector2Defines how many times the image will repeat horizontally and vertically.

Minimal example

const spriteRenderer = new SpriteRenderer({
    image: "mySprite.png",
});

Full example

const spriteRenderer = new SpriteRenderer({
    image: this.assetManager.getImage("image.png"),
    width: 1920,
    height: 1080,
    offset: new Vector2(0, 0),
    flipHorizontally: false,
    flipVertically: false,
    rotation: 0,
    opacity: 1,
    maskColor: "#FF0000",
    maskColorMix: 0,
    tintColor: "#00FF00",
    layer: "Default",
    slice: { x: 0, y: 0, width: 1920, height: 1080 },
    scale: new Vector2(1, 1),
    tiled: new Vector2(1, 1),
    smooth: false,
});

Notes

  • Render layers (layer) determine which cameras can see this sprite (according to each Camera’s configured layers).
  • Pixel smoothing (smooth) can improve vector graphics or high-resolution images but is not recommended for pixel art.
  • The slice property allows rendering only a specific portion of the image.
  • Rendering logic and the application of masks, tints, flips, and tiling are handled by the system that processes SpriteRenderer components. The component only defines visual properties.