Tech

Growing a meadow from a seed

The hill at the top of my home page has about two thousand blades of grass on it. I didn’t draw any of them. A small function grows the whole meadow when the site builds, and the browser just receives plain SVG.

I wanted three things from it:

  • No work in the browser. Growing grass on a phone every time someone visits is a waste of their battery.
  • The same meadow every time. If a blade moves between two builds, the diff is noise and the page shifts for no reason.
  • Something that still feels drawn. Uneven and a bit messy, with bare patches and clumps.

Randomness you can repeat

Math.random() gives a different answer on every run, so it can’t give me a stable meadow. Instead the generator uses a tiny seeded generator called mulberry32. Give it the same seed and it hands back the same sequence of numbers, forever.

export function createRng(seed: string | number): Rng {
  let state = hashSeed(seed);
  return () => {
    state = (state + 0x6d2b79f5) | 0;
    let t = Math.imul(state ^ (state >>> 15), 1 | state);
    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
}

Each hill gets its own stream, seeded with its layer number. That mattered more than I expected: when I made the far hill a little taller, the nearer hills didn’t reshuffle, because they never shared numbers with it.

Five hills, far to near

The meadow is five layers stacked from the horizon down. Far hills are pale, and their grass is a single short pencil stroke. Near hills are darker, and their blades are proper tapered shapes, some with an outline and a few with a light edge where the sun catches them.

Each blade is one path with two curved sides that meet at the tip:

export function fillBlade({ x, y, width, height, lean, bend }: BladeShape): string {
  const half = width / 2;
  const cx = lean * 0.35 + bend;
  const cy = -height * 0.55;
  return (
    `M${n(x - half)} ${n(y)}` +
    `q${n(cx - width * 0.32 + half)} ${n(cy)} ${n(lean + half)} ${n(-height)}` +
    `q${n(cx + width * 0.32 - lean)} ${n(cy + height)} ${n(half - lean)} ${n(height)}z`
  );
}

Blades grow in tufts of 8 to 20, and the tufts follow a bit of smooth noise along the hill, so some stretches are lush and others are nearly bare. Tufts on the left lean toward lighter greens, which gives the hill a sense of light coming from somewhere.

Making it sway

Animating two thousand blades one by one would be a lot to ask of a phone, so nothing smaller than a tuft moves on its own. Each tuft rotates a degree or two around its base, and far hills sway more slowly than near ones, which gives a little parallax for free.

.meadow .tf {
  animation: meadow-sway var(--dur) var(--ease-sway) calc(var(--p) * var(--dur) * -1) infinite
    alternate;
}

@keyframes meadow-sway {
  from {
    transform: rotate(calc(var(--amp) * -1));
  }
  to {
    transform: rotate(var(--amp));
  }
}

Every fifteen seconds or so a gust leans whole strips of the hill with skewX, one strip after another from left to right, so you can watch it travel. Only transform changes, which keeps the work on the compositor. If your system asks for reduced motion, the meadow holds still.

What I’d change

The light is faked with color weighting, and up close you can tell. One day I’d like the sun (it follows your local time) to actually shade the hill. For now I like that it’s a little wrong. Most of my drawings are.