PixiJS v8: async init and WebGPU
PixiJS is the library I reach for when a page needs to draw a lot of things fast: thousands of sprites, a game, a data visualisation that's too busy for SVG.
PixiJS is the library I reach for when a page needs to draw a lot of things fast: thousands of sprites, a game, a data visualisation that’s too busy for SVG. Version 8 came out in March, and it’s the biggest change in years. It adds a WebGPU renderer alongside WebGL, and to make that possible, a few habits from v7 have to change.
Here’s what changes when you upgrade, starting with the one that breaks every hello-world.
Creating the app is now async
In v7 you passed options to the constructor and used the app straight away. In v8 you create it empty and then await its init:
import { Application, Assets, Sprite } from 'pixi.js';
const app = new Application();
await app.init({ background: '#1099bb', resizeTo: window });
document.body.appendChild(app.canvas);
const texture = await Assets.load('/bunny.png');
const bunny = new Sprite(texture);
bunny.anchor.set(0.5);
bunny.position.set(app.screen.width / 2, app.screen.height / 2);
app.stage.addChild(bunny);
app.ticker.add((ticker) => {
bunny.rotation += 0.05 * ticker.deltaTime;
});
It’s async because choosing a renderer is async. Asking the browser for a WebGPU device returns a promise, and the renderer code itself is loaded with a dynamic import, so only the one you use ends up downloaded.
Three smaller changes hide in that snippet:
- app.canvas, not app.view. view still works but is deprecated.
- The ticker callback gets the Ticker, not a delta number. Use ticker.deltaTime.
- Passing options to the constructor still works with a deprecation warning, but init is the way forward.
In a framework, the async init needs care. A React component that mounts, unmounts and mounts again (as Strict Mode does in development) can start a second init before the first one finishes. Keep a flag, and only destroy an app whose init has completed:
useEffect(() => {
const app = new Application();
let ready = false;
let cancelled = false;
app.init({ resizeTo: hostRef.current }).then(() => {
if (cancelled) {
app.destroy(true);
return;
}
ready = true;
hostRef.current.appendChild(app.canvas);
// build the scene here
});
return () => {
cancelled = true;
if (ready) app.destroy(true);
};
}, []);
WebGPU is opt-in
You might expect v8 to use WebGPU wherever it’s available. In 8.1, the automatic choice still prefers WebGL, because it’s the most tested path. To try WebGPU, ask for it:
await app.init({ preference: 'webgpu' });
If the browser doesn’t support WebGPU, Pixi falls back to WebGL, so it’s safe to set. For most scenes the two look identical, and the API you write against is the same. The difference shows up with very large numbers of objects, and in custom shaders, which now need WGSL for WebGPU as well as GLSL for WebGL.
Graphics: shape first, then style
The Graphics API was rewritten, and this is where most upgrade time goes. In v7, you set a style and then drew:
// v7
const g = new Graphics();
g.beginFill(0xde3249);
g.drawRect(0, 0, 100, 60);
g.endFill();
In v8, you draw a shape and then fill or stroke it:
// v8
const g = new Graphics()
.rect(0, 0, 100, 60)
.fill(0xde3249)
.circle(160, 30, 30)
.fill({ color: 0x1099bb, alpha: 0.5 })
.stroke({ width: 2, color: 0xffffff });
The old methods (beginFill, drawRect, lineStyle and friends) still exist, marked deprecated, so an upgraded app keeps running while you convert. The draw prefix is gone from all the shape methods: rect, circle, ellipse, poly, roundRect.
There’s a useful new piece too: GraphicsContext. Turning drawing commands into GPU geometry is the expensive part, and a context lets many Graphics objects share that work:
import { Graphics, GraphicsContext } from 'pixi.js';
const star = new GraphicsContext().star(0, 0, 5, 20).fill(0xffd700);
for (let i = 0; i < 500; i++) {
const g = new Graphics(star); // shares the geometry
g.position.set(Math.random() * 800, Math.random() * 600);
app.stage.addChild(g);
}
Five hundred stars, one set of geometry.
One package
v7 was split across many @pixi/* packages, and mixing their versions caused strange errors. In v8 everything comes from pixi.js. If your package.json lists any @pixi/ packages from v7, remove them and import from pixi.js instead. Leftover duplicates are the first thing to check when something behaves oddly after the upgrade.
Text takes an options object
The same “options object” style shows up across the API:
const label = new Text({
text: 'Score: 0',
style: { fill: 0xffffff, fontSize: 24 },
});
The old new Text(‘Score: 0’, style) form still works, but the object form is what the docs and types lead with.
Is it worth upgrading?
For a new project, yes, without question. For an existing v7 app, the async init is a small change, and the deprecated Graphics methods give you time. Budget most of the effort for custom shaders, filters and any plugins that hooked into v7 internals. And try preference: ‘webgpu’ on a copy of your heaviest scene: it’s one line to find out whether it helps you.