Skip to content

Bitmap fonts

A text is baked: drawn once into the atlas, then moved as an image. Changing its string bakes it again, which is fine for a title and wasteful for a score that changes at every catch, or a timer that changes every frame.

A bitmap font is a set of glyphs, each a frame. A text drawn with one is laid out from its glyphs at every draw, one quad per character, and bakes nothing when it changes. Use one for counters, scores and timers.

engine.bitmapFont('score', { size: 56, outline: true, chars: '0123456789' })
const score = stage.add(engine.text('0', { bitmapFont: 'score', x: 40, y: 50, align: 'left' }))
engine.onFrame(() => {
score.text = String(points) // nothing baked, whatever the value
})

engine.bitmapFont(name, options) makes a font from the page’s fonts, with the look of engine.text. The options are the text style’s:

Option Default What it is
size 40 The size the glyphs are drawn at, stage units
font, weight Fredoka, 700 As for texts
color cream, #FBEFE3 The glyphs’ colour
outline, outlineWidth none A dark outline, as for texts
shadow, shadowBlur none A soft shadow, on the high tier only, as for texts
chars the digits Characters baked now; any other is baked the first time it is drawn

Each glyph is baked into the atlas once, at the backing store’s resolution. They bake again by themselves after a resize, a tier change, a web font arriving, or an atlas wipe. List in chars what the font will show ('0123456789:' for a timer), so they are ready before the game starts.

await engine.load({ digits: 'fonts/digits.fnt' })
const timer = stage.add(engine.text('60', { bitmapFont: 'digits', x: 640, y: 60 }))

Tools such as BMFont, Hiero, Glyph Designer and msdf-bmfont export a description of the font and one or more page images. engine.load reads:

  • a .fnt file, in BMFont’s text or XML format;
  • a .json file in BMFont’s JSON format (info, common, pages, chars, kernings): a .json with chars and no frames is read as a font, not an atlas.

The page images are loaded from next to the file, under the names it gives. Kerning pairs are applied.

From memory, for a font you made or fetched yourself: engine.bitmapFont(name, { data, pages }), with data the file’s text (or the parsed JSON) and pages the page images (HTMLImageElement, canvas or ImageBitmap), by page id.

Export the pages without rotated glyphs: the engine does not read them.

{ bitmapFont: name } in the text’s style draws it with the font (the font itself, a BitmapFont, works too). The font must be made or loaded first: no bitmap font "name": load or make it first otherwise.

  • size scales the font: without it, the text is at the font’s own size.
  • align and y work as for baked texts: x is the start, the middle or the end, y the middle of the line.
  • The look is the font’s: color, outline and shadow in the text’s style are ignored. tint multiplies the glyphs, so a white font tinted with hand.color gives one font for every player.
  • One line only: \n is not a line break.

Changing text.text is free. Changing the style (text.style = { size: 72 }) is free too, since nothing is baked.

  • Baked text (engine.text without bitmapFont): titles, labels, anything that changes a few times a game. Any character, any language, no setup.
  • Bitmap font: scores, counters, timers, anything that changes often. A few dozen glyphs, made once.