Pixel Realms: a storybook world you wander with other people
- React 19
- TypeScript
- Web Workers
- OffscreenCanvas
- WebSocket
- FastAPI
- Procedural generation
The use case
A shared world to explore with other people, built to show that a browser can run a game loop, generate a huge map and talk to other players over a WebSocket without ever touching the page’s main thread. It is also just meant to be charming.
Measured
- tiles in each of the six worlds
- 2,048 × 2,048
- frames per second, and main-thread freezes over 50 ms, in an 8 s run at the widest zoom
- 120 fps, 0
- average to build and draw one 16 × 16 tile chunk, with scenery
- 1.4 ms
Frame rate and chunk timings were measured in a development build with headless Chromium on a Mac, in a 1920 by 1080 window, running for 8 seconds at the widest zoom, by day and by night. In a separate in-process test, the server’s room logic handled 80 players all within range, each sending 15 updates a second (about 95,000 outgoing messages a second), using about 4% of one core; that excludes the cost of the network itself.
What I built
- Handed the canvas to a worker with OffscreenCanvas, so the worker owns the whole game: a fixed 60 Hz simulation on a timer, drawing on the browser’s frame, and the WebSocket. The page only forwards input and shows text.
- Generated the worlds from seeds. Each world is 2,048 by 2,048 tiles, built in 16 by 16 chunks on demand, drawn once to a small canvas and kept in a least-recently-used cache, so the cost of a frame follows the screen size, not the world size.
- Wrote the WebSocket server in the existing FastAPI app: one in-memory room per world, a player only hears about people nearby at full rate, and everything is validated and capped (80 players a world, 400 in all, 6 connections an address, about 40 messages a second each).
- Made it safe to open to anyone: no free text anywhere. Names are picked from a silly-name generator and everything else a player sends is a number (a position, an emote), so there is nothing to moderate.
- Gave the world a day that turns to night every ten minutes, weather, fireflies and drifting cloud shadows, all worked out from the clock so every player sees the same sky without a single message.
- Filled it with bunnies, frogs, ducks and cats whose wandering is a function of the time, so everyone sees the same animal in the same place for free; pets that follow a player; mushroom houses, giant flowers, lanterns and signposts with silly names.
- Replaced stone gates with six storybook doors (a treehouse door, a rainbow, a wardrobe, a moon door, a rabbit hole and a toadstool), each joining two worlds, with a test that every door in every world is reachable by road.
- Built click-to-walk with a capped A* search for nearby targets and step-by-step planning for far ones, plus keyboard, run, wheel, pinch and button zoom in whole-pixel steps so the art stays sharp.
- Drew all art from code (characters, tiles, doors, scenery, animals, emotes), so there are no image files to load.
Decisions worth explaining
The worker draws, not just computes
Moving a canvas’s control to the worker means a slow frame in the game cannot block scrolling or typing on the page, and a busy page cannot make the game stutter.
Everything shared by the clock needs no network
The time of day, the weather and every wandering animal are pure functions of the time (and a seed). Two strangers see the same dusk and the same bunny, and the server never hears about either.
Each browser is the authority for its own character
There is no referee. A browser tells the server where it is about 15 times a second and the server passes it on. That keeps the server tiny and is fine for a social world, but it is not a design for competitive play.
Nearby people at full rate, everyone else now and then
Sending every move to everyone grows with the square of the crowd. The server sends full-rate updates only to players within about 1,500 pixels and every tenth update to the rest, which keeps the roster and mini-map roughly right for a fraction of the traffic.
No free text, so no moderation
The cheapest way to make a public world kind is to give people nothing to be unkind with. Emotes are numbers and names are generated.
Zoom in whole steps
Pixel art blurs at fractional scales, so zoom moves between whole numbers, easing quickly from one to the next, and the camera stays on the character.
Limits, on purpose
The server keeps everything in memory in one process, so it would need shared state to run as several. Players are trusted about their own position, so it is not for competitive play. Worlds are generated rather than hand-drawn, so landmarks are few. A world holds 80 players; beyond that you can still wander alone.