Excalibur.js Sprite Sheet: Load, Slice and Animate in TypeScript

· 6 min read · Loopixel team

Type-checked in strict mode against excalibur@1.0.1 using real sprite_64.png and atlas_64.json

Four direction frames of a pixel character arranged for Excalibur.js SpriteSheet with ImageFiltering.Pixel

Excalibur.js is a TypeScript 2D HTML5 game engine with first-class SpriteSheet and Animation classes. Because every Loopixel export packs square frames with zero padding, zero margin and hard-alpha transparency, you can slice a 1-row action or a 4-direction walk cycle in a dozen lines of TypeScript without writing custom UV math.

On this page · 5

Sample files & helper

Grab the real 64 px 4-direction sheet, its atlas JSON and the MIT-licensed TypeScript loader directly to test in your Excalibur v1 project:

Load and slice with Pixel filtering

Excalibur defaults to blended texture filtering, which softens pixel edges when scaled on a canvas. Set filtering: ex.ImageFiltering.Pixel on the ImageSource and antialiasing: false in your Engine options, then slice with SpriteSheet.fromImageSource. No spacing or originOffset is needed because Loopixel sheets have 0 px margins.

Load image with Pixel filtering and slice 8 × 4 cells
import * as ex from "excalibur";

// 1. Load sprite_64.png with Pixel filtering so upscaling stays crisp
const walkImage = new ex.ImageSource("./sprite_64.png", {
  filtering: ex.ImageFiltering.Pixel,
});

// 2. Slice the 512 x 256 sheet on an exact 8 x 4 grid (0 padding, 0 offset)
const walkSheet = ex.SpriteSheet.fromImageSource({
  image: walkImage,
  grid: {
    rows: 4,
    columns: 8,
    spriteWidth: 64,
    spriteHeight: 64,
  },
});
Loopixel 4-direction 64 px sprite sheet annotated with Excalibur grid rows 0 to 3 and frame indices 0 to 31
The 8 × 4 grid in sprite_64.png: row 0 front (0..7), row 1 right (8..15), row 2 back (16..23), row 3 left (24..31).

Build 4-direction animations

Excalibur’s Animation.fromSpriteSheet indexes cells left-to-right, top-to-bottom starting at 0. Because each row has 8 frames, multiplying the row index by 8 gives each direction’s slice cleanly.

DirectionRow in sprite_64.pngExcalibur spriteSheetIndexFrame timing
Front (down)Row 0[0, 1, 2, 3, 4, 5, 6, 7]100 ms (10 fps)
RightRow 1[8, 9, 10, 11, 12, 13, 14, 15]100 ms (10 fps)
Back (up)Row 2[16, 17, 18, 19, 20, 21, 22, 23]100 ms (10 fps)
LeftRow 3[24, 25, 26, 27, 28, 29, 30, 31]100 ms (10 fps)
Create looping animations for all four directions
const FRAME_MS = 100; // 10 fps (matches atlas_64.json meta.fps = 10)

const anims = {
  front: ex.Animation.fromSpriteSheet(walkSheet, [0, 1, 2, 3, 4, 5, 6, 7], FRAME_MS, ex.AnimationStrategy.Loop),
  right: ex.Animation.fromSpriteSheet(walkSheet, [8, 9, 10, 11, 12, 13, 14, 15], FRAME_MS, ex.AnimationStrategy.Loop),
  back:  ex.Animation.fromSpriteSheet(walkSheet, [16, 17, 18, 19, 20, 21, 22, 23], FRAME_MS, ex.AnimationStrategy.Loop),
  left:  ex.Animation.fromSpriteSheet(walkSheet, [24, 25, 26, 27, 28, 29, 30, 31], FRAME_MS, ex.AnimationStrategy.Loop),
};

Drop-in TypeScript loader

Instead of hard-coding 64 or row counts, import buildLoopixelAnimations from loopixel-excalibur.ts. It reads meta.frame_size.w, meta.mode (single or quad), and meta.fps from atlas_{size}.json, so the exact same code works for 32, 48, 64, 96 and 128 px exports.

Run with Excalibur v1.0+ and switch directions on WASD / arrows
import * as ex from "excalibur";
import type { LoopixelAtlas } from "./loopixel-excalibur";
import { buildLoopixelAnimations } from "./loopixel-excalibur";

const res = await fetch("./atlas_64.json");
const atlas = (await res.json()) as LoopixelAtlas;

const image = new ex.ImageSource(`./${atlas.meta.image}`, {
  filtering: ex.ImageFiltering.Pixel,
});

const game = new ex.Engine({
  width: 640,
  height: 360,
  antialiasing: false,
  backgroundColor: ex.Color.fromHex("#0e0e0d"),
});

await game.start(new ex.Loader([image]));

const anims = buildLoopixelAnimations(image, atlas);
const hero = new ex.Actor({ pos: ex.vec(320, 180), scale: ex.vec(2, 2) });
hero.graphics.use(anims.front);
game.add(hero);

game.input.keyboard.on("press", (evt) => {
  if (evt.key === ex.Keys.Down || evt.key === ex.Keys.S) hero.graphics.use(anims.front);
  if (evt.key === ex.Keys.Right || evt.key === ex.Keys.D) hero.graphics.use(anims.right);
  if (evt.key === ex.Keys.Up || evt.key === ex.Keys.W) hero.graphics.use(anims.back);
  if (evt.key === ex.Keys.Left || evt.key === ex.Keys.A) hero.graphics.use(anims.left);
});
  1. Install Excalibur (npm install excalibur) and copy loopixel-excalibur.ts into your source folder.
  2. Copy sprite_64.png and atlas_64.json (or any 32–128 px pair from your Loopixel ZIP) into your static public/ folder.
  3. Scale your Actor by whole numbers (ex.vec(2, 2) or ex.vec(3, 3)) so every pixel stays square and free of mixels.

Common Excalibur pitfalls

  • Blurry sprites on scale. Pass filtering: ex.ImageFiltering.Pixel to ImageSource and antialiasing: false to Engine; without both, WebGL linear filtering blurs pixel edges.
  • Passing anim_64.gif into ImageSource. Excalibur warns on .gif paths in ImageSource; use the PNG sprite sheet (sprite_64.png) for SpriteSheet.
  • Fractional actor scale. Keep actor.scale at integer multiples (1, 2, 3) to avoid uneven pixel sizes (mixels) on screen.
  • Passing spacing/margin values. Leave spacing unset on SpriteSheet.fromImageSource: Loopixel cells have 0 px margin and 0 px offset.

Sources

Engine menu paths and property names above were checked against the official documentation:

Try it with one image

Upload one image and get an 8-frame pixel sprite sheet in about a minute. 1 credit per animation, credit packs from $2.99, paid once.

Create a sprite sheet