FIELD NOTES
Drawing a whole game in code
Riftspinner's spiders, enemies and items are not images. They are functions, and that choice shaped almost everything else about the game.
PLAY FREE IN YOUR BROWSEROpen Riftspinner's files and you will not find a sprite sheet. The robotic spider you control, every enemy, each weapon and armour icon, the maps and the bosses are all drawn at runtime by JavaScript, using the canvas drawing commands: paths, arcs, gradients, lines and fills. There is no art pipeline, no artist's tool and no image of a wretch anywhere in the game itself. This article explains why we made that choice, the tricks that make it practical, and what it costs, because it is the decision from which a lot of other design follows.
Why draw in code
The first reason is size. As of this release the game's single script is about 720 KB uncompressed and about 215 KB compressed over the wire, and that includes every drawing routine for every enemy, rig and item, the game logic, the sound and the interface. The fonts add about 50 KB. A comparable game with hand-painted sprite sheets for thirteen rigs, seventeen enemy types, dozens of items and animated frames would easily be several times larger. For a game that has to load on a phone over a mobile connection, and which is meant to be playable offline after one visit, small is a feature.
The second reason is that drawing in code makes art parametric. An enemy is not a picture, it is a function that takes a size, an angle, an animation time and a few state flags. That means an enemy can turn to face you, flash when it is hit and wobble as it moves, all driven by a few numbers on the enemy rather than extra frames of artwork. Cosmetic options such as shot colours, trails and kill bursts are variations on the same drawing rather than new files.
The third reason is accessibility. Because every actor is drawn by our own code, adding a bright outline to the spider, enemies, hostile shots and loot for high-contrast mode is a single conditional in the drawing routine. With bitmaps, it would be a second set of every image. The same goes for the colour-blind palette. This is part of why the options in the accessibility article were affordable.
The portrait trick
The game needs pictures of its own characters in places the main scene does not draw them: the bestiary card for an enemy, the rig select screen, the item cards and the first-contact popup the first time you meet a new host. Those are small static pictures, and writing a second set of drawing code for them would guarantee the two drifting apart.
So we do not. The portrait renderer calls the same draw functions the game uses, with one difference: it temporarily swaps the shared drawing context for the context of an offscreen canvas, draws the actor once at the size needed, and swaps back. The result is cached, so each portrait is rendered once. Anything that changes how an enemy is drawn in the game changes its portrait automatically, because there is only one drawing routine.
One more step: the guide pictures
The guide pages you are reading are plain HTML, and they cannot run the game's canvas code. They need real image files. Rather than redraw anything, a headless script opens the game, uses the same portrait renderer to produce each picture, and exports it as a PNG into the site's image folder. The bestiary page's picture of a Cinder is the game's own Cinder, drawn by the game's own code, saved to a file. The same applies to the rigs and to item icons. The current set is under 500 KB in total.
The payoff is consistency. The game, the interface and the guides cannot disagree about what something looks like, and when we redesign an enemy (as with the new Rift Weaver, which has a violet spider-loom body with silk threads and a cocoon state), the guide pictures regenerate from the same source. The cost is a small piece of plumbing: a function that maps display names to image file names, because the bestiary says "Goblin cell" and the file is called goblin.png. We learned to keep that mapping in one place after a guide page referenced an image under the wrong name.
What it costs to draw everything every frame
Drawing in code is not free, and the most useful lessons here are about performance. Canvas drawing calls are queued by the browser and executed later, so timing a draw function measures only the cost of queuing the commands. To measure real cost you have to force the browser to finish, which we do by reading a pixel back from the canvas after each draw. Done that way, a surprise appeared: when we added a richer layered background, it cost roughly nine times as much as the old one when rendered in software, as our test machines do.
Three things brought it back under control. First, layers that never change are pre-rendered once to offscreen canvases instead of being redrawn every frame. Second, parallax scrolling uses whole-pixel offsets, because blitting a bitmap at a fractional position forces the browser to resample every pixel. Third, we added a quality governor: the game watches its own frame times and drops background detail automatically if it is struggling, and there is a manual background-detail setting for players who want to choose. Software rendering in a test harness is slower than a real graphics card, so the numbers we measure are pessimistic. They have the right shape, though, and they were the argument for a governor instead of a hope.
Another consequence of code-drawn art: a frame is a lot of function calls. Every enemy that is on screen draws itself with a handful of paths, gradients and strokes, and a late wave can have hundreds. Everything that can be cached, is, and the reduce-motion option thins out particles and effects for players who want less on screen.
The honest trade-offs
This approach has real downsides, and it would be dishonest to present it as simply better.
- Style limits. Shapes, gradients and strokes give a clean, graphic look. They do not give you painted textures, hand-drawn character or photographic detail. The visual identity of the game, glowing geometric hosts on a dark map, is partly a choice and partly a constraint.
- No outside artists. A collaborator who draws well but does not write JavaScript cannot contribute directly. Every art change is a code change.
- Code grows. Our script is large because art is part of the code. More enemies, more rigs and more cosmetics all add lines, and a huge file is harder to work with and slower to parse than the same data in image files.
- Art bugs are code bugs. A wrong argument in a draw function can throw an error on every frame. That is why the main loop wraps drawing in its own error handling, so a fault while drawing does not stop the rest of the game updating. See how we test.
- Animation is expensive to author. A hand-drawn animation is a sequence of pictures. A coded one is a formula for each part. For an organic movement such as a spider's legs, getting the formula right can take much longer than drawing the frames would.
For this game, those costs are worth paying. A fast first load, parametric art, one source of truth for the game and the guides, and effortless outlines and palettes for accessibility are all things we would otherwise have had to buy back some other way. For a different project, with a large art team and a painterly style, the answer would be the opposite. The honest summary is that procedural art is a good fit when the look you want is something a few dozen well-chosen shapes can express, and a poor fit when it is not.
The result of all this is visible on the bestiary and rigs pages: every picture there was drawn by the same code that draws them during a run.