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.
A bitmap font from a web font
Section titled “A bitmap font from a web font”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.
A bitmap font from files (BMFont)
Section titled “A bitmap font from files (BMFont)”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
.fntfile, in BMFont’s text or XML format; - a
.jsonfile in BMFont’s JSON format (info,common,pages,chars,kernings): a.jsonwithcharsand noframesis 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.
Drawing with a bitmap font
Section titled “Drawing with a bitmap font”{ 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.
sizescales the font: without it, the text is at the font’s own size.alignandywork as for baked texts:xis the start, the middle or the end,ythe middle of the line.- The look is the font’s:
color,outlineandshadowin the text’s style are ignored.tintmultiplies the glyphs, so a white font tinted withhand.colorgives one font for every player. - One line only:
\nis not a line break.
Changing text.text is free. Changing the style (text.style = { size: 72 }) is free too, since nothing is baked.
Which to use
Section titled “Which to use”- Baked text (
engine.textwithoutbitmapFont): 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.