API Reference
Complete API documentation for the funky-pixi-text library.
Installation
npm install funky-pixi-text pixi.jsPIXI.js v7 (^7.2.4) is a peer dependency.
Since 1.1.1 — PIXI is a real peer dependency
Up to and including 1.1.0 the package bundled its own private copy of PIXI. From 1.1.1 it imports pixi.js as an external peer, so the library and your application share one PIXI instance and the published bundle drops from roughly 577 KB to 159 KB. Make sure your bundler resolves pixi.js to exactly one copy.
Quick Start
import { Application, Assets } from "pixi.js";
import { Text, FontLoader } from "funky-pixi-text";
const app = new Application({ backgroundColor: 0x222222, resizeTo: window });
document.body.appendChild(app.view as HTMLCanvasElement);
// Load an MSDF font (atlas .png + bmfont .json)
await Assets.load({
src: "fonts/Roboto-Black.json",
loadParser: FontLoader.name,
});
const label = new Text("Hello MSDF!", {
fontName: "Roboto-Black",
fontSize: 96,
color: 0xffffff,
align: "center",
anchorX: 0.5,
anchorY: 0.5,
});
label.position.set(app.screen.width / 2, app.screen.height / 2);
app.stage.addChild(label);Generating MSDF Fonts
This library consumes BMFont JSON atlases generated by msdf-bmfont-xml or compatible tools.
npm i -g msdf-bmfont-xml
msdf-bmfont -f json -o Roboto-Black --font-size 64 ./Roboto-Black.ttfThis produces two files: Roboto-Black.json (glyph metrics, kerning, atlas layout) and Roboto-Black.png (the MSDF atlas texture). Place them side-by-side and load the .json through PIXI Assets.
Loading Fonts
The package exports a PIXI Asset load parser, registered automatically as a PIXI extension when you import the library.
Loading through Assets requires 1.1.1 or newer
The parser is registered against whichever pixi.js instance the library imports. On 1.1.0 and earlier that was the private copy bundled inside the package, so an Assets.load call made from your application's own PIXI never saw the parser — it resolved without error, but Text then rendered nothing. From 1.1.1 both sides share a single instance and the snippets below work as written. On older versions, call FontLoader.load directly instead.
Loading by URL
import { Assets } from "pixi.js";
import { FontLoader } from "funky-pixi-text";
await Assets.load({
src: "fonts/Roboto-Black.json",
loadParser: FontLoader.name, // "MSDFFontLoaderExtension"
});
// Reference in Text() by the file base name
new Text("Hi", { fontName: "Roboto-Black" });Loading multiple fonts
await Assets.load([
{ src: "fonts/Roboto-Black.json", loadParser: FontLoader.name },
{ src: "fonts/Tourney.json", loadParser: FontLoader.name },
{ src: "fonts/Meddon-Regular.json", loadParser: FontLoader.name },
]);Overriding the texture URL or font name
await Assets.load({
src: "fonts/MyFont.json",
loadParser: FontLoader.name,
data: {
fontName: "MyCustomKey", // key used by Text({ fontName })
textureUrl: "fonts/[email protected]", // override atlas path
},
});Calling FontLoader directly
FontLoader is a plain object, so you can bypass PIXI Assets entirely — useful when you run your own asset pipeline, or when you need font loading to work on 1.1.0 and earlier.
import { FontLoader } from "funky-pixi-text";
const font = await FontLoader.load("fonts/Roboto-Black.json", {
src: "fonts/Roboto-Black.json",
data: {}, // optional { fontName, textureUrl } overrides
});
font.texture; // PIXI.Texture — the MSDF atlas
font.metrics; // BMFontData — glyphs, kerning, distanceField
await FontLoader.unload(font); // frees the atlas texture| Member | Description |
|---|---|
| load(url, asset): Promise<LoadedFont> | Fetches the BMFont JSON and its atlas, and registers the font under data.fontName or the file base name. |
| unload(font, asset?): Promise<void> | Destroys the atlas texture and drops the font from the cache. |
| name: string | Parser id — "MSDFFontLoaderExtension". Pass as loadParser to Assets.load. |
| extension: ExtensionType | Extension descriptor used to self-register with PIXI on import. |
The Text Class
Text extends PIXI.Container and emits a single batched PIXI.Mesh containing all glyphs.
Constructor
new Text(text: string, style: TextStyle)Both arguments are required. The font referenced by style.fontName must already be loaded via Assets.
Instance Properties
| Property | Description |
|---|---|
| text: string | Get or set the displayed string. Assigning is equivalent to calling setText(). |
| anchor: PIXI.ObservablePoint | Observable anchor, so text.anchor.set(0.5) behaves like any other PIXI display object. Mirrors the anchorX / anchorY style values. |
| shader: PIXI.Shader | null | The shader compiled for the current combination of effects, or null until the first build completes. |
Instance Methods
| Method | Description |
|---|---|
| setText(text: string): void | Replace the displayed string and rebuild the layout. |
| setStyle(partial: Partial<TextStyle>): void | Update one or more style properties. Uses a uniform-only fast-path when possible. |
| setColor(color: number | TextStyleGradient): void | Shortcut to change fill color or gradient (uniform-only update when shape unchanged). |
| setSmoothing(value: number): void | Adjust edge AA smoothing (0.05 – 5.0). |
| setKerning(enabled: boolean, scale?: number): void | Toggle kerning and optionally scale the kerning amount. |
| getTextBounds(): PIXI.Rectangle | Returns the laid-out bounding box including padding for shadow / extrude / smoothing. |
| checkPerformanceConfigUpdate(): boolean | Compares the global performance config against the one this instance was built with, rebuilding and returning true if it changed. Always returns false when the style carries its own performanceConfig. |
| debugKerning(): void | Logs all kerning pairs present in the font. |
| destroy(options?): void | Frees geometry, material, shader, and removes the instance from internal tracking. |
Static Methods
| Method | Description |
|---|---|
| Text.enableAutoConfigUpdate(ticker?: PIXI.Ticker): () => void | Begin watching the global performance config and rebuild all live Text instances when it changes. Returns a cleanup function. |
| Text.disableAutoConfigUpdate(): void | Stop the global watcher. |
TextStyle Reference
type TextStyle = {
fontName: string; // REQUIRED — key used when font was loaded
fontSize?: number; // default 16 (logical pixels)
color?: number | TextStyleGradient; // 0xRRGGBB or gradient object; default 0xffffff
// Layout
align?: "left" | "center" | "right" | "justify"; // default "left"
anchorX?: number; // 0..1 (0 = left, 0.5 = center, 1 = right); default 0
anchorY?: number; // 0..1 (0 = top, 0.5 = middle, 1 = bottom); default 0
wordWrap?: boolean; // default false
breakWords?: boolean; // with wordWrap, split words wider than maxWidth
// mid-word instead of overflowing; default false
maxWidth?: number; // pixels, used with wordWrap and/or autoScale; default Infinity
maxHeight?: number; // pixels, used with autoScale; default Infinity
autoScale?: boolean; // shrink fontSize to fit maxWidth/maxHeight; default false
leading?: number; // extra px added to line height
lineHeight?: number; // override line height in pixels (0 = use font default)
letterSpacing?: number; // px added between characters
kerning?: boolean; // default true
kerningScale?: number; // multiplier for kerning amount; default 1.0
// Effects
stroke?: TextStyleStroke | null;
dropShadow?: TextStyleDropShadow | null;
extrude?: TextStyleExtrude | null;
fillTexture?: TextStyleTextureFill | null;
// Rendering
smoothing?: number; // edge AA half-width (0.05..5.0); default 0.02
scale?: number; // visual multiplier; default 1.0
msdfSign?: number; // 1 or -1; flip signed distance (rare)
// Performance
performanceMode?: "auto" | "high-quality" | "balanced" | "mobile-optimized";
performanceConfig?: PerformanceConfig;
};Layout behaviour changed in 1.1.0
Word wrap now takes precedence over autoScale on width: wrapped text re-flows inside maxWidth at full scale, and effect padding no longer triggers scaling. autoScale now shrinks only a single unbreakable word wider than maxWidth, or wrapped text taller than maxHeight. Empty text measures zero height rather than one line, and a character missing from the atlas logs a one-time warning per font instead of silently rendering as a gap.
Gradient
type TextStyleGradient = {
type: "linear" | "circular";
colors: number[]; // 0xRRGGBB array (up to maxGradientStops)
alphas?: number[]; // matching per-stop alphas (0..1)
stops?: number[]; // matching positions (0..1)
angle?: number; // degrees, linear only
center?: { x: number; y: number }; // circular only, 0..1 in glyph space
radius?: number; // circular only, 0..1; <=0 = auto
};Stroke
type TextStyleStroke = {
color: number; // 0xRRGGBB
width: number; // 0..10 (px in MSDF distance units)
smoothing?: number; // 0..10, default 1.0
};Drop Shadow
type TextStyleDropShadow = {
color: number; // 0xRRGGBB
angle: number; // degrees
distance: number; // px
blur: number; // 0..30 (clamped by performance config)
alpha?: number; // 0..1, default 1
};Extrude (3D)
type TextStyleExtrude = {
depth?: number; // px, default 16
steps?: number; // raymarch steps (clamped by performance config), default 24
angle?: number; // degrees, direction of extrusion, default 45
sideColor?: number; // 0xRRGGBB; defaults to a darker face color
};Texture Fill
type TextStyleTextureFill = {
url?: string; // image URL (loaded via Assets if not cached)
assetId?: string; // OR an existing Assets alias
uvScale?: [number, number]; // default [1,1]
uvOffset?: [number, number]; // default [0,0]
rotation?: number; // radians, default 0
mix?: number; // 0..1, default 1
colorAlpha?: number; // 0..1, default 1
textureAlpha?: number; // 0..1, default 1
blendMode?: TextureBlendMode; // "normal" | "multiply" | "screen" |
// "overlay" | "softLight" | "hardLight" |
// "colorDodge" | "colorBurn" |
// "vividLight" | "linearLight" |
// "luminosity" | "hue"
};