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
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:
- sprite_64.png (512 × 256 px, 4 rows × 8 frames, hard-alpha RGBA)
- atlas_64.json (frame rectangles,
meta.fps = 10,animationslists) - loopixel-excalibur.ts (strict TypeScript helper checked against
excalibur@1.0.1)
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.
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,
},
});
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.
| Direction | Row in sprite_64.png | Excalibur spriteSheetIndex | Frame timing |
|---|---|---|---|
| Front (down) | Row 0 | [0, 1, 2, 3, 4, 5, 6, 7] | 100 ms (10 fps) |
| Right | Row 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) |
| Left | Row 3 | [24, 25, 26, 27, 28, 29, 30, 31] | 100 ms (10 fps) |
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.
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);
});- Install Excalibur (
npm install excalibur) and copyloopixel-excalibur.tsinto your source folder. - Copy
sprite_64.pngandatlas_64.json(or any 32–128 px pair from your Loopixel ZIP) into your staticpublic/folder. - Scale your
Actorby whole numbers (ex.vec(2, 2)orex.vec(3, 3)) so every pixel stays square and free of mixels.
Common Excalibur pitfalls
- Blurry sprites on scale. Pass
filtering: ex.ImageFiltering.PixeltoImageSourceandantialiasing: falsetoEngine; without both, WebGL linear filtering blurs pixel edges. - Passing
anim_64.gifintoImageSource. Excalibur warns on.gifpaths inImageSource; use the PNG sprite sheet (sprite_64.png) forSpriteSheet. - Fractional actor scale. Keep
actor.scaleat integer multiples (1, 2, 3) to avoid uneven pixel sizes (mixels) on screen. - Passing spacing/margin values. Leave
spacingunset onSpriteSheet.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:
- Excalibur.js v1.0 SpriteSheet source
- Excalibur.js v1.0 Animation source
- Excalibur.js ImageSource filtering
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