Options
All
  • Public
  • Public/Protected
  • All
Menu

Class FontRenderer

FontRenderer renders Minecraft Font text onto the given 2D canvas context.

You must call loadFontImage to load the font onto the internal canvas. Example:

let renderer = new FontRenderer(ctx);
renderer.loadFontImage().then(() => {
    renderer.drawStringFormatted("Hello!");
});

Hierarchy

  • FontRenderer

Index

Constructors

constructor

  • new FontRenderer(ctx: CanvasRenderingContext2D, scaleFactor?: number): FontRenderer
  • Creates a FontRenderer with a given 2D canvas context, and an optional scale factor for displaying text.

    Parameters

    • ctx: CanvasRenderingContext2D
    • Default value scaleFactor: number = 2

    Returns FontRenderer

Properties

context

context: CanvasRenderingContext2D

The rendering context that the text will be rendered onto.

Accessors

scaleFactor

  • get scaleFactor(): number
  • set scaleFactor(newScale: number): void
  • The scale factor (like Minecraft's GUI scale setting) for text. Defaults to 2x.

    Returns number

  • The scale factor (like Minecraft's GUI scale setting) for text. Defaults to 2x.

    Parameters

    • newScale: number

    Returns void

Methods

drawString

  • drawString(str: string, x?: number, y?: number, style?: FontStyle): void
  • Draws a string component at the given position with a given FontStyle. It is recommended to use drawStringFormatted or drawStringFormattedShadow instead.

    Parameters

    • str: string
    • Default value x: number = 0
    • Default value y: number = 0
    • Default value style: FontStyle = new FontStyle()

    Returns void

drawStringFormatted

  • drawStringFormatted(str: string, x?: number, y?: number, shadow?: boolean): void
  • Draws a string (parsed for formatting codes) at a given position. Takes an optional shadow argument to display with darker colors, although drawStringFormattedShadow handles adding the shadow for you.

    Parameters

    • str: string
    • Default value x: number = 0
    • Default value y: number = 0
    • Default value shadow: boolean = false

    Returns void

drawStringFormattedShadow

  • drawStringFormattedShadow(str: string, x?: number, y?: number): void
  • Draws a string (parsed for formatting codes) at a given position, with a shadow added behind it.

    Parameters

    • str: string
    • Default value x: number = 0
    • Default value y: number = 0

    Returns void

drawStringTooltip

  • drawStringTooltip(str: string, x?: number, y?: number): void
  • Draws a string in the style used for tooltips

    Parameters

    • str: string
    • Default value x: number = 0
    • Default value y: number = 0

    Returns void

getCharWidth

  • getCharWidth(char: string): number
  • Get the width that a single character (not bolded) will take up on the canvas.

    Parameters

    • char: string

    Returns number

getFormattedStringWidth

  • getFormattedStringWidth(str: string): number
  • Get the width that a formatted string will take up on the canvas.

    Parameters

    • str: string

    Returns number

getStringWidth

  • getStringWidth(str: string, bold: boolean): number
  • Get the width that a string component will take up on the canvas. Do not use this for formatted text.

    Parameters

    • str: string
    • bold: boolean

    Returns number

getTooltipStringHeight

  • getTooltipStringHeight(_str: string): number
  • Gets the height of a string in the style used for tooltips

    Parameters

    • _str: string

    Returns number

getTooltipStringWidth

  • getTooltipStringWidth(str: string): number
  • Gets the width of a string in the style used for tooltips

    Parameters

    • str: string

    Returns number

loadFontImage

  • loadFontImage(src?: string): Promise<void>
  • Loads the ascii.png image into the internal font canvas. Takes an optional src parameter to change the location of ascii.png.

    Parameters

    • Default value src: string = "ascii.png"

    Returns Promise<void>

Generated using TypeDoc