FontImage

Font Image Object - Draw text on a 2D canvas by using characters in an image

  • 96 characters (from space to tilde) are stored in an image
  • Uses a default 8x8 font if none is supplied
  • You can also use fonts from the main tile sheet

Constructor

new FontImage(imageopt, tileSizeopt, paddingSizeopt)

Create an image font

Parameters:
NameTypeAttributesDefaultDescription
imageHTMLImageElement<optional>

Image for the font, if undefined default font is used

tileSizeVector2<optional>
(8,8)

Size of the font source tiles

paddingSizeVector2<optional>
(0,1)

How much extra space to add between characters

Example
// use built in font
const font = new FontImage;

// draw text
font.drawTextScreen('LittleJS\nHello World!', vec2(200, 50));

Methods

drawText(text, pos, scaleopt, centeropt, contextopt)

Draw text in world space using the image font

Parameters:
NameTypeAttributesDefaultDescription
textstring
posVector2
scalenumber<optional>
.25
centerboolean<optional>
contextCanvasRenderingContext2D | OffscreenCanvasRenderingContext2D<optional>
drawContext

drawTextOverlay(text, pos, scaleopt, centeropt)

Draw text on overlay canvas in world space using the image font

Parameters:
NameTypeAttributesDefaultDescription
textstring
posVector2
scalenumber<optional>
4
centerboolean<optional>

drawTextScreen(text, pos, scaleopt, centeropt, contextopt)

Draw text on overlay canvas in screen space using the image font

Parameters:
NameTypeAttributesDefaultDescription
textstring
posVector2
scalenumber<optional>
4
centerboolean<optional>
contextCanvasRenderingContext2D | OffscreenCanvasRenderingContext2D<optional>
drawContext