· 2 min read

How the Kintsugi hero breaks and rebuilds

The hero is one photograph, a cut-out and a depth map, driven by native scroll. Here is how scroll becomes a story position, and what happens when WebGL is not an option.

  • WebGL
  • three.js
  • scroll

The hero on this site is a single photograph of me that breaks into pieces as you scroll, and then gets built again with gold seams. I wanted the whole thing to stay a normal web page, so the first rule was: never take over the scroll.

Scroll becomes a story position

The page has a list of beats, each with a weight in content/story.json. The conductor in src/ui/conductor.js only reads where the page is. It takes the middle of the viewport, finds which beat that point sits in, and returns a number I call the story position, measured in beat weights. It never calls preventDefault and never moves the scroll itself.

js
const c = window.scrollY + window.innerHeight / 2;
if (c <= boxes[0].top) return 0;
for (let i = 0; i < boxes.length; i++) {
  const b = boxes[i];
  if (c < b.top + b.h) return starts[i] + weights[i] * clamp((c - b.top) / b.h, 0, 1);
}
return total;

Because scrolling stays native, momentum, keyboard, find-in-page and anchor links all keep working. The stage just chases the number: each frame it eases its current position toward the target, so a jumpy wheel still produces a smooth animation.

A cut-out, a street plate and a depth map

The photo is split into three assets. A matte cuts me out. A plate holds the street behind me. A depth map, from the phone that took the picture, says how far each pixel is. In figure.js the matte decides which triangles of a jittered grid touch my body, and the depth map gives them a little relief. In backdrop.js the street becomes a dense plane pushed back along each view ray by its depth, so the first frame is exactly the photograph and depth only shows once the camera moves.

The triangles are grouped into cells. An edge between two cells becomes a seam, and the seams are what later turn gold. Everything the shaders need is a pure function of a few uniforms such as crack, break, dust and build, so the same story position always gives the same frame, forwards or backwards.

When it cannot run

If the visitor asks for reduced motion, or the browser has no WebGL 2, the stage is never created and the page keeps the still photo. If the stage throws while loading, the same fallback is used. There is also a note on the page that says why, and a way to play the animation anyway.

js
const noGL = !hasWebGL2();
if ((reduceMotion.matches && !forceMotion) || noGL) {
  root.classList.add('is-static');
  explainStatic(noGL ? 'gl' : 'motion');
  return;
}

I have only tested this on a few machines, including a software renderer, so I would not call it battle-tested. The stage does lower its pixel ratio when frames run slow, but I have not measured how well that works on low-end phones.