PixiJS provides three powerful text rendering systems, each optimized for different use cases:
Text
)High-quality text rendering using the browser's canvas engine.
import { Text } from 'pixi.js';
// Basic text
const text = new Text({
text: 'Hello PixiJS!',
style: {
fontFamily: 'Arial',
fontSize: 32,
fill: '#ffffff',
}
});
// Rich styling
const styledText = new Text({
text: 'Styled Text',
style: {
fontFamily: 'Arial',
fontSize: 48,
fill: ['#ff0000', '#00ff00'], // Gradient
stroke: { color: '#000000', width: 4 },
dropShadow: {
color: '#000000',
blur: 4,
distance: 6,
angle: Math.PI / 6
}
}
});
Best for:
BitmapText
)High-performance text using pre-rendered font atlases.
import { BitmapFont, BitmapText } from 'pixi.js';
// Install a bitmap font
BitmapFont.install({
name: 'Game Font',
style: {
fontFamily: 'Arial',
fontSize: 32,
fill: '#ffffff'
}
});
// Create bitmap text
const text = new BitmapText({
text: 'High Performance!',
style: {
fontFamily: 'Game Font',
fontSize: 32
}
});
// Load external bitmap font
const font = await Assets.load('fonts/game.fnt');
const text2 = new BitmapText({
text: 'Using loaded font',
style: {
fontFamily: font.fontFamily
}
});
Best for:
HTMLText
)HTML and CSS-based text rendering within the PixiJS scene.
import { HTMLText } from 'pixi.js';
// Basic HTML text
const text = new HTMLText({
text: '<b>Bold</b> and <i>Italic</i>',
style: {
fontSize: 24,
fill: '#ffffff'
}
});
// Rich HTML text with styling
const richText = new HTMLText({
text: '<custom>Custom Tag</custom>',
style: {
fontFamily: 'Arial',
fontSize: 32,
fill: 0x4a4a4a,
align: 'center',
tagStyles: {
custom: {
fontSize: 32,
fill: '#00ff00',
fontStyle: 'italic'
}
}
}
textureStyle: {
scaleMode: 'linear',
}
});
Best for:
Text
: Medium (one texture per unique text)BitmapText
: Low (shared texture atlas)HTMLText
: High (DOM elements + textures)Text
: MediumBitmapText
: FastHTMLText
: SlowText
: Slow for frequent updatesBitmapText
: Fast for any updatesHTMLText
: Medium for updatesChoose the Right Type
Text
for styled, occasionally updated textBitmapText
for frequently updated or numerous textHTMLText
for rich formatting needsPerformance Tips
cacheAsBitmap
Memory Management