Chess in the Margins
Chess drawn in ballpoint on a sheet of paper, where every piece is a creature and captures turn into little fights. Underneath the doodles is a complete rules engine, a small search AI, server-checked online rooms and a game that keeps working with the network unplugged.

A short endgame in ballpoint against a tiny engine. The interactive demo loads here when scripts run.
- 0 depsto build. Plain HTML, CSS and JavaScript; GSAP and rough.js are vendored.
- 1 engine
engine.jsruns in the browser and on the server, so both agree on the rules. - 197,281perft nodes at depth 4 from the start position, matching the reference count.
- 1 DO / roomeach online game is its own Durable Object holding only the move list.
Chess that looks doodled, plays for real
Most chess apps look like a spreadsheet of squares. This one is meant to feel like a game someone doodled in the margin of a notebook: wobbly ink lines, pieces with legs and hats, and a short scuffle every time one takes another. The story is light (two kingdoms arguing over a golden star), but the constraints were serious:
- It has to be real chess: every rule, including the awkward ones (en passant, threefold repetition, insufficient material), checked against known perft counts.
- The hand-drawn look must stay smooth on a phone, not only on a desktop GPU.
- It should work offline, and stay small enough to read: a handful of files, no framework, no bundler.
- Online games must be fair without trusting the browser.
One engine, two places

browser (static, Vercel) Cloudflare
┌──────────────────────────────────┐ ┌──────────────────────────────┐
│ game.js board, camera, fights │ POST │ Worker /rooms │
│ │ │ ─────▶ │ new 6-char code ──▶ /init │
│ ├─ engine.js rules + search ◀─┼─same───┼─▶ Room (Durable Object) │
│ ├─ creatures.js rough.js art │ file │ move list, seats, result │
│ └─ one <canvas> per frame │ WS │ replays moves to verify │
│ bake-worker.js OffscreenCanvas │ ◀────▶ │ alarm: delete after 24 h │
│ sw.js offline cache │ └──────────────────────────────┘
└──────────────────────────────────┘
│ /api/weather (Vercel function) ──▶ Open-Meteo, position rounded to 0.5°- engine.js0x88 board with full legal move generation, castling, en passant, promotion, the 50-move rule, threefold repetition, insufficient material and SAN. Exposed as one global (
Game,think,perft) and imported unchanged by the server. - think()Negamax with alpha-beta, a capture-only quiescence search (up to 6 plies), MVV-LVA move ordering and iterative deepening that searches the previous best move first. Depth 1 / 2 / 4 by level, a 600 ms budget (1 s on hard) checked every 1024 nodes. Evaluation is material plus piece-square tables; the easy level adds ±60 noise.
- creatures.jsTwelve creatures drawn with rough.js from separate parts. Each part is drawn twice and the copies alternate, which makes the ink lines “boil” like a flip-book.
- RenderingA hidden SVG stays as the skeleton that GSAP animates, but the whole cast is painted onto a single canvas each frame from pre-rendered part images. The board is a tilted plane with the creatures projected onto it.
- bake-worker.jsTurns an
ImageBitmapinto a PNG in anOffscreenCanvasworker, so baking art never stalls an animation frame. - RoomA SQLite-backed Durable Object per game using the hibernating WebSocket API. It stores only
{from, to, promo}moves, the seats with their tokens, the result and any rematch or draw offer. - sw.jsPrecaches the core files and about 40 sounds. Pages and scripts are network-first with a cache fallback, so a new page never runs old scripts; other assets are cache-first with background refresh.
What it’s made of
- Client
- Plain JavaScript, HTML, CSS. Canvas, OffscreenCanvas in a worker, Web Audio, WebSocket, Service Worker, PWA manifest.
- Libraries
- GSAP (animation, camera, fights) and rough.js (hand-drawn strokes), vendored for offline use.
- Rooms
- Cloudflare Worker + Durable Objects (SQLite storage), WebSockets, rate-limit binding, Wrangler 4.
- Hosting
- Static site on Vercel from
main; one serverless function for weather (Open-Meteo). - Content
- 40 mate-in-two puzzles from the Lichess puzzle database (CC0); public-domain Freesound recordings.
- Tests / CI
- Node 22
node:test, GitHub Actions with SHA-pinned actions.
Choices, and what they cost

The game is a handful of files that a browser runs as written. Anyone can read the source next to the running page.
Trade-off: no tree-shaking or type checking;game.js is large (about 300 KB) and organised by convention, not by a module system.The server imports the same engine.js as the browser, so “legal” means exactly the same thing in both places and there is only one rules implementation to test.
A room keeps only the move list and rebuilds the game by replaying it on every move. State is tiny and can never drift out of sync with the rules.
Trade-off: replay cost grows with game length, which is negligible at chess scale.Animating dozens of live SVG elements was not smooth on phones. Painting pre-rendered parts onto one canvas per frame keeps the hand-drawn look and the frame rate.
Trade-off: the SVG still exists as an invisible skeleton, so there are two representations to keep aligned.The page never asks for location. The function reads the host's IP geolocation, rounds it to 0.5° and asks Open-Meteo; nothing is stored.
Trade-off: the weather is only as accurate as IP geolocation.Don’t trust the browser
The server is authoritative. A move is applied only if it appears in the engine's own legal-move list, the sender's colour is on turn, both seats are filled and the game is not over. A client cannot claim a result; games end by mate, the rules, resignation or an accepted draw.
- Seats: a reconnecting player gets the same seat back with a 128-bit random token. Tokens are never sent to anyone else, and later arrivals become read-only spectators. Sending
hellotwice on one socket does not grab the second seat. - Room codes: 6 characters from an alphabet without look-alikes (no O/0/I/1/L), drawn with
crypto.getRandomValuesand rejection sampling so every code is equally likely. Collisions are retried up to five times. - Abuse limits: 10 new rooms a minute per IP, messages capped at 600 bytes and 12 a second, 64 sockets per room, a whitelist of six emotes, and names stripped of control characters,
<>and direction-flipping Unicode, then cut to 20 characters. - Lifetime: a Durable Object alarm deletes all room storage 24 hours after last use.
- Edge: an origin allow-list on room creation and WebSocket upgrades, a strict CSP (
script-src 'self',connect-srclimited to the rooms worker), HSTS preload, COOP and a restrictive Permissions-Policy.
Counted, not guessed
npm test runs perft first, then the rule, site and room suites. CI repeats that on every push and pull request, then runs npm audit and a wrangler deploy --dry-run.
- Perft: start position depth 4 = 197,281; Kiwipete depth 3 = 97,862; position 3 depth 4 = 43,238; position 4 depth 3 = 9,467.
- Rules: mate and stalemate, repetition (including an en passant square no pawn can use, which must not make a position new), en passant, 50-move, insufficient material, castling, promotion, king safety.
- Rooms: about 18 tests covering seats and tokens, legality, spectators, draw and resign, rematch, junk and flood messages, name sanitising, the socket cap, origins and room-code uniformity.
- Site check: every file the page and its offline copy need exists, and the security headers match what the page actually uses.
What it doesn’t do
- The AI is deliberately modest: at most depth 4, no transposition table, evaluation by material and piece-square tables. It is a friendly opponent, not a strong one.
- The clock runs in the browser; the room server does not enforce time controls.
- Computer, two-players-one-screen and puzzles work offline; online rooms need the network.