indiaciv, part 6: Drawing 50,000 sprites in a browser
Five parts of pipeline produced a 325 KB bundle of byte arrays (part 5 finished it). This last part is the other half: turning those bytes into a map you can drag around at 60 fps, with no image files, on whatever laptop or phone opens it. It’s also the part where I learned the most, mostly by doing it wrong first.
No image files
Every texture on the map is painted by code when the page loads. The renderer is
PixiJS v8 on WebGL, and before it draws anything, atlas.js paints a 2048 ×
2048 canvas with every tile and sprite the map needs, 241 of them:

- Terrain tiles: 10 terrain types × 4 variants, each a hexagon filled with a gradient, a few hundred soft brush dabs, a terrain motif (field strips for plains, dune ripples for desert, scree for mountains), paper grain and a faint edge. Each also has a greyed, darker copy for neighbouring countries.
- Relief: mountains (with and without snow), hills, broadleaf, jungle and pine trees.
- Settlements: four city tiers and small towns, with houses, a dome, a temple spire and, for the biggest, towers.
- Icons: resources and map sites, drawn as small vector shapes.
Painting it is part of the under-a-second load. In return the art is about 44 KB of code instead of megabytes of PNG, it’s original (no game assets), and as you’ll see below, a new theme is mostly just a different painter.
Each hex picks one of the 4 variants, a rotation in 60° steps and a slight tint, all from a hash of its index. So the same hex always looks the same, and neighbouring hexes rarely repeat.
Layers
The map is a stack of layers, bottom to top:
The workhorse is PixiJS’s ParticleContainer, which draws thousands of sprites in a single draw call
as long as they all share one texture. That’s the real reason for the single atlas: terrain,
relief and icons all come from the same canvas, so a whole layer is one batch.
City banners are HTML, not WebGL. They’re text-heavy, need crisp type at any zoom, and are clickable, and the DOM is good at all three. That decision came back to bite me.
Why was mine slower than usaciv?
The first version worked and looked right, and it was slow. On my laptop it was fine-ish, but in
headless Chromium with software rendering (SwiftShader, a good stand-in for a weak GPU) panning ran at
4 frames per second, against 20 for usaciv on the same machine. Measured with a small Playwright
harness (scripts/perf.mjs) that pans and zooms for four seconds and records frame times, long tasks
and DOM size:
A CPU profile pointed at four problems:
- It rendered every frame, forever. PixiJS’s default ticker redraws at the display’s refresh rate whether anything changed or not. A static map doesn’t need 60 redraws a second while you read a tooltip.
- It drew everything, always. About 16,000 terrain tiles and 33,000 relief sprites went to the GPU every frame, even when you were zoomed into one city.
- 61,674 DOM nodes. Every one of the 983 city banners existed all the time, each with inline SVG icons for its yields, and positioning them read element widths, which forced the browser to recompute layout.
- Pixel readback at load. Greying out foreign tiles read every pixel back with
getImageData, which stalls the GPU-backed canvas.
Render only when something changes
The ticker is off (autoStart: false). Instead, anything that changes what’s visible (camera, hover,
lens, window size) calls requestRender(), which schedules exactly one frame:
let pending = false;
const requestRender = () => {
if (pending) return;
pending = true;
requestAnimationFrame(() => {
pending = false;
scene.prepare(app.renderer); // cull chunks to the current view
app.render();
});
};
When you stop moving, the page uses no CPU and no GPU.
Chunks and culling
Every static layer is split into 16 × 16-hex chunks, each its own ParticleContainer or
Graphics with a precomputed bounding box. Before each frame, PixiJS’s Culler marks every chunk
outside the screen as culled, and culled chunks are skipped entirely. Zoomed in on Delhi at a typical
reading zoom, that’s 15 chunks out of 90 or so: 3,745 of 16,068 terrain tiles and 4,159 of 33,462
relief sprites actually drawn.
Two traps here. My first culling pass culled everything, because the culler reads each object’s
world transform and those were stale until the next render, which hadn’t happened yet. Passing
skipUpdateTransform = false makes it update them first. And I tried to go further by baking the
zoomed-out map into one texture with cacheAsTexture, which gave an empty map: in PixiJS 8.23, cached
textures silently drop ParticleContainer content. That idea went in the bin.
Banners from a pool
City banners now exist only while on screen. The banner layer keeps a Map of live banners and a free
list. When a city scrolls off screen its element goes back to the pool, and when one scrolls in it
takes an element and fills it in. Widths come from canvas measureText, so the layout never asks the
DOM how wide anything is. Yield icons became CSS background sprites instead of inline SVG. DOM nodes:
61,674 → about 1,900.
Less detail when far away
At the whole-India view every mountain is a few pixels, but there were still 33,462 of them. Below zoom 0.22 the map switches to a second, sparse relief set: one peak per mountain hex instead of three, two trees per forest instead of six or seven, plus every city and town. That’s 9,573 sprites. At that size you can’t see the difference, and on software GL panning went from 9.3 to 14.8 fps in an A/B run.
On a real GPU the map now holds 60+ fps panning and is interactive in under a second. On software rendering it’s still behind usaciv, which has a much lighter scene, but it’s no longer embarrassing.
Four themes
Since every pixel is painted in code, a theme is a different set of painters and palettes. There are
four, switchable from a dropdown and kept in the URL (?theme=atlas):

