Loading...
Loading...
Use this skill when rendering live HTML/DOM elements (or frozen snapshots of them) as PixiJS v8 textures via the EXPERIMENTAL HTML-in-Canvas browser APIs. Covers the pixi.js/html-source side-effect import, feature-detection with canvas.requestPaint, HTMLSource for a live, repainting element kept interactive in the browser (autoLayout/autoUpdate/autoRequestPaint, requestPaint, isReady, the direct-child-of-canvas + layoutsubtree requirement), ElementImageSource for an immutable captureElementImage() snapshot (autoClose, ready immediately), using the source on a Sprite/Texture/Mesh, fallback-only auto-detection via Texture.from at priority -10, and destroy/cleanup. Triggers on: HTMLSource, ElementImageSource, pixi.js/html-source, requestPaint, captureElementImage, ElementImage, layoutsubtree, autoRequestPaint, autoUpdate, autoClose, HTML in canvas, render DOM to texture, HTMLSourceOptions, ElementImageSourceOptions, HTMLSourceCanvas, experimental.
npx skill4agent add pixijs/pixijs-skills pixijs-html-sourceHTMLSourceElementImageSourceTextureSourceSpriteTextureMeshHTMLSourceElementImageSourceimport 'pixi.js/html-source'These sources rely on the experimental HTML-in-Canvas browser proposal and are marked EXPERIMENTAL in PixiJS v8. The browser API must be enabled or the texture uploader throws on first render; feature-detect withbefore relying on it. The API may change between minor releases.canvas.requestPaint
pixijs-scene-spriteSpriteTextureMeshimport "pixi.js/html-source";
import { Application, Sprite } from "pixi.js";
import { HTMLSource } from "pixi.js/html-source";
const app = new Application();
await app.init({ resizeTo: window });
document.body.appendChild(app.canvas);
// The element must be a direct child of the Pixi canvas.
const form = document.createElement("form");
form.innerHTML = '<input value="still editable" />';
app.canvas.appendChild(form);
// Render the live form as a sprite. It stays interactive in the browser.
const source = new HTMLSource({ resource: form });
const sprite = Sprite.from(source);
sprite.anchor.set(0.5);
sprite.position.set(app.screen.width / 2, app.screen.height / 2);
app.stage.addChild(sprite);pixijs-scene-spritepixijs-scene-meshPerspectiveMeshpixijs-scene-dom-containerpixijs-assetspixijs-environmentsTextureSourceTextureSourceOptionsresolutionscaleModeaddressModelabelresourceHTMLSourceOptions| Option | Type | Default | Description |
|---|---|---|---|
| | — | Required. The live DOM element to render. Must be a direct child of the owning canvas, or the constructor throws. |
| | — | The canvas that owns the element's layout subtree. Inferred from |
| | | Set the |
| | | Listen for the canvas |
| | | Request one initial paint after construction. Set |
ElementImageSourceOptions| Option | Type | Default | Description |
|---|---|---|---|
| | — | Required. A snapshot from |
| | | Call |
import "pixi.js/html-source";
import { HTMLSource, ElementImageSource } from "pixi.js/html-source";pixi.js/html-sourceextensions.add(...)HTMLSourceElementImageSource'html'pixi.js/html-sourcepixi.jspixi.js/html-sourceimport 'pixi.js/html-source'import type { HTMLSourceCanvas } from "pixi.js/html-source";
const canvas = app.canvas as HTMLSourceCanvas;
if (canvas.requestPaint) {
// HTML-in-Canvas is available.
}app.canvasHTMLSourceCanvasrequestPaintcaptureElementImagesource.requestPaint()falseconst form = document.createElement("form");
app.canvas.appendChild(form); // direct child of the canvas
const source = new HTMLSource({ resource: form });
const sprite = Sprite.from(source);<canvas>resource.parentElementcanvaslayoutsubtreepaintsource.isReadyfalsetrueresourceWidthresourceHeightoffsetWidthoffsetHeightconst source = new HTMLSource({ resource: clock, autoRequestPaint: false });
const sprite = Sprite.from(source);
app.ticker.add(() => {
clock.textContent = new Date().toLocaleTimeString();
source.requestPaint(); // re-snapshot the DOM this frame
});autoRequestPaint: falsesource.requestPaint()import { ElementImageSource } from "pixi.js/html-source";
import type { HTMLSourceCanvas } from "pixi.js/html-source";
const canvas = app.canvas as HTMLSourceCanvas;
const snapshot = canvas.captureElementImage!(element);
const source = new ElementImageSource({ resource: snapshot, autoClose: true });
const sprite = Sprite.from(source);captureElementImage()ElementImagepaintsnapshot.close()autoClose: truedestroy()TextureSourceSprite.from(source)Texture.from(source)import { Rectangle, Texture } from "pixi.js";
// A 64x64 slice of the rendered element.
const chunk = new Texture({
source,
frame: new Rectangle(0, 0, 64, 64),
});
// Mapped onto geometry (e.g. a perspective warp).
const mesh = new PerspectiveMesh({ texture: Texture.from(source) /* ... */ });// Resolves to an HTMLSource (element) or ElementImageSource (snapshot) only as a last resort.
const sprite = Sprite.from(elementAlreadyInTheCanvas);ElementImageTexture.fromSprite.from-10autoUpdateautoCloseimport { HTMLSource } from "pixi.js/html-source";
// ...but never importing the side effect, in a build that tree-shakes it awayimport "pixi.js/html-source";
import { HTMLSource } from "pixi.js/html-source";'html'const canvas = app.canvas as HTMLSourceCanvas;
if (!canvas.requestPaint) {
// Fall back to a static image, DOMContainer overlay, or a message.
}document.body.appendChild(form); // wrong parent
const source = new HTMLSource({ resource: form }); // throwsHTMLSourceapp.canvas.appendChild(form)canvaspaintautoUpdate: truesource.requestPaint()autoRequestPaint: falseconst source = new ElementImageSource({ resource: snapshot, autoClose: true });
const other = new ElementImageSource({ resource: snapshot }); // shares the snapshot
source.destroy(); // closes the snapshot — `other` is now a use-after-freeautoClose: trueautoClosesnapshot.close()source.destroy();HTMLSource.destroy()paintElementImageSource.destroy()autoClosesnapshot.close()