pixijs-scene-dom-container
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDOMContainerpixi.jsDOMPipeimport 'pixi.js/dom'is marked EXPERIMENTAL in PixiJS v8. The API may change between minor releases.DOMContainer
Assumes familiarity with . extends , so it's a leaf: do not nest PixiJS children inside it. Nest DOM content inside the HTML element itself, or wrap multiple instances in a . Not available in Web Workers; a worker has no DOM to overlay.
pixijs-scene-core-conceptsDOMContainerViewContainerDOMContainerContainerDOMContainerpixi.jsDOMPipeimport 'pixi.js/dom'在PixiJS v8中被标记为EXPERIMENTAL(实验性)。API可能在小版本更新中发生变化。DOMContainer
本文假设你已熟悉 。 继承自 ,因此它是一个叶子节点:不要在其中嵌套PixiJS子元素。请将DOM内容嵌套在HTML元素本身内部,或者将多个实例包装在一个中。Web Workers环境下不可用,因为Worker没有可叠加的DOM。
pixijs-scene-core-conceptsDOMContainerViewContainerDOMContainerContainerQuick Start
快速开始
ts
import "pixi.js/dom";
const input = document.createElement("input");
input.type = "text";
input.placeholder = "Enter name...";
const dom = new DOMContainer({
element: input,
anchor: 0.5,
});
dom.position.set(app.screen.width / 2, app.screen.height / 2);
app.stage.addChild(dom);Related skills: (scene graph basics), (wrap multiple DOM overlays), (pointer handling on canvas vs DOM), (screen-reader overlays).
pixijs-scene-core-conceptspixijs-scene-containerpixijs-eventspixijs-accessibilityts
import "pixi.js/dom";
const input = document.createElement("input");
input.type = "text";
input.placeholder = "Enter name...";
const dom = new DOMContainer({
element: input,
anchor: 0.5,
});
dom.position.set(app.screen.width / 2, app.screen.height / 2);
app.stage.addChild(dom);相关技能: (场景图基础)、(包装多个DOM叠加层)、(画布与DOM的指针处理)、(屏幕阅读器叠加层)。
pixijs-scene-core-conceptspixijs-scene-containerpixijs-eventspixijs-accessibilityConstructor options
构造函数选项
All options (, , , , , , etc.) are also valid here — see .
ContainerpositionscaletintlabelfilterszIndexskills/pixijs-scene-core-concepts/references/constructor-options.mdLeaf-specific options added by :
DOMContainerOptions| Option | Type | Default | Description |
|---|---|---|---|
| | | The HTML element the container drives. Any element is valid: |
| | | Origin of the element relative to its own dimensions. |
tintfiltersmaskblendModeContainer所有的选项(、、、、、等)在此处同样有效——详见。
ContainerpositionscaletintlabelfilterszIndexskills/pixijs-scene-core-concepts/references/constructor-options.mdDOMContainerOptions| 选项 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| | | 容器所控制的HTML元素。任何元素都有效: |
| | | 元素相对于自身尺寸的原点。 |
tintfiltersmaskblendModeContainerCore Patterns
核心模式
Setup and the side-effect import
设置与副作用导入
ts
import "pixi.js/dom";
import { DOMContainer } from "pixi.js";Or use the combined import that registers the pipe and re-exports the class:
ts
import { DOMContainer } from "pixi.js/dom";The default browser bundle already imports for you via , so works out of the box in a typical browser app. You only need the explicit line when you set (custom builds) or when running under a non-browser bundle.
pixi.jspixi.js/dombrowserAll.tsDOMContainerimport 'pixi.js/dom'skipExtensionImports: truets
import "pixi.js/dom";
import { DOMContainer } from "pixi.js";或者使用组合导入,自动注册管道并导出类:
ts
import { DOMContainer } from "pixi.js/dom";默认的浏览器包已通过自动导入,因此在典型的浏览器应用中可直接使用。只有当你设置(自定义构建)或在非浏览器包环境下运行时,才需要显式添加语句。
pixi.jsbrowserAll.tspixi.js/domDOMContainerskipExtensionImports: trueimport 'pixi.js/dom'Transforms, anchor, and alpha
变换、锚点与透明度
ts
const dom = new DOMContainer({
element: document.createElement("div"),
anchor: 0.5,
});
dom.position.set(400, 300);
dom.scale.set(1.5);
dom.rotation = Math.PI / 8;
dom.alpha = 0.5;Transforms on the propagate to the element as CSS . shifts the element's origin relative to its own dimensions: is top-left, centers, is bottom-right. A single number sets both axes; an object sets each independently. (including inherited parent alpha) is written to the element's every frame.
DOMContainertransformanchor00.51{ x, y }alphastyle.opacityIf no is provided, a is created by default.
element<div>ts
const dom = new DOMContainer({
element: document.createElement("div"),
anchor: 0.5,
});
dom.position.set(400, 300);
dom.scale.set(1.5);
dom.rotation = Math.PI / 8;
dom.alpha = 0.5;DOMContainertransformanchor00.51{ x, y }alphastyle.opacity如果未提供选项,会自动创建一个元素。
element<div>Styling the element directly
直接为元素设置样式
ts
const panel = document.createElement("div");
panel.innerHTML = "<h2>Score</h2><p>1500</p>";
panel.style.color = "white";
panel.style.fontFamily = "Arial";
panel.style.pointerEvents = "none";
const dom = new DOMContainer({ element: panel });
dom.position.set(50, 50);
app.stage.addChild(dom);PixiJS does not interfere with CSS styling on the element. The shared root is set to , and each attached element defaults to . Override to on purely decorative overlays so the canvas still receives clicks underneath them.
<div>pointer-events: nonepointer-events: autononets
const panel = document.createElement("div");
panel.innerHTML = "<h2>Score</h2><p>1500</p>";
panel.style.color = "white";
panel.style.fontFamily = "Arial";
panel.style.pointerEvents = "none";
const dom = new DOMContainer({ element: panel });
dom.position.set(50, 50);
app.stage.addChild(dom);PixiJS不会干扰元素的CSS样式。共享根节点默认设置为,每个附加的元素默认设置为。对于纯装饰性的叠加层,可将其设置为,这样画布仍能接收到下方的点击事件。
<div>pointer-events: nonepointer-events: autononeVisibility and cleanup
可见性与清理
ts
dom.visible = false;
dom.visible = true;
dom.destroy();Setting or removing the from the scene graph detaches the element from the DOM. Restoring visibility re-attaches it. removes the element from its parent node and nulls internal references; the HTML element itself is preserved, so you can re-attach it elsewhere:
visible = falseDOMContainerdestroy()ts
const element = dom.element;
dom.destroy();
document.body.appendChild(element);ts
dom.visible = false;
dom.visible = true;
dom.destroy();设置或从场景图中移除会将元素从DOM中分离。恢复可见性会重新附加元素。方法会将元素从其父节点移除并清空内部引用;HTML元素本身会被保留,因此你可以将其重新附加到其他位置:
visible = falseDOMContainerdestroy()ts
const element = dom.element;
dom.destroy();
document.body.appendChild(element);The DOM container root
DOM容器根节点
The DOMPipe uses a shared root with that hosts every attached element. It's exposed as (an ). All elements render above canvas content; you can't interleave DOM elements between PixiJS draw calls.
<div>z-index: 1000app.domContainerRootHTMLDivElementDOMContainerOn the first render that has an attached , the pipe automatically appends the root to the canvas's parent node. Append it yourself next to if you need explicit, stable placement in the DOM tree (for example, when the canvas shares a wrapper with other layered content):
DOMContainerapp.canvasts
document.body.appendChild(app.canvas);
document.body.appendChild(app.domContainerRoot);The root uses absolute positioning and its transform is recomputed from the canvas via a , so CSS-scaled canvases stay aligned without extra work.
getBoundingClientRect()ResizeObserverDOMPipe使用一个共享的根节点()来托管所有附加的元素。它可以通过(一个)访问。所有元素都会渲染在画布内容上方;你无法在PixiJS绘制调用之间插入DOM元素。
<div>z-index: 1000app.domContainerRootHTMLDivElementDOMContainer当首次渲染附加了的内容时,管道会自动将根节点附加到画布的父节点。如果你需要在DOM树中明确、稳定的位置(例如当画布与其他分层内容共享一个容器时),可以手动将其附加到旁边:
DOMContainerapp.canvasts
document.body.appendChild(app.canvas);
document.body.appendChild(app.domContainerRoot);根节点使用绝对定位,其变换会通过从画布的重新计算,因此经过CSS缩放的画布无需额外操作即可保持对齐。
ResizeObservergetBoundingClientRect()Common Mistakes
常见错误
[MEDIUM] Missing the pixi.js/dom import in custom builds
[MEDIUM] 自定义构建中缺少pixi.js/dom导入
The default browser bundle auto-registers , so most apps do not need an explicit import. It is only required when you opt out of auto-imports:
DOMPipets
await app.init({ skipExtensionImports: true });
// Now you must add this yourself:
import "pixi.js/dom";Without registration under a custom build, is still importable but the renderer has no pipe to process it; elements never synchronize with the scene graph and never appear.
DOMContainer默认浏览器包会自动注册,因此大多数应用无需显式导入。只有当你禁用自动导入时才需要添加:
DOMPipets
await app.init({ skipExtensionImports: true });
// 现在你必须手动添加:
import "pixi.js/dom";在自定义构建环境下,如果未注册,仍然可以导入,但渲染器没有处理它的管道;元素将无法与场景图同步,也不会显示出来。
DOMPipeDOMContainer[MEDIUM] Expecting filters, masks, or blend modes to affect DOM elements
[MEDIUM] 认为滤镜、遮罩或混合模式会影响DOM元素
Wrong:
ts
const dom = new DOMContainer();
dom.filters = [new BlurFilter()];Correct:
ts
dom.element.style.filter = "blur(4px)";DOM elements are HTML overlays positioned via CSS transforms; they exist outside the WebGL/WebGPU pipeline. PixiJS filters, masks, and blend modes have no effect on them. Use CSS filters and CSS on the element directly.
mix-blend-mode错误示例:
ts
const dom = new DOMContainer();
dom.filters = [new BlurFilter()];正确示例:
ts
dom.element.style.filter = "blur(4px)";DOM元素是通过CSS变换定位的HTML叠加层;它们位于WebGL/WebGPU渲染管线之外。PixiJS的滤镜、遮罩和混合模式对它们没有效果。请直接在元素上使用CSS滤镜和CSS 。
mix-blend-mode[MEDIUM] Do not nest children inside a DOMContainer
[MEDIUM] 不要在DOMContainer中嵌套子元素
Wrong:
ts
const dom = new DOMContainer();
dom.addChild(new Sprite(texture));Correct:
ts
const group = new Container();
group.addChild(dom, new Sprite(texture));DOMContainerViewContainerallowChildren = falseDOMContainerContainerelement.appendChild(...)错误示例:
ts
const dom = new DOMContainer();
dom.addChild(new Sprite(texture));正确示例:
ts
const group = new Container();
group.addChild(dom, new Sprite(texture));DOMContainerViewContainerallowChildren = falseDOMContainerContainerelement.appendChild(...)[LOW] Forgetting to set anchor for centered positioning
[LOW] 忘记设置锚点以实现居中定位
The default anchor is , placing the element's top-left corner at the container's position. For centering a UI element on its scene-graph position, set :
(0, 0)anchor: 0.5ts
const dom = new DOMContainer({ element: myElement, anchor: 0.5 });
dom.position.set(400, 300);默认锚点为,即元素的左上角会对齐容器的位置。如果要让UI元素相对于场景图位置居中,请设置:
(0, 0)anchor: 0.5ts
const dom = new DOMContainer({ element: myElement, anchor: 0.5 });
dom.position.set(400, 300);