A koi's markings are easy to draw on a still silhouette. The harder part is keeping them attached to the same fish when it bends away from you. In Koiworld, a small koi breeding game I've been building for about a year, every fish is drawn at runtime with Canvas 2D: no sprites, no image files. This post walks through how the spine, the swim and the markings fit together.
The spine follows the head
Each koi has a physics spine: a chain of 27 links that follows the head. On every update, each link points back toward the link in front of it, but it may only bend so far relative to that link:
if (bend > MAX_LINK_BEND) linkAngle = prevAngle + MAX_LINK_BEND; else if (bend < -MAX_LINK_BEND) linkAngle = prevAngle - MAX_LINK_BEND; The link also turns toward that target gradually instead of snapping to it in one frame. Together, these make a sharp turn curve the whole body instead of folding it at a single joint.
The swim wave is added sideways
On top of the spine, a lateral wave travels down the body. It gets faster and wider as the fish speeds up, up to a cap. Speed is measured in body radii, so big and small koi share the same tuning:
const speedInRadii = Math.sqrt(speedSquared) / Math.max(1, this.R); this.wavePhase += dt * (2.5 + Math.min(4, speedInRadii * 0.6)); this.waveAmp = 0.15 + Math.min(0.15, speedInRadii * 0.03); The wave is applied along the body's normal and scaled by the square of the position along the body (q runs from 0 at the head to 1 at the tail), so the tail swings much more than the head:
const w = Math.sin(this.wavePhase - q * 5) * this.waveAmp * R * 0.45 * q * q; The outline clips the markings
From the bent centerline and a body-width profile, I place points on both sides and join them with quadratic curves, using each point as the control point and ending each segment at the midpoint to the next one:
ctx.quadraticCurveTo(xs[i] + dx, ys[i] + dy, (xs[i] + xs[j]) / 2 + dx, (ys[i] + ys[j]) / 2 + dy); After filling the body, the renderer clips to the same path and draws the markings inside it. The spots are built from scale-sized cells laid out in fish-local coordinates, so they bend with the body, and the clip keeps the paint inside the silhouette:
ctx.save(); ctx.clip(); // 2. Patterns, bent along the body, built from scale-sized hexagons so their edges step like the scales. if (spots && spots.length > 0) this.drawScaleSpots(ctx, spots); A small inheritance model supplies the inputs
The renderer doesn't know anything about genetics; it just gets a different set of inputs for each fish. Those come from a deliberately simplified Mendelian model. For two-allele traits, each parent passes on a copy with a probability based on how many copies it carries, and a small trait-specific mutation can add one:
const gamete = (genetics: KoiGenetics) => Math.random() < getTraitCopies(genetics, trait) / 2 ? 1 : 0; let copies = gamete(genetics1) + gamete(genetics2); if (Math.random() < ALLELE_TRAIT_MUTATION_CHANCE[trait]) copies = Math.min(2, copies + 1); Some traits are recessive: one copy is a hidden carrier and two change the look (Doitsu, for example, replaces the small scales with a band of large hexagonal scales along the back). Others are incompletely dominant, like long fins that get longer with two copies. Spot colors and shapes are inherited, but each fry gets new spot positions, so siblings look related without being identical. When you select two parents, the game gives a one-line hint about a trait they could pass on.
Try it
You can open the pond in a browser as a guest, with no sign-up and no ads: koigarden.shop. There is also an Android version. If you follow a marking near the tail while a fish turns, I'd love to hear where it feels like part of the fish and where it still looks painted on.
Disclosure: this post was written and published by AI agents helping me promote the game, using code quoted directly from its source. I also used AI coding assistants while building the game.


