Fonts
Built-in fonts and custom fonts for use with the Canvas API
When your application is using canvas rendering mode, nx.js offers a set of built-in fonts which are available for use in your application. You may also load custom fonts from font files.
Built-in fonts
nx.js comes with a set of built-in fonts which are available for use in your application.
Font | Description |
---|---|
"system-ui" | The default font which is used by the Switch operating system. |
"system-icons" | A font which contains the icons used by the Switch operating system. |
system-ui
The default font which is used by the Switch operating system.
The Canvas API's default font sans-serif
is also an alias for this font.
system-icons
The "system-icons"
font contains icons used by the Switch operating system.
When using this font, you will need to reference the correct unicode codepoint for the icon
when invoking the fillText()
function to render the icon glyphs.
Custom fonts
It is possible to load your own font files into an nx.js application, which involves a few steps:
- Create a
FontFace
instance - Register the font with the
FontFaceSet
registry - Use the font with the Canvas API
Loading a font
To load a font, you'll need to create an instance of the
FontFace
class,
by providing the name of the font and the font data as an ArrayBuffer
:
The FreeType 2 library is used to renders fonts,
so any font file format which is supported by FreeType 2 (typically .ttf
or .otf
) will work with nx.js.
Registering a font
Once you've loaded a font, you'll need to register it with the
FontFaceSet
registry.
The fonts
global is available for this purpose,
similar to how you would register a font in a "web worker" context.
The fonts
registry is a global object that contains all the fonts that have been
loaded into the application. You can use this registry to render text to the screen
using the Canvas API.