Loading...
Loading...
PixiJS v8 patterns for Starwards - Containers, Sprites, Graphics, Textures, ticker integration, event handling, and testing with Playwright data-id selectors
npx skill4agent add starwards/starwards starwards-pixijsApplicationimport { Application } from 'pixi.js';
const app = new Application();
await app.init({
width: 800,
height: 600,
backgroundColor: 0x1099bb,
});
document.body.appendChild(app.canvas);| Option | Type | Default | Description |
|---|---|---|---|
| | | Initial width |
| | | Initial height |
| | | Background color |
| | - | Enable anti-aliasing |
| | | Pixel resolution |
| | - | Auto-resize target |
| | | Renderer type |
import { Container } from 'pixi.js';
const container = new Container({
x: 100,
y: 100,
});
app.stage.addChild(container);setChildIndex()zIndexsortableChildren// Local to global
const globalPos = obj.toGlobal(new Point(0, 0));
// Global to local
const localPos = container.toLocal(new Point(100, 100));container.cullable = true; // Enable culling
container.cullableChildren = true; // Cull children recursively
container.cullArea = new Rectangle(0, 0, 400, 400); // Custom cull boundsimport { Sprite, Assets } from 'pixi.js';
const texture = await Assets.load('bunny.png');
const sprite = new Sprite(texture);
sprite.anchor.set(0.5); // Center anchor
sprite.x = 100;
sprite.y = 100;
sprite.tint = 0xff0000; // Red tint
sprite.alpha = 0.8;| Property | Description |
|---|---|
| The texture to display |
| Origin point (0-1 range) |
| Color tint |
| Blend mode for compositing |
| Size (scales texture) |
import { Graphics } from 'pixi.js';
// Draw shape, then fill/stroke
const graphics = new Graphics()
.rect(50, 50, 100, 100)
.fill(0xff0000)
.stroke({ width: 2, color: 'white' });
// Circle with fill and stroke
const circle = new Graphics()
.circle(100, 100, 50)
.fill({ color: 0x00ff00, alpha: 0.5 })
.stroke({ width: 3, color: 0x000000 });| v7 (OLD) | v8 (NEW) |
|---|---|
| |
| |
| |
| |
| |
| |
const lines = new Graphics()
.moveTo(0, 0)
.lineTo(100, 100)
.lineTo(200, 0)
.stroke({ width: 2, color: 0xff0000 });const rectWithHole = new Graphics()
.rect(0, 0, 100, 100)
.fill(0x00ff00)
.circle(50, 50, 20)
.cut(); // Creates holeimport { GraphicsContext, Graphics } from 'pixi.js';
const context = new GraphicsContext()
.rect(0, 0, 100, 100)
.fill(0xff0000);
const g1 = new Graphics(context);
const g2 = new Graphics(context); // Shares same dataimport { Text, TextStyle } from 'pixi.js';
const text = new Text({
text: 'Hello World',
style: {
fontFamily: 'Arial',
fontSize: 24,
fill: 0xffffff,
align: 'center',
},
});| Property | Description |
|---|---|
| Font name |
| Size in pixels |
| Fill color |
| Stroke settings |
| Text alignment |
| Enable word wrapping |
| Wrap width |
import { BitmapText } from 'pixi.js';
const bitmapText = new BitmapText({
text: 'Score: 1000',
style: { fontFamily: 'MyBitmapFont', fontSize: 32 },
});import { Assets, Sprite } from 'pixi.js';
// Single asset
const texture = await Assets.load('path/to/image.png');
const sprite = new Sprite(texture);
// Multiple assets
const textures = await Assets.load(['a.png', 'b.png']);
// With alias
await Assets.load({ alias: 'hero', src: 'images/hero.png' });
const heroTexture = Assets.get('hero');Assets.addBundle('game', [
{ alias: 'player', src: 'player.png' },
{ alias: 'enemy', src: 'enemy.png' },
]);
const assets = await Assets.loadBundle('game');const manifest = {
bundles: [
{
name: 'load-screen',
assets: [{ alias: 'bg', src: 'background.png' }],
},
{
name: 'game',
assets: [{ alias: 'hero', src: 'hero.png' }],
},
],
};
await Assets.init({ manifest });
await Assets.loadBundle('load-screen');// As texture
const svgTexture = await Assets.load('icon.svg');
const sprite = new Sprite(svgTexture);
// As Graphics (scalable)
const svgContext = await Assets.load({
src: 'icon.svg',
data: { parseAsGraphicsContext: true },
});
const graphics = new Graphics(svgContext);// Unload from cache and GPU
await Assets.unload('texture.png');
// Unload from GPU only (keep in memory)
texture.source.unload();
// Destroy texture
texture.destroy();import { Ticker, UPDATE_PRIORITY } from 'pixi.js';
// Using app ticker
app.ticker.add((ticker) => {
sprite.rotation += 0.1 * ticker.deltaTime;
});
// One-time callback
app.ticker.addOnce((ticker) => {
console.log('Called once');
});
// With priority (higher runs first)
app.ticker.add(updateFn, null, UPDATE_PRIORITY.HIGH);UPDATE_PRIORITY.HIGH = 50UPDATE_PRIORITY.NORMAL = 0UPDATE_PRIORITY.LOW = -50app.ticker.maxFPS = 60; // Cap framerate
app.ticker.minFPS = 30; // Clamp deltaTime| Property | Description |
|---|---|
| Scaled frame delta |
| Raw milliseconds since last frame |
| Current frames per second |
sprite.eventMode = 'static'; // Interactive, non-moving
sprite.eventMode = 'dynamic'; // Interactive, moving (receives idle events)
sprite.eventMode = 'passive'; // Default, children can be interactive
sprite.eventMode = 'none'; // No interactionsprite.eventMode = 'static';
sprite.on('pointerdown', (event) => {
console.log('Clicked at', event.global.x, event.global.y);
});
sprite.on('pointermove', (event) => { /* ... */ });
sprite.on('pointerup', (event) => { /* ... */ });
sprite.on('pointerover', (event) => { /* ... */ });
sprite.on('pointerout', (event) => { /* ... */ });import { Rectangle, Circle } from 'pixi.js';
sprite.hitArea = new Rectangle(0, 0, 100, 100);
// or
sprite.hitArea = new Circle(50, 50, 50);sprite.cursor = 'pointer';
sprite.cursor = 'grab';
sprite.cursor = 'url(cursor.png), auto';container.interactiveChildren = false; // Skip children hit testingresolutioncontainer.filters = nullfilterAreacullable = trueRenderGroupsinteractiveChildren = false// OLD (v7)
const app = new Application({ width: 800 });
// NEW (v8)
const app = new Application();
await app.init({ width: 800 });// OLD (v7)
graphics.beginFill(0xff0000).drawRect(0, 0, 100, 100).endFill();
// NEW (v8)
graphics.rect(0, 0, 100, 100).fill(0xff0000);// OLD (v7)
ticker.add((dt) => sprite.rotation += dt);
// NEW (v8)
ticker.add((ticker) => sprite.rotation += ticker.deltaTime);// OLD (v7)
app.view
// NEW (v8)
app.canvasSpriteGraphicsMeshContainer// OLD (v7)
const rect = container.getBounds();
// NEW (v8)
const rect = container.getBounds().rectangle;Applicationmodules/browser/src/radar/camera-view.tsimport { Application, ApplicationOptions, Container } from 'pixi.js';
export class CameraView extends Application {
constructor(public camera: Camera) {
super();
}
public async initialize(
pixiOptions: Partial<ApplicationOptions>,
container: WidgetContainer
) {
await super.init(pixiOptions);
// Limit FPS to prevent GPU heating
this.ticker.maxFPS = 30;
// Handle resize
container.on('resize', () => {
this.resizeView(container.width, container.height);
});
// Append canvas
container.getElement().append(this.canvas);
}
// Coordinate transformations
public worldToScreen = (w: XY) => this.camera.worldToScreen(this.renderer, w.x, w.y);
public screenToWorld = (s: XY) => this.camera.screenToWorld(this.renderer, s.x, s.y);
// Layer management
public addLayer(child: Container) {
this.stage.addChild(child);
}
}ticker.maxFPS = 30worldToScreen()screenToWorld()addLayer()renderRootmodules/browser/src/radar/grid-layer.tsimport { Container, Graphics } from 'pixi.js';
export class GridLayer {
private stage = new Container();
private gridLines = new Graphics();
constructor(private parent: CameraView) {
this.parent.events.on('screenChanged', () => this.drawSectorGrid());
this.stage.addChild(this.gridLines);
}
get renderRoot(): Container {
return this.stage;
}
private drawSectorGrid() {
// Clear and redraw
this.gridLines.clear();
// Draw lines using v8 API
this.gridLines
.moveTo(0, screen)
.lineTo(this.parent.renderer.width, screen)
.stroke({ width: 2, color: magnitude.color, alpha: 0.5 });
}
}stagerenderRootscreenChangedgraphics.clear()// Drawing selection rectangle
const graphics = new Graphics();
graphics
.rect(min.x, min.y, width, height)
.fill({ color: selectionColor, alpha: 0.2 })
.stroke({ width: 1, color: selectionColor, alpha: 1 });
// Drawing grid lines
this.gridLines
.moveTo(0, screenY)
.lineTo(rendererWidth, screenY)
.stroke({ width: 2, color: lineColor, alpha: 0.5 });private redraw() {
this.graphics.clear();
// ... draw new content
}modules/browser/src/radar/interactive-layer.tsimport { Container, FederatedPointerEvent, Rectangle } from 'pixi.js';
export class InteractiveLayer {
private stage = new Container();
constructor(private parent: CameraView) {
// Set cursor
this.stage.cursor = 'crosshair';
// Enable interaction
this.stage.interactive = true;
// Set hit area to full canvas
this.stage.hitArea = new Rectangle(
0, 0,
this.parent.renderer.width,
this.parent.renderer.height
);
// Register events
this.stage.on('pointerdown', this.onPointerDown);
this.stage.on('pointermove', this.onPointerMove);
this.stage.on('pointerup', this.onPointerUp);
// Update hit area on resize
this.parent.events.on('screenChanged', () => {
this.stage.hitArea = new Rectangle(
0, 0,
this.parent.renderer.width,
this.parent.renderer.height
);
});
}
private onPointerDown = (event: FederatedPointerEvent) => {
const screenPos = XY.clone(event.global);
const worldPos = this.parent.screenToWorld(screenPos);
// ... handle interaction
};
}stage.interactive = truestage.hitArea = new Rectangle(...)event.globalscreenToWorld()modules/browser/src/radar/texts-pool.tsexport class TextsPool {
private texts: Text[] = [];
constructor(private container: Container) {}
*[Symbol.iterator]() {
let index = 0;
while (true) {
if (index >= this.texts.length) {
const text = new Text({ text: '', style: { ... } });
this.texts.push(text);
this.container.addChild(text);
}
const text = this.texts[index];
text.visible = true;
yield text;
index++;
}
}
return() {
// Hide unused texts
for (let i = this.usedCount; i < this.texts.length; i++) {
this.texts[i].visible = false;
}
}
}
// Usage
const textsIterator = this.textsPool[Symbol.iterator]();
for (const item of items) {
const text = textsIterator.next().value;
text.text = item.label;
text.x = item.x;
text.y = item.y;
}
textsIterator.return(); // Hide unuseddata-idthis.canvas.setAttribute('data-id', 'Tactical Radar');// Select canvas by data-id
const canvas = page.locator('[data-id="Tactical Radar"]');
// Get attribute values
const zoom = await canvas.getAttribute('data-zoom');class RadarDriver {
constructor(private canvas: Locator) {}
async getZoom() {
return Number(await this.canvas.getAttribute('data-zoom'));
}
async setZoom(target: number) {
await this.canvas.dispatchEvent('wheel', { deltaY: ... });
}
}data-idpage.locator('[data-id="Panel Name"]')| Task | Starwards Pattern |
|---|---|
| Create layer | |
| Draw graphics | |
| Redraw | |
| Interactive | |
| Events | |
| Coords | |
| FPS limit | |
| Test selector | |
beginFill()drawRect()endFill()rect().fill().stroke()new Application({ width: 800 })await app.init({ width: 800 })graphics.clear()stage.hitAreaTexture.from()