- Monsoon, the default: painted tiles, a frame after the borders of Indian miniature paintings, storm-indigo panels and serif type.
- Survey: a plain, colony-sim style field map. Flat muted tiles with a thin grid, square dark-grey panels, a sans-serif.
- Atlas: an old survey sheet. Pale watercolour washes on paper, mountains drawn as hatched ink profiles, tree symbols, towns as carmine blocks, water-lined coasts, a neatline border, a compass rose and a scale bar. The bar’s length follows the zoom: one hex is 20 km across its flats and world pixels, so a kilometre is screen pixels.
- Pixel: retro. Textures are posterised, and the WebGL canvas renders at a third of the screen
resolution and is scaled up with
image-rendering: pixelated. That makes every line, label and tile chunky at any zoom, and it means drawing a ninth as many pixels.
The theme is picked once at load and switching reloads the page. That kept the code simple: the
palette module exports one theme’s values, the atlas painter checks the theme in a few places, and CSS
handles the chrome with [data-theme=…] rules.
One theme bug is worth telling. To mute the Survey theme’s sprites I first drew them through a canvas
filter: saturate(0.55). On software GL, the hundreds of filtered draws made Chromium lose the WebGL
context, an empty map with “Could not initialize shader” errors. Replacing the filter with one pass
over the pixels at the end fixed it. The general lesson: canvas filters aren’t free, and on some
devices they share the GPU with your WebGL context.
Shipping it
The site is a static bundle with every URL relative (Vite base: './'), so it can live in any
folder. make publish builds it and copies it into this blog’s Hugo repo under static/indiaciv/,
and GitHub Actions deploys the blog. That’s how it’s served at aeris.fyi/indiaciv.
Three deployment bugs, each small and each instructive:
- A stale
index.html.rsyncdecides whether to copy a file from its size and modification time. A rebuild produced a newindex.htmlof exactly the same size (only a content hash in a filename changed), so it wasn’t copied, and the page pointed at a script that didn’t exist.rsync --checksumfixed it. - A transient 503. The first live load got a 503 from the CDN for one data file, and the map failed to load. Data fetches now retry with backoff on 5xx and 429.
- The blog’s own CI failed installing Dart Sass from the snap store (“unable to contact snap store”). The theme doesn’t need it, so that step is now allowed to fail.
That’s the series
Twenty public datasets, one hex grid, five pipeline stages, 61 checks, a 325 KB bundle, and about 2,600 lines of JavaScript that paint and draw it all. If I had to pick one lesson from the whole project, it’s compute it twice and compare: source totals against hex totals, the rule table in Python against the rule table in JavaScript, an A/B perf run against a baseline. Nearly every real bug in this series was caught that way.
Go and explore the map. Try the Atlas theme, hover a hex in Ladakh, find your district.
Series: 1 · India on hexes · 2 · Terrain · 3 · People · 4 · Yields · 5 · Climate and roads · 6 · The browser