Pixijs Scene Text

作者 pixijs83760c6f5346MIT346 个星标收录于 2026年10月8日更新于 2026年10月8日仓库7天前更新

Use this skill when rendering text in PixiJS v8. Covers Text for canvas-quality styled labels, BitmapText for cheap per-frame updates via glyph atlas, HTMLText for HTML/CSS markup via SVG, SplitText and SplitBitmapText for per-character animation, TextStyle, tagStyles, constructor options, TextOptions, HTMLTextOptions, BitmapText, SplitTextOptions, SplitBitmapTextOptions. Triggers on: Text, BitmapText, HTMLText, SplitText, SplitBitmapText, TextStyle, HTMLTextStyle, BitmapFont.install, tagStyles, fontFamily, wordWrap.

AI 生成的概览

指导在 PixiJS v8 中使用 Text、BitmapText、HTMLText、SplitText 和 SplitBitmapText 渲染文本。

功能
讲解 PixiJS v8 的五个文本渲染类,以及样式、性能和动画之间的取舍。涵盖选项对象构造函数、TextStyle 与 tagStyles、更新开销、常见错误和 API 参考。产出的是指导与代码示例,而非文件或脚本。
适用场景
适用于在 PixiJS v8 场景中实现或调试文本渲染,例如样式化标签、每帧更新的分数或计时器、HTML 标记或逐字符动画。也适合在多个文本类之间做选择或排查更新开销问题。
运行要求
目标项目需使用 PixiJS v8。仅为说明文档,不附带脚本。假定读者熟悉 PixiJS 核心概念,并可能引用字体加载与性能相关技能。

PixiJS has five text-rendering classes that cover different trade-offs between styling, performance, and animation. Text renders to a canvas for full CSS-style fidelity. BitmapText reads from a pre-generated atlas for cheap updates. HTMLText renders an HTML fragment via SVG <foreignObject> for rich markup. SplitText and SplitBitmapText wrap the first two classes and expose per-character, per-word, and per-line containers for animation.

Assumes familiarity with pixijs-scene-core-concepts. All text classes are leaf nodes; they cannot have children. Wrap multiple text instances in a Container to group them.

Quick Start

ts
const text = new Text({  text: "Hello PixiJS",  style: {    fontFamily: "Arial",    fontSize: 36,    fill: 0xffffff,    stroke: { color: 0x4a1850, width: 5 },    dropShadow: { color: 0x000000, blur: 4, distance: 6 },  },});
text.anchor.set(0.5);text.x = app.screen.width / 2;text.y = 40;
app.stage.addChild(text);

All text classes use options-object constructors; positional (string, style) from v7 is not supported.

Related skills: pixijs-scene-core-concepts (leaves, transforms), pixijs-assets (font loading), pixijs-performance (BitmapText tradeoffs), pixijs-color (FillInput for fill/stroke), pixijs-scene-graphics (gradients and patterns reused via FillInput).

Variants

VariantUse whenTrade-offsReference
TextHigh-quality static or infrequent-update labelsExpensive to update (canvas re-draw + GPU upload)references/text.md [blocked]
BitmapTextScores, timers, gameplay labels, anything that changes every frameLimited styling; fixed glyph atlas; requires MSDF for crisp scalingreferences/bitmap-text.md [blocked]
HTMLTextRich formatted text, mixed styles, real HTML tagsAsync rendering (one frame delay); similar update cost to Textreferences/html-text.md [blocked]
SplitTextPer-character animation with rich stylingEach char is a full Text; expensive for long stringsreferences/split-text.md [blocked]
SplitBitmapTextPer-character animation on long strings or dynamic contentInherits BitmapText limitations (glyph atlas, no MSDF-free crispness)references/split-bitmap-text.md [blocked]

When to use what

  • "I need a styled static label" → Text. Use for titles, menus, dialog, error messages. See references/text.md.
  • "I need a score or timer that updates every frame" → BitmapText. Updates only reposition quads; no canvas re-draw. See references/bitmap-text.md.
  • "I need mixed formatting with <b>, <i>, <br>" → HTMLText. Real HTML/CSS rendering via SVG. See references/html-text.md.
  • "I need inline colored tags like <red>Warning:</red>" → Text or HTMLText with tagStyles. Both support it.
  • "I need to animate each character individually" → SplitText for short strings, SplitBitmapText for long strings or many instances. See references/split-text.md / references/split-bitmap-text.md.
  • "I need CJK / Arabic / emoji-heavy text" → Text or HTMLText. BitmapText fails because the glyph set is too large for a single atlas.
  • "I need a custom font" → Load via Assets.load({ src: 'font.woff2', data: { family: 'MyFont' } }) first, then set style.fontFamily: 'MyFont'. Works for Text and HTMLText.

Update cost comparison

Update triggerTextBitmapTextHTMLTextSplitTextSplitBitmapText
Changing .textHighVery lowHighVery high (N text re-renders)Low (N quad repositions)
Changing .styleHighMediumHighVery highMedium
Moving (.x, .y)FreeFreeFreeFreeFree
Rotating / scalingFreeFreeFreeFreeFree

"Free" = normal Container transform cost. "High" = new canvas draw + GPU upload. "Very low" = quad reposition only. Update strings that change per-frame only on BitmapText or SplitBitmapText.

Quick concepts

  • Options-object constructors. Every v8 text class uses new Text({ text, style, ... }). The v7 (string, style) form is removed.
  • tagStyles. Text and HTMLText support per-tag styling via style.tagStyles. Tags are only parsed when tagStyles has entries; otherwise < is treated literally.
  • BitmapFont.install. Pre-generates an atlas before you create any BitmapText. Without install, the first BitmapText with a new fontFamily generates the atlas lazily.
  • MSDF fonts. Multi-channel Signed Distance Field fonts stay sharp at any size. Generate with external tools (e.g., msdf-bmfont), load via Assets.load('font.fnt'). Requires import 'pixi.js/text-bitmap' in custom builds.

Common Mistakes

[HIGH] Updating Text.text every frame

Wrong:

ts
app.ticker.add(() => {  scoreText.text = `Score: ${score}`;});

Correct:

ts
const scoreText = new BitmapText({ text: "Score: 0", style });app.ticker.add(() => {  scoreText.text = `Score: ${score}`;});

Every Text update re-rasterizes the whole string. Use BitmapText for any value that changes per-frame.

[HIGH] Positional constructor args

Wrong:

ts
const text = new Text("Hello", { fontSize: 24 });

Correct:

ts
const text = new Text({ text: "Hello", style: { fontSize: 24 } });

v8 removed the (string, style) form. All text classes use options objects.

[HIGH] Not importing pixi.js/text-bitmap in custom builds

Under skipExtensionImports: true or aggressive tree-shaking, Assets.load('font.fnt') silently returns raw data unless you add import 'pixi.js/text-bitmap'. The standard import { ... } from 'pixi.js' bundle includes the extension.

[MEDIUM] Adding children to a text instance

Every text class sets allowChildren = false. Wrap in a Container to group text with other content.

API Reference

来源与署名

来源:pixijs/pixijs-skills位于skills/pixijs-scene-text提交83760c6

许可证: MIT

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架