TilemapRenderer

The TilemapRenderer component renders a tile-based map. It uses a tileset image as the source for individual tiles, arranged according to an array of tile IDs. It uses the entity’s Transform for position. See Rendering for an overview.

Each tile is referenced by an ID, where 0 represents empty space. The tile data can be provided directly, or populated from a Tiled map with the TiledWrapper component.

Options

OptionTypeDefaultDescription
tilesetTilesetThe tileset that provides the tiles (see below).
datanumber[][]Array of tile IDs. 0 is empty space.
chunksChunk[][]Tile data split into chunks, for large maps.
widthnumber0Map width in tiles.
heightnumber0Map height in tiles.
tileWidthnumberRendered tile width.
tileHeightnumberRendered tile height.
layerstring"Default"The render layer.
opacitynumber1Opacity between 0 and 1.
tintColorstringColor used to tint the tiles.
maskColorstringMask color applied to the tiles.
maskColorMixnumberMask color opacity between 0 and 1.
smoothbooleanfalseSmooths pixels. Not recommended for pixel art.
animationsMap<number, TileAnimation>emptyAnimated tiles, keyed by the tile ID to animate (see below).

Tileset

FieldTypeDescription
imageHTMLImageElement | stringThe tileset image, or an asset URL/name string.
widthnumberTileset width in tiles.
tileWidthnumberTile width in pixels.
tileHeightnumberTile height in pixels.
marginVector2Optional margin (top and left) in pixels.
spacingVector2Optional spacing (bottom and right) in pixels.

Tile animations

A TileAnimation cycles a tile through a sequence of tileset tile IDs. The animations map is keyed by the tile ID that should animate: every tile in the map with that ID plays the animation. Animations always loop.

OptionTypeDefaultDescription
tilesnumber[][]The sequence of tile IDs to cycle through.
fpsnumber12Frames per second.

Example

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

this.entityManager.createEntity([
    new Transform(),
    new TilemapRenderer({
        layer: "Default",
        tileset: {
            image: this.assetManager.getImage("tileset.png"),
            width: 8,
            tileWidth: 16,
            tileHeight: 16,
        },
        data: [1, 2, 3, 4],
        width: 2,
        height: 2,
    }),
]);

Animated tiles example

import { Transform, TilemapRenderer, TileAnimation } from "angry-pixel";

this.entityManager.createEntity([
    new Transform(),
    new TilemapRenderer({
        tileset: {
            image: this.assetManager.getImage("tileset.png"),
            width: 8,
            tileWidth: 16,
            tileHeight: 16,
        },
        data: [1, 2, 3, 4],
        width: 2,
        height: 2,
        // Every tile with ID 3 cycles through 3, 4, 5 at 6 fps.
        animations: new Map([[3, new TileAnimation({ tiles: [3, 4, 5], fps: 6 })]]),
    }),
]);