TextRenderer

The TextRenderer component renders text to the screen. It works with web-safe and imported fonts, and works optimally with bitmap fonts. It uses the entity’s Transform for position. See Rendering for an overview.

Options

OptionTypeDefaultDescription
textstring"Hello World!"The text to render.
colorstring"#000000"The text color.
fontFontFace | string"Arial"The font family.
fontSizenumber16Font size in pixels.
widthnumber192Width of the box the text is rendered in.
heightnumber16Height of the box the text is rendered in.
layerstring"Default"The render layer.
alignmentTextAlignmentTextAlignment.CenterText alignment: Left, Center, or Right.
lineHeightnumberfont sizeLine height in pixels.
letterSpacingnumber0Space between characters in pixels.
offsetVector2(0, 0)Offset from the entity’s position.
rotationnumber0Rotation in radians.
opacitynumber1Opacity between 0 and 1.
flipHorizontallybooleanfalseFlips the text horizontally.
flipVerticallybooleanfalseFlips the text vertically.
smoothbooleanfalseSmooths pixels. Not recommended for bitmap fonts.
shadow{ color: string; offset: Vector2; opacity: number }Optional text shadow.
textureAtlas{ charRanges?: number[]; fontSize?: number; spacing?: number }charRanges: [32, 126, 161, 255], fontSize: 64, spacing: 8Configuration of the generated character atlas.

The textureAtlas.charRanges option defines, in number pairs, the ranges of characters the component can render. The default [32, 126, 161, 255] covers characters 32–126 and 161–255.

Example

import { Transform, TextRenderer, TextAlignment } from "angry-pixel";

this.entityManager.createEntity([
    new Transform(),
    new TextRenderer({
        text: "Score: 0",
        color: "#FFFFFF",
        fontSize: 24,
        layer: "UI",
        alignment: TextAlignment.Left,
    }),
]);

Imported fonts are loaded through the asset manager with loadFont.