Sprite Sheets
A sprite sheet packs the frames of an animation (or many small images) into one texture. The GPU then draws any frame by sampling a rectangle of that texture, which means one texture bind and, with batching, one draw call for many sprites. This lesson covers grid sheets, the metadata that describes them, and the import settings that keep pixel art crisp. Drawing sprites from a sheet in code is covered in Sprites & Sprite Sheets.
Grid sheets: margin, spacing, frame size
The simplest sheet is a grid: every frame the same size, laid out in rows. Tools describe it with four numbers: frame size, margin (border around the whole sheet), spacing (gap between frames) and columns. Getting one wrong shifts every frame by a few pixels:
def grid_frames(sheet_w, sheet_h, frame_w, frame_h, margin=0, spacing=0):
cols = (sheet_w - 2 * margin + spacing) // (frame_w + spacing)
rows = (sheet_h - 2 * margin + spacing) // (frame_h + spacing)
frames = []
for r in range(rows):
for c in range(cols):
x = margin + c * (frame_w + spacing)
y = margin + r * (frame_h + spacing)
frames.append((x, y, frame_w, frame_h))
return cols, rows, frames
cols, rows, frames = grid_frames(136, 70, 32, 32, margin=1, spacing=2)
print(f"{cols} columns x {rows} rows = {len(frames)} frames")
for i in (0, 1, 3, 4, 7):
print(f"frame {i}: x={frames[i][0]:3} y={frames[i][1]:3} w={frames[i][2]} h={frames[i][3]}")4 columns x 2 rows = 8 frames
frame 0: x= 1 y= 1 w=32 h=32
frame 1: x= 35 y= 1 w=32 h=32
frame 3: x=103 y= 1 w=32 h=32
frame 4: x= 1 y= 35 w=32 h=32
frame 7: x=103 y= 35 w=32 h=32
The same numbers appear in Unity's Sprite Editor (Slice → Grid By Cell Size, with Offset and Padding), Godot's Sprite2D (hframes/vframes for gapless grids) and SpriteFrames editor (Add frames from sprite sheet, with separation and offset), and Tiled's tileset dialog (margin and spacing).
Why spacing and extrusion exist
With bilinear filtering or mipmaps, the GPU blends neighbouring texels. At a frame's edge, that pulls in a few pixels from the next frame: thin coloured lines appear around sprites ("bleeding"). Two fixes:
- Spacing/padding of 1–2 transparent pixels between frames.
- Extrusion: copy each frame's edge pixels outward into the padding, so any bleed samples the right colour (packing tools such as TexturePacker and free-tex-packer offer it as an option).
For pixel art, use point (nearest) filtering, no mipmaps and no compression that blurs: Unity Filter Mode: Point and Compression: None; Godot Project Settings → Rendering → Textures → Canvas Textures → Default Texture Filter: Nearest. Then bleeding mostly disappears anyway.
Frame metadata and timing
Packed sheets (frames of different sizes, trimmed of empty space) need a data file. Aseprite, TexturePacker and similar tools write JSON listing each frame's rectangle, its duration, and named tags (animation ranges). Exporting from Aseprite on the command line, for example:
aseprite -b hero.aseprite --sheet hero.png --data hero.json \
--format json-array --sheet-type packed --list-tagsA game then plays an animation by summing durations, which supports frames that hold longer than others (an anticipation frame before a jump, say):
import json
data = json.loads('''{
"frames": [
{"filename": "hero 0", "frame": {"x": 0, "y": 0, "w": 24, "h": 32}, "duration": 100},
{"filename": "hero 1", "frame": {"x": 24, "y": 0, "w": 24, "h": 32}, "duration": 100},
{"filename": "hero 2", "frame": {"x": 48, "y": 0, "w": 24, "h": 32}, "duration": 100},
{"filename": "hero 3", "frame": {"x": 72, "y": 0, "w": 28, "h": 30}, "duration": 250},
{"filename": "hero 4", "frame": {"x": 100, "y": 0, "w": 30, "h": 26}, "duration": 80},
{"filename": "hero 5", "frame": {"x": 130, "y": 0, "w": 30, "h": 26}, "duration": 80}
],
"meta": {"frameTags": [
{"name": "run", "from": 0, "to": 2, "direction": "forward"},
{"name": "jump", "from": 3, "to": 5, "direction": "forward"}
]}
}''')
def frame_at(tag_name, t_ms, loop=True):
tag = next(t for t in data["meta"]["frameTags"] if t["name"] == tag_name)
frames = data["frames"][tag["from"]:tag["to"] + 1]
total = sum(f["duration"] for f in frames)
t = t_ms % total if loop else min(t_ms, total - 1)
for f in frames:
if t < f["duration"]:
return f["filename"], f["frame"]
t -= f["duration"]
for t in [0, 150, 299, 300, 450]:
print(f"run @ {t:3} ms -> {frame_at('run', t)[0]}")
for t in [0, 249, 250, 330, 999]:
print(f"jump @ {t:3} ms -> {frame_at('jump', t, loop=False)[0]}")run @ 0 ms -> hero 0
run @ 150 ms -> hero 1
run @ 299 ms -> hero 2
run @ 300 ms -> hero 0
run @ 450 ms -> hero 1
jump @ 0 ms -> hero 3
jump @ 249 ms -> hero 3
jump @ 250 ms -> hero 4
jump @ 330 ms -> hero 5
jump @ 999 ms -> hero 5
Note the jump uses loop=False: it holds its last frame instead of wrapping, which is what you want for one-shot animations.
Production tips
- Keep the source files (
.aseprite,.psd,.kra) in version control (LFS) and treat the PNG + JSON as build output, regenerated by a script. - Keep one pivot convention (bottom centre for characters) and set it in the import settings, or the character jitters between frames of different sizes.
- Put animations that are on screen together on the same sheet or atlas, so they batch (Texture Atlas Packing).
- Mobile GPUs and older APIs limit texture size (4096 or 8192 pixels per side are common limits); split very large sheets.
Test yourself
Thin lines from neighbouring frames appear at sprite edges when the camera zooms. What helps?
- More frames
- Padding with extrusion between frames, or point filtering for pixel art
- A bigger frame rate
- Converting to JPEG
A sheet is 136 px wide with a 1 px margin, 2 px spacing and 32 px frames. How many columns?
- 3
- 4
- 5
- Can't tell
Why store per-frame durations instead of one frame rate?
- It's smaller
- Some frames need to hold longer (anticipation, impact) than others
- Engines require it
- To loop