pixijs-scene-text
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePixiJS has five text-rendering classes that cover different trade-offs between styling, performance, and animation. renders to a canvas for full CSS-style fidelity. reads from a pre-generated atlas for cheap updates. renders an HTML fragment via SVG for rich markup. and wrap the first two classes and expose per-character, per-word, and per-line containers for animation.
TextBitmapTextHTMLText<foreignObject>SplitTextSplitBitmapTextAssumes familiarity with . All text classes are leaf nodes; they cannot have children. Wrap multiple text instances in a to group them.
pixijs-scene-core-conceptsContainerPixiJS 拥有五个文本渲染类,在样式、性能和动画之间做出了不同的权衡。 渲染到画布以实现完整的CSS级样式保真度。 从预先生成的图集中读取数据,实现低开销的更新。 通过SVG 渲染HTML片段,支持丰富的标记。 和 封装前两个类,暴露逐字符、逐词和逐行的容器以支持动画。
TextBitmapTextHTMLText<foreignObject>SplitTextSplitBitmapText假设你已熟悉 。所有文本类都是叶子节点;它们不能拥有子元素。若要将多个文本实例分组,请将它们包裹在 中。
pixijs-scene-core-conceptsContainerQuick 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 from v7 is not supported.
(string, style)Related skills: (leaves, transforms), (font loading), (BitmapText tradeoffs), ( for fill/stroke), (gradients and patterns reused via ).
pixijs-scene-core-conceptspixijs-assetspixijs-performancepixijs-colorFillInputpixijs-scene-graphicsFillInputts
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);所有文本类均使用选项对象构造函数;v7版本中的 位置参数形式不再受支持。
(string, style)相关技能: (叶子节点、变换)、(字体加载)、(BitmapText的权衡)、(用于填充/描边的)、(通过复用渐变和图案)。
pixijs-scene-core-conceptspixijs-assetspixijs-performancepixijs-colorFillInputpixijs-scene-graphicsFillInputVariants
变体对比
| Variant | Use when | Trade-offs | Reference |
|---|---|---|---|
| High-quality static or infrequent-update labels | Expensive to update (canvas re-draw + GPU upload) | references/text.md |
| Scores, timers, gameplay labels, anything that changes every frame | Limited styling; fixed glyph atlas; requires MSDF for crisp scaling | references/bitmap-text.md |
| Rich formatted text, mixed styles, real HTML tags | Async rendering (one frame delay); similar update cost to | references/html-text.md |
| Per-character animation with rich styling | Each char is a full | references/split-text.md |
| Per-character animation on long strings or dynamic content | Inherits BitmapText limitations (glyph atlas, no MSDF-free crispness) | references/split-bitmap-text.md |
| 变体类型 | 使用场景 | 优缺点对比 | 参考文档 |
|---|---|---|---|
| 高质量静态或低频更新的标签 | 更新开销大(画布重绘 + GPU上传) | references/text.md |
| 分数、计时器、游戏玩法标签等需要逐帧更新的内容 | 样式受限;依赖固定字形图集;需要MSDF才能实现清晰缩放 | references/bitmap-text.md |
| 富格式文本、混合样式、真实HTML标签场景 | 异步渲染(延迟一帧);更新开销与 | references/html-text.md |
| 需富样式且逐字符动画的场景 | 每个字符都是完整的 | references/split-text.md |
| 长字符串或动态内容的逐字符动画场景 | 继承BitmapText的限制(字形图集、无MSDF则无法清晰显示) | references/split-bitmap-text.md |
When to use what
场景选型指南
- "I need a styled static label" → . Use for titles, menus, dialog, error messages. See
Text.references/text.md - "I need a score or timer that updates every frame" → . Updates only reposition quads; no canvas re-draw. See
BitmapText.references/bitmap-text.md - "I need mixed formatting with ,
<b>,<i>" →<br>. Real HTML/CSS rendering via SVG. SeeHTMLText.references/html-text.md - "I need inline colored tags like " →
<red>Warning:</red>orTextwithHTMLText. Both support it.tagStyles - "I need to animate each character individually" → for short strings,
SplitTextfor long strings or many instances. SeeSplitBitmapText/references/split-text.md.references/split-bitmap-text.md - "I need CJK / Arabic / emoji-heavy text" → or
Text.HTMLTextfails because the glyph set is too large for a single atlas.BitmapText - "I need a custom font" → Load via first, then set
Assets.load({ src: 'font.woff2', data: { family: 'MyFont' } }). Works forstyle.fontFamily: 'MyFont'andText.HTMLText
- "我需要一个带样式的静态标签" → 使用。适用于标题、菜单、对话框、错误提示。详见
Text。references/text.md - "我需要逐帧更新的分数或计时器" → 使用。仅重新定位四边形;无需画布重绘。详见
BitmapText。references/bitmap-text.md - "我需要、
<b>、<i>这类混合格式" → 使用<br>。通过SVG实现真实HTML/CSS渲染。详见HTMLText。references/html-text.md - "我需要这类内联彩色标签" → 使用带
<red>警告:</red>的tagStyles或Text。两者均支持该功能。HTMLText - "我需要为每个字符单独设置动画" → 短字符串用,长字符串或多实例用
SplitText。详见SplitBitmapText/references/split-text.md。references/split-bitmap-text.md - "我需要显示CJK/阿拉伯语/大量表情符号文本" → 使用或
Text。HTMLText因字形集过大无法放入单个图集而失效。BitmapText - "我需要自定义字体" → 先通过加载,再设置
Assets.load({ src: 'font.woff2', data: { family: 'MyFont' } })。此方法适用于style.fontFamily: 'MyFont'和Text。HTMLText
Update cost comparison
更新开销对比
| Update trigger | Text | BitmapText | HTMLText | SplitText | SplitBitmapText |
|---|---|---|---|---|---|
Changing | High | Very low | High | Very high (N text re-renders) | Low (N quad repositions) |
Changing | High | Medium | High | Very high | Medium |
Moving ( | Free | Free | Free | Free | Free |
| Rotating / scaling | Free | Free | Free | Free | Free |
"Free" = normal Container transform cost. "High" = new canvas draw + GPU upload. "Very low" = quad reposition only. Update strings that change per-frame only on or .
BitmapTextSplitBitmapText| 更新触发条件 | Text | BitmapText | HTMLText | SplitText | SplitBitmapText |
|---|---|---|---|---|---|
修改 | 高开销 | 极低开销 | 高开销 | 极高开销(N次Text重渲染) | 低开销(N次四边形重定位) |
修改 | 高开销 | 中等开销 | 高开销 | 极高开销 | 中等开销 |
移动( | 无额外开销 | 无额外开销 | 无额外开销 | 无额外开销 | 无额外开销 |
| 旋转/缩放 | 无额外开销 | 无额外开销 | 无额外开销 | 无额外开销 | 无额外开销 |
"无额外开销" = 常规Container变换开销。"高开销" = 新画布绘制 + GPU上传。"极低开销" = 仅四边形重定位。仅在或上更新逐帧变化的字符串。
BitmapTextSplitBitmapTextQuick concepts
核心概念速览
- Options-object constructors. Every v8 text class uses . The v7
new Text({ text, style, ... })form is removed.(string, style) - .
tagStylesandTextsupport per-tag styling viaHTMLText. Tags are only parsed whenstyle.tagStyleshas entries; otherwisetagStylesis treated literally.< - . Pre-generates an atlas before you create any
BitmapFont.install. Without install, the firstBitmapTextwith a newBitmapTextgenerates the atlas lazily.fontFamily - MSDF fonts. Multi-channel Signed Distance Field fonts stay sharp at any size. Generate with external tools (e.g., msdf-bmfont), load via . Requires
Assets.load('font.fnt')in custom builds.import 'pixi.js/text-bitmap'
- 选项对象构造函数:v8版本的所有文本类均使用形式。v7版本的
new Text({ text, style, ... })形式已被移除。(string, style) - :
tagStyles和Text支持通过HTMLText实现按标签设置样式。仅当style.tagStyles有配置项时才会解析标签;否则tagStyles会被视为字面量。< - :在创建任何
BitmapFont.install之前预先生成图集。若不执行install操作,首次使用新BitmapText创建fontFamily时会延迟生成图集。BitmapText - MSDF字体:多通道有符号距离场字体在任何尺寸下都能保持清晰。需通过外部工具(如msdf-bmfont)生成,再通过加载。自定义构建中需引入
Assets.load('font.fnt')。import 'pixi.js/text-bitmap'
Common Mistakes
常见错误
[HIGH] Updating Text.text
every frame
Text.text[高风险] 逐帧更新Text.text
Text.textWrong:
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 update re-rasterizes the whole string. Use for any value that changes per-frame.
TextBitmapText错误示例:
ts
app.ticker.add(() => {
scoreText.text = `Score: ${score}`;
});正确示例:
ts
const scoreText = new BitmapText({ text: "Score: 0", style });
app.ticker.add(() => {
scoreText.text = `Score: ${score}`;
});每次更新都会重新光栅化整个字符串。所有逐帧变化的值都应使用。
TextBitmapText[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 form. All text classes use options objects.
(string, style)错误示例:
ts
const text = new Text("Hello", { fontSize: 24 });正确示例:
ts
const text = new Text({ text: "Hello", style: { fontSize: 24 } });v8版本已移除形式。所有文本类均使用选项对象。
(string, style)[HIGH] Not importing pixi.js/text-bitmap
in custom builds
pixi.js/text-bitmap[高风险] 自定义构建中未引入pixi.js/text-bitmap
pixi.js/text-bitmapUnder or aggressive tree-shaking, silently returns raw data unless you add . The standard bundle includes the extension.
skipExtensionImports: trueAssets.load('font.fnt')import 'pixi.js/text-bitmap'import { ... } from 'pixi.js'当开启或激进摇树优化时,会静默返回原始数据,除非你添加。标准的包已包含该扩展。
skipExtensionImports: trueAssets.load('font.fnt')import 'pixi.js/text-bitmap'import { ... } from 'pixi.js'[MEDIUM] Adding children to a text instance
[中风险] 向文本实例添加子元素
Every text class sets . Wrap in a to group text with other content.
allowChildren = falseContainer所有文本类都设置了。若要将文本与其他内容分组,请将其包裹在中。
allowChildren = falseContainer