cat ./work/geometric-crystals.md

Geometric Crystals

A generative-art crystal engine — first written in p5.js in 2020, rewritten in 2026 as the dependency-free, seeded engine behind this site's hero.

Role
Solo — art and code
Type
Personal project
Year
2020
Status
live
  • TypeScript
  • Canvas 2D
  • p5.js
  • Vitest
Cover card: Geometric Crystals — seeded crystals

The problem

I wanted a generative piece that felt like mine: structured but surprising, closer to mineral than to noise. In 2020 I built it in p5.js inside a Gatsby site. In 2026 it became the visual identity of this portfolio, which raised a new problem: p5 is roughly a megabyte of JavaScript for a background.

What I built

Each crystal stacks up to seven layer types: outline shape, centred shape, circles, simple lines, dotted lines, a ring of shapes and stepped hexagons. Each layer is drawn or skipped by a weighted dice roll, then given a colour from the palette. A grid of these fills the canvas, and no two are alike.

For this site I rewrote the engine:

  • Pure and seeded. Instead of drawing through p5’s transform stack, each layer now returns draw operations in absolute coordinates. Give it the same seed and you get the same crystals, every time.
  • No dependencies. The renderer is about 30 lines of plain Canvas 2D, a few kilobytes instead of a megabyte.
  • Tested. Unit tests cover determinism, geometry (hexagon vertices sit exactly on the radius) and bounds (nothing escapes the canvas, even on a phone).
  • Polite. It animates only while it’s on screen, draws once and stops if you prefer reduced motion, and falls back to a pre-rendered image without JavaScript.

Hard parts

Porting behaviour, not just code. p5’s random stream and transform state were tangled together, so I made every layer consume its randomness whether it draws or not. That keeps the output stable when the draw rules change.

Outcome

It’s the hero at the top of the home page. Hit ↻ new crystals to roll a new seed. The same engine renders every cover image on this site.