pixijs-application
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseApplicationstageapp.init()autoDetectRendererApplicationstageapp.init()autoDetectRendererQuick Start
快速开始
ts
import { Application } from "pixi.js";
const app = new Application();
await app.init({
resizeTo: window,
background: "#1099bb",
antialias: true,
preference: "webgl",
autoDensity: true,
resolution: window.devicePixelRatio,
});
document.body.appendChild(app.canvas);Related skills: (renderers, render pipeline), (render loop detail), (working with ), (non-browser setups).
pixijs-core-conceptspixijs-tickerpixijs-scene-containerapp.stagepixijs-environmentsts
import { Application } from "pixi.js";
const app = new Application();
await app.init({
resizeTo: window,
background: "#1099bb",
antialias: true,
preference: "webgl",
autoDensity: true,
resolution: window.devicePixelRatio,
});
document.body.appendChild(app.canvas);相关技能: (渲染器、渲染管线)、(渲染循环细节)、( 相关操作)、(非浏览器环境配置)。
pixijs-core-conceptspixijs-tickerpixijs-scene-containerapp.stagepixijs-environmentsCore Patterns
核心模式
Lifecycle: construct, init, render, destroy
生命周期:构造、初始化、渲染、销毁
ts
import { Application } from "pixi.js";
const app = new Application();
await app.init({ width: 800, height: 600 });
document.body.appendChild(app.canvas);
// ... run scene, ticker drives app.render() automatically ...
app.destroy(
{ removeView: true, releaseGlobalResources: true },
{ children: true, texture: true, textureSource: true },
);- allocates the instance but creates nothing. Options passed here are ignored with a v8 deprecation warning.
new Application() - is async. It builds the renderer, wires up plugins, and must complete before you can use
app.init(options),app.canvas, orapp.renderer.app.screen - The TickerPlugin calls every frame once init resolves (unless
app.render()).autoStart: false - — the first argument forwards to
app.destroy(rendererDestroyOptions, stageDestroyOptions). Passrenderer.destroy()ortrueto remove the canvas from the DOM. Add{ removeView: true }to drain global pools (batches, texture caches) when tearing down and re-creating an app in the same tab; omitting it is the usual cause of flickering and stale textures after a re-init (seereleaseGlobalResources: true).pixijs-performance
ts
import { Application } from "pixi.js";
const app = new Application();
await app.init({ width: 800, height: 600 });
document.body.appendChild(app.canvas);
// ... 运行场景,ticker 会自动驱动 app.render() ...
app.destroy(
{ removeView: true, releaseGlobalResources: true },
{ children: true, texture: true, textureSource: true },
);- 仅分配实例但不创建任何资源。在此处传入的配置会被忽略并触发v8版本的弃用警告。
new Application() - 是异步方法。它会构建渲染器、关联插件,只有当该方法执行完成后,你才能使用
app.init(options)、app.canvas或app.renderer。app.screen - 一旦初始化完成,TickerPlugin 会在每一帧调用 (除非设置
app.render())。autoStart: false - — 第一个参数会传递给
app.destroy(rendererDestroyOptions, stageDestroyOptions)。传入renderer.destroy()或true可将画布从DOM中移除。添加{ removeView: true }可在销毁并重新创建应用时清空全局池(批次、纹理缓存);如果省略该参数,通常会导致重新初始化后出现闪烁和纹理失效的问题(详见releaseGlobalResources: true)。pixijs-performance
Key init options
核心初始化选项
ts
await app.init({
width: 800,
height: 600,
background: 0x1099bb,
backgroundAlpha: 1,
antialias: true,
resolution: window.devicePixelRatio,
autoDensity: true,
preference: "webgpu",
autoStart: true,
sharedTicker: false,
resizeTo: window,
canvas: document.querySelector("#game-canvas") as HTMLCanvasElement,
});For every option — view/canvas, background, renderer preference (including the array form), ticker, resize, culler, events, accessibility, WebGL/WebGPU context flags, Graphics bezier smoothness, GC, and per-renderer overrides ( / / ) — see references/application-options.md.
webglwebgpucanvasOptionsts
await app.init({
width: 800,
height: 600,
background: 0x1099bb,
backgroundAlpha: 1,
antialias: true,
resolution: window.devicePixelRatio,
autoDensity: true,
preference: "webgpu",
autoStart: true,
sharedTicker: false,
resizeTo: window,
canvas: document.querySelector("#game-canvas") as HTMLCanvasElement,
});关于所有选项——视图/画布、背景、渲染器偏好设置(包括数组形式)、ticker、resize、culler、事件、无障碍访问、WebGL/WebGPU上下文标志、Graphics贝塞尔曲线平滑度、GC以及各渲染器的覆盖配置( / / ),请查看 references/application-options.md。
webglwebgpucanvasOptionsApplication properties
Application 属性
ts
app.stage; // root Container; add all display objects here
app.renderer; // the WebGL/WebGPU/Canvas renderer instance
app.canvas; // the HTMLCanvasElement (insert it into the DOM yourself)
app.screen; // Rectangle describing the visible area in CSS pixels
app.domContainerRoot; // HTMLDivElement that holds DOMContainer overlaysapp.stageContainerpixijs-scene-containerpixijs-core-conceptspixijs-custom-renderingapp.domContainerRoot<div>DOMContainerapp.canvaspixijs-scene-dom-containerts
app.stage; // 根容器;所有显示对象都需要添加到这里
app.renderer; // WebGL/WebGPU/Canvas 渲染器实例
app.canvas; // HTMLCanvasElement(需要你自行插入到DOM中)
app.screen; // 描述CSS像素可见区域的Rectangle对象
app.domContainerRoot; // 用于承载DOMContainer覆盖层的HTMLDivElementapp.stageContainerpixijs-scene-containerpixijs-core-conceptspixijs-custom-renderingapp.domContainerRootDOMContainer<div>app.canvaspixijs-scene-dom-containerResizePlugin
ResizePlugin
Set at init (or reassign later) to have the plugin listen for the event and call with the target element's client size. Combine with and for high-DPI output.
resizeToapp.resizeToresizerenderer.resize()autoDensity: trueresolution: window.devicePixelRatiots
await app.init({ resizeTo: window });
app.resizeTo = document.querySelector("#game-container") as HTMLElement;
app.resize(); // immediate resize to the target's current size
app.queueResize(); // defer the resize to the next animation frame
app.cancelResize(); // drop a pending queueResizeThe plugin keeps the canvas matched to the target. and update in response; read them after the resize to place UI.
app.screenapp.canvas.width/height- — immediate synchronous resize.
app.resize() - — coalesces rapid calls by deferring to the next frame; internally used by the
app.queueResize()listener to avoid redundant work.window.resize - — cancels a queued resize. Call this before tearing down your own layout code that triggered
app.cancelResize().queueResize
在初始化时设置 (或之后重新赋值 ),插件会监听 事件,并根据目标元素的客户端尺寸调用 。结合 和 可实现高DPI输出。
resizeToapp.resizeToresizerenderer.resize()autoDensity: trueresolution: window.devicePixelRatiots
await app.init({ resizeTo: window });
app.resizeTo = document.querySelector("#game-container") as HTMLElement;
app.resize(); // 立即同步调整尺寸
app.queueResize(); // 将尺寸调整延迟到下一动画帧
app.cancelResize(); // 取消已排队的尺寸调整该插件会保持画布与目标元素尺寸匹配。 和 会随之更新;调整尺寸完成后可读取这些属性来布局UI。
app.screenapp.canvas.width/height- — 立即同步调整尺寸。
app.resize() - — 通过延迟到下一帧来合并频繁的调用;
app.queueResize()监听器内部会使用该方法来避免冗余操作。window.resize - — 取消已排队的尺寸调整。在销毁触发
app.cancelResize()的自定义布局代码前调用该方法。queueResize
Ticker basics
Ticker 基础
The TickerPlugin creates and registers on it at . Control the loop with / and add callbacks with / :
app.tickerapp.render()UPDATE_PRIORITY.LOWapp.start()app.stop()app.ticker.addapp.ticker.addOncets
app.ticker.add((ticker) => {
sprite.rotation += 0.01 * ticker.deltaTime;
});
app.ticker.addOnce(() => {
console.log("runs once on the next frame, then removes itself");
});
app.stop(); // pause the render loop (e.g. tab hidden)
app.start(); // resumeThe callback receives the instance; read for a frame-rate-independent multiplier (~1.0 at 60fps), for real milliseconds, or for the current frame rate. See for priorities, FPS capping, , shared vs private tickers, and the v8 callback signature change.
Tickerticker.deltaTimeticker.deltaMSticker.FPSpixijs-tickeronRenderTickerPlugin 会创建 ,并将 注册到 优先级。可通过 / 控制循环,通过 / 添加回调:
app.tickerapp.render()UPDATE_PRIORITY.LOWapp.start()app.stop()app.ticker.addapp.ticker.addOncets
app.ticker.add((ticker) => {
sprite.rotation += 0.01 * ticker.deltaTime;
});
app.ticker.addOnce(() => {
console.log("在下一帧运行一次,然后自动移除");
});
app.stop(); // 暂停渲染循环(例如标签页隐藏时)
app.start(); // 恢复渲染循环回调函数会接收 实例;可读取 获取帧率无关的乘数(60fps时约为1.0), 获取实际毫秒数,或 获取当前帧率。关于优先级、FPS限制、、共享与私有ticker以及v8版本回调签名的变化,请查看 。
Tickerticker.deltaTimeticker.deltaMSticker.FPSonRenderpixijs-tickerManual render loop
手动渲染循环
ts
await app.init({ autoStart: false, width: 800, height: 600 });
document.body.appendChild(app.canvas);
function frame() {
updateScene();
app.render();
requestAnimationFrame(frame);
}
frame();autoStart: falseapp.render()app.renderer.render({ container: app.stage })app.ticker.update()app.render()ts
await app.init({ autoStart: false, width: 800, height: 600 });
document.body.appendChild(app.canvas);
function frame() {
updateScene();
app.render();
requestAnimationFrame(frame);
}
frame();autoStart: falseapp.render()app.renderer.render({ container: app.stage })app.render()app.ticker.update()CullerPlugin (opt-in)
CullerPlugin(可选启用)
The CullerPlugin skips rendering containers that fall outside . It isn't registered by default; add it before creating your app:
app.renderer.screents
import {
Application,
Container,
Sprite,
extensions,
CullerPlugin,
Rectangle,
} from "pixi.js";
extensions.add(CullerPlugin);
const app = new Application();
await app.init({ width: 800, height: 600 });
const world = new Container();
world.cullable = true; // this container is culled when its bounds leave the screen
world.cullableChildren = true; // default; set `false` to skip recursing into children
const tile = Sprite.from("tile.png");
tile.cullable = true;
world.addChild(tile);
app.stage.addChild(world);Containers are not culled unless is set. Override the default bounds check with when child bounds are expensive to compute. The plugin wraps so runs before every frame. See for when culling pays off.
cullablecontainer.cullArea = new Rectangle(x, y, w, h)app.render()Culler.shared.cull(app.stage, app.renderer.screen)pixijs-performanceCullerPlugin 会跳过渲染那些超出 范围的容器。它默认未注册;需在创建应用前添加:
app.renderer.screents
import {
Application,
Container,
Sprite,
extensions,
CullerPlugin,
Rectangle,
} from "pixi.js";
extensions.add(CullerPlugin);
const app = new Application();
await app.init({ width: 800, height: 600 });
const world = new Container();
world.cullable = true; // 当该容器的边界超出屏幕时会被剔除
world.cullableChildren = true; // 默认值;设置为`false`可跳过对子容器的递归检查
const tile = Sprite.from("tile.png");
tile.cullable = true;
world.addChild(tile);
app.stage.addChild(world);只有设置了 的容器才会被剔除。当子容器的边界计算成本较高时,可通过 覆盖默认的边界检查。该插件会封装 ,因此每一帧渲染前都会执行 。关于剔除的适用场景,请查看 。
cullablecontainer.cullArea = new Rectangle(x, y, w, h)app.render()Culler.shared.cull(app.stage, app.renderer.screen)pixijs-performanceCustom Application plugins
自定义Application插件
Extend by registering a class with , , and . Both methods are called with bound to the Application instance, so and are available.
Applicationstatic initstatic destroystatic extension = ExtensionType.Applicationthisthis.rendererthis.stagets
import {
Application,
ExtensionType,
extensions,
type ApplicationOptions,
} from "pixi.js";
class FpsOverlay {
public static extension = ExtensionType.Application;
public static init(this: Application, options: Partial<ApplicationOptions>) {
// runs inside app.init() after the renderer is created
// attach props/methods to `this` to expose them on the app
}
public static destroy(this: Application) {
// runs inside app.destroy() — tear down anything you attached
}
}
extensions.add(FpsOverlay);Plugins initialize in registration order and destroy in reverse. To add typed options for your plugin, extend :
PixiMixins.ApplicationOptionsts
declare global {
namespace PixiMixins {
interface ApplicationOptions {
fpsOverlay?: { visible?: boolean };
}
}
}
await app.init({ fpsOverlay: { visible: true } });The built-in , , and opt-in all use this same contract. If you set , register the built-ins you need yourself ().
ResizePluginTickerPluginCullerPluginskipExtensionImports: trueextensions.add(ResizePlugin, TickerPlugin)通过注册一个包含 、 和 的类来扩展 。这两个方法的 会绑定到Application实例,因此可以访问 和 。
static initstatic destroystatic extension = ExtensionType.ApplicationApplicationthisthis.rendererthis.stagets
import {
Application,
ExtensionType,
extensions,
type ApplicationOptions,
} from "pixi.js";
class FpsOverlay {
public static extension = ExtensionType.Application;
public static init(this: Application, options: Partial<ApplicationOptions>) {
// 在app.init()内部、渲染器创建后执行
// 可将属性/方法绑定到`this`,从而在应用实例上暴露
}
public static destroy(this: Application) {
// 在app.destroy()内部执行 — 清理你绑定的所有资源
}
}
extensions.add(FpsOverlay);插件会按照注册顺序初始化,并按照相反顺序销毁。若要为你的插件添加类型化选项,可扩展 :
PixiMixins.ApplicationOptionsts
declare global {
namespace PixiMixins {
interface ApplicationOptions {
fpsOverlay?: { visible?: boolean };
}
}
}
await app.init({ fpsOverlay: { visible: true } });内置的 、 和可选的 都遵循这一约定。如果设置了 ,你需要自行注册所需的内置插件()。
ResizePluginTickerPluginCullerPluginskipExtensionImports: trueextensions.add(ResizePlugin, TickerPlugin)Common Mistakes
常见错误
[CRITICAL] Passing options to the constructor
[严重] 向构造函数传递配置选项
Wrong:
ts
const app = new Application({ width: 800, height: 600 });
document.body.appendChild(app.canvas);Correct:
ts
const app = new Application();
await app.init({ width: 800, height: 600 });
document.body.appendChild(app.canvas);In v8 the constructor takes no arguments. Options passed there are ignored and log a deprecation warning; the renderer is only created inside the async call.
Applicationinit()错误写法:
ts
const app = new Application({ width: 800, height: 600 });
document.body.appendChild(app.canvas);正确写法:
ts
const app = new Application();
await app.init({ width: 800, height: 600 });
document.body.appendChild(app.canvas);在v8版本中, 构造函数不接收任何参数。在此处传入的配置会被忽略并触发弃用警告;渲染器仅会在异步的 调用中创建。
Applicationinit()[HIGH] Using app.view instead of app.canvas
[高风险] 使用app.view而非app.canvas
Wrong:
ts
document.body.appendChild(app.view);Correct:
ts
document.body.appendChild(app.canvas);app.viewapp.canvas错误写法:
ts
document.body.appendChild(app.view);正确写法:
ts
document.body.appendChild(app.canvas);在v8版本中, 已重命名为 。旧的getter仍可使用但会触发弃用警告。
app.viewapp.canvas[MEDIUM] Touching app.canvas or app.renderer before init resolves
[中风险] 在初始化完成前访问app.canvas或app.renderer
Wrong:
ts
const app = new Application();
document.body.appendChild(app.canvas);
app.init({ width: 800, height: 600 });Correct:
ts
const app = new Application();
await app.init({ width: 800, height: 600 });
document.body.appendChild(app.canvas);app.rendererapp.canvasapp.screeninit()undefined错误写法:
ts
const app = new Application();
document.body.appendChild(app.canvas);
app.init({ width: 800, height: 600 });正确写法:
ts
const app = new Application();
await app.init({ width: 800, height: 600 });
document.body.appendChild(app.canvas);只有当 承诺完成后,、 和 才会被赋值。提前访问会返回 。
init()app.rendererapp.canvasapp.screenundefined