Text
Text rendering is a crucial part of any game UI. dill-pixel provides several text implementations through Pixi.js, each with its own advantages and use cases.
Basic Text
The most straightforward way to add text is using the basic text implementation:
Style Options
Basic text supports various style options:
Advantages
- Simple to implement
- Good performance for static text
- Supports basic formatting
Pitfalls
- Limited styling options
- No rich text formatting
- Can be blurry if scaled
HTML Text
For more complex text formatting, use HTML text:
Supported HTML Tags
<strong>
or<b>
for bold text<em>
or<i>
for italic text<u>
for underlined text<s>
for strikethrough text<span>
with style attributes for custom formatting
Advantages
- Rich text formatting
- Supports HTML-style markup
- Color and style variations within text
- Familiar HTML syntax
Pitfalls
- Higher performance overhead than basic text
- More complex to implement
- Limited HTML tag support
- May have inconsistent rendering across platforms
Bitmap Text
For optimal performance, especially in games with lots of text updates, use bitmap text:
Advantages
- Best performance
- Consistent rendering across platforms
- Perfect for frequently updated text (scores, timers)
- Crisp at any scale
Pitfalls
- Requires pre-generated bitmap fonts
- Limited to characters included in the font
- No dynamic styling
- Fixed color unless using tint
- Larger asset size
Implementation Example
Here’s a complete example showing all three text types:
Font Loading
Web and bitmap fonts need to be loaded via assetpack before they can be used in your game.
Here’s how you might structure your assets folder to include a web and bitmap font version of the same font:
Directoryassets
Directoryrequired{m}
Directoryfonts{fix} // ensures assetpack mipmap plugin doesn’t resize any image files inside this folder
- KumbhSans-Bold{family=KumbhSans}{wf}.ttf
- KumbhSans-Medium{family=KumbhSans}{wf}.ttf
- KumbhSans-Regular{family=KumbhSans}{wf}.ttf
- KumbhSans-SemiBold{family=KumbhSans}{wf}.ttf
- KumbhSansBlack.fnt // bitmap font (font name inside the file needs to be the same as the file name, and will be the bitmap font family name)
- KumbhSansBlack.png // bitmap font image
This setup ensures that fonts will be loaded as part of the required assets, and will be available to your game.