So, as stated in the documentation for loadFont
(emphasis mine):
Load(s) a bitmap font into asset manager...
So you're not going to be able load e.g. a .woff
with that method, as it can only load bitmap fonts, e.g .png
.
This being said, although there doesn't seem to be much further in the docs about this, there was a PR from a few months ago that was merged into kaboom. It seems to allow for the engine to render font glyphs to an off-screen canvas using the browser's HTML5 canvas APIs, and then stitch those together to dynamically create a PNG font from normal browser fonts.
I had a quick look through the source code and it seems to be quite a promising path: https://github.com/replit/kaboom/blob/master/src/kaboom.ts#L2419
So, how do I use a non-bitmap font?
From what I can gather, you can just set your font styles to either
- The name of a bitmap font loaded via
loadFont
; or
- The name of any font that the HTML5 canvas can render
Thus, we could use e.g. font: "sans-serif"
, font: "arial"
, etc if we wanted to use any of the system fonts, and if you'd like to use a custom font, you can just load it onto the page using CSS, and then set font:
equal to the name you give it in your @font-face
rule.
This answer covers loading CSS fonts for HTML5 canvas, but I'll show a brief example here too:
In a CSS file, lets call it fonts.css
:
@font-face {
font-family: 'kenney-blocks';
src: url('fonts/Kenney-Blocks.woff');
}
In your main HTML file, in the <head>
section:
<link rel="stylesheet" href="fonts.css" />
In your Kaboom setup:
kaboom({
font: "kenney-blocks",
// ...
Or, you could update Kaboom
Further digging reveals that while they haven't updated their docs, the latest version of Kaboom actually has two loadFont
methods, with the old version from the docs being renamed to loadBitmapFont
.
If you update to the latest version of the library, you'll be able to use the following (and note that you don't need to pass in the bitmap size parameters as it's not trying to load a bitmap font anymore)
loadFont("kenny-blocks", "Kenney-Blocks.woff");
(see https://github.com/replit/kaboom/blob/master/src/kaboom.ts#L987)
However, since they've clearly renamed some stuff, this could potentially be a breaking change, which might be worth keeping in mind if you've already written quite a lot of your game.