Manual Ship

On this page
  1. Export
  2. Play it
  3. Several sprites
  4. Pitfalls
  5. Next

PixiJS

Export a sprite as a PixiJS atlas with its animations, frame timing and a pivot at the feet, and play it in a few lines.

The pixi format writes the atlas PNG and a JSON file in PixiJS's own Spritesheet layout, which Assets.load reads as it is, in PixiJS v7 and v8. Every animation becomes a list of textures for an AnimatedSprite, every frame has its anchor at the feet, and the timing of each animation travels in the file.

The starter hero walking in a PixiJS page at 2x, standing on the same ground line in every frame
hero_pixi.json playing in PixiJS 8

Export#

Make a starter project to follow along, then export its hero:

bash
mode8 init my-game --template platformer
cd my-game
terminal
$ mode8 export assets/hero/frames --out-dir web/assets --name hero --formats pixi --animations assets/hero/master.m8.json
✓ Exported hero (15 frames) → web/assets/
  pixi · 3 files
  animations attack, hurt, idle, jump, walk
  palette nes_master_gothic from mode8.toml

Next:
  mode8 verify --assets-dir web/assets

hero_pixi.json has the three parts PixiJS expects: frames (each with its rectangle and an anchor), animations (frame names in playback order) and meta. The speed and the loop setting of each animation have no slot in that layout, so they sit under meta.mode8:

terminal
$ python3 -c "import json; d = json.load(open('web/assets/hero_pixi.json')); print(d['animations']['walk']); print(d['meta']['mode8'])"
['walk_0', 'walk_1', 'walk_2', 'walk_3', 'walk_4', 'walk_5']
{'fps': 8, 'pivot': [36, 72], 'animations': {'attack': {'fps': 8, 'loop': False, 'durations': [2, 1, 2]}, 'hurt': {'fps': 8, 'loop': False}, 'idle': {'fps': 4, 'loop': True, 'durations': [2, 1, 2, 1]}, 'jump': {'fps': 8, 'loop': False}, 'walk': {'fps': 10, 'loop': True}}}

Play it#

Serve the folder with any web server (Assets.load cannot read files from file://), and load the sheet:

js
import { Application, Assets, AnimatedSprite, TextureStyle } from "pixi.js";

TextureStyle.defaultOptions.scaleMode = "nearest";      // square pixels when scaled

const app = new Application();
await app.init({ width: 480, height: 270, roundPixels: true, background: "#1a1c2c" });
document.body.appendChild(app.canvas);

const sheet = await Assets.load("assets/hero_pixi.json");
const timing = sheet.data.meta.mode8.animations;

const hero = new AnimatedSprite(sheet.animations.walk);
hero.animationSpeed = timing.walk.fps / 60;              // the ticker runs at 60 per second
hero.loop = timing.walk.loop;
hero.position.set(240, 220);                             // where the feet stand
hero.scale.set(2);
hero.play();
app.stage.addChild(hero);

The anchor comes from the file, so position is the point between the feet, in every animation. To switch animation, set hero.textures = sheet.animations.attack, update animationSpeed and loop from timing.attack, and call hero.play(); hero.onComplete fires when a one-shot animation ends.

In PixiJS v7 the same code works with v7's setup: BaseTexture.defaultOptions.scaleMode = SCALE_MODES.NEAREST and new Application({ ... }) instead of app.init().

Several sprites#

Export each sprite under its own --name into the same folder; the files never collide. For a project's whole cast, run one export per asset:

terminal
$ mode8 export assets/slime/master.png --out-dir web/assets --name slime --formats pixi
✓ Exported slime (1 frame) → web/assets/
  pixi · 3 files
  animations master
  palette nes_master_gothic from mode8.toml

Next:
  mode8 verify --assets-dir web/assets

A single image becomes a sheet with one frame, named master after its file.

Pitfalls#

  • Blurry sprites. Set the nearest scale mode before loading any texture, and scale by whole numbers (scale.set(2), not 1.5).
  • The animation runs too fast. animationSpeed is a fraction of the 60 Hz ticker, not frames per second: divide the fps by 60.
  • 404 on the image. meta.image names hero_atlas.png relative to the JSON. Keep the two files in the same folder.

Next#