Marbled paper backgrounds on a canvas, without WebGL

Draw a soft marbled background in plain canvas 2D: value noise folded through itself, combed into veins, drawn small and scaled up, capped at 12 frames a second.

6 min read

The real component. Click the picture to open it live and try it before you read how it was built.

Our product analytics page has one source of colour: a marbled paper ground behind the dashboard. It is drawn live on a 2D canvas, with no images and no WebGL.

Noise, folded through itself

Start with value noise: random heights on a grid, blended smoothly. Feed the output back in as an offset (domain warping) twice, and the smooth blobs fold into swirls that look like ink on water. A sine of the result combs those swirls into veins.

src/registry/landing-pages/inlet/marble-field.tsx

"use client";

import { useRef } from "react";
import { useCanvasLoop, type CanvasFrame } from "@/hooks/use-canvas-loop";
import { valueNoise } from "@/lib/noise";
import { cn } from "@/lib/utils";

// The marbling's inks, kept pale so the ground stays calm behind the product: soft cobalt, pale blues and paper,
// one hue only.
const INKS = [
  [96, 130, 228],
  [160, 190, 246],
  [236, 241, 251],
  [196, 214, 250],
  [124, 158, 236],
];
// The field is drawn small and scaled up, which keeps it soft and costs almost nothing.
const CELL = 4;
const SCALE = 0.005;
const VEINS = 8;
const DRIFT = 0.03;
const FPS = 12;

// A colour along the inks for a value in 0..1, blended between neighbours.
function inkAt(value: number): number[] {
  const at = Math.min(Math.max(value, 0), 0.999) * (INKS.length - 1);
  const from = INKS[Math.floor(at)];
  const to = INKS[Math.floor(at) + 1];
  const mix = at - Math.floor(at);
  return from.map((channel, index) => channel + (to[index] - channel) * mix);
}

// Marbled paper: noise pushed through itself twice, so the inks fold into veins like paint combed on water.
function marble(x: number, y: number, time: number): number {
  const warpX = valueNoise(x + time, y);
  const warpY = valueNoise(x + 5.2, y + 1.3 - time);
  const foldX = valueNoise(x + 3.5 * warpX + 1.7, y + 3.5 * warpY + 9.2);
  const foldY = valueNoise(x + 3.5 * warpX + 8.3, y + 3.5 * warpY + 2.8);
  const pool = valueNoise(x + 3 * foldX, y + 3 * foldY);
  // Fold the pooled value back and forth so each pool splits into fine combed veins.
  return 0.5 + 0.5 * Math.sin(pool * VEINS + foldX * 5);
}

function draw({ context, width, height, time }: CanvasFrame) {
  for (let y = 0; y < height; y += CELL) {
    for (let x = 0; x < width; x += CELL) {
      const [red, green, blue] = inkAt(marble(x * SCALE, y * SCALE, time * DRIFT));
      context.fillStyle = `rgb(${red} ${green} ${blue})`;
      context.fillRect(x, y, CELL, CELL);
    }
  }
}

// The page's one material: marbled endpaper in soft blues, drawn in code on a canvas, drifting very slowly
// (still for reduced motion), softened and given paper grain. Product windows sit on it.
export function MarbleField({ className }: { className?: string }) {
  const canvasRef = useRef<HTMLCanvasElement>(null);
  useCanvasLoop(canvasRef, draw, FPS);
  return (
    <div aria-hidden className={cn("absolute inset-0 overflow-hidden bg-blue-700", className)}>
      <canvas ref={canvasRef} className="size-full scale-105 blur-sm" />
      <div className="absolute inset-0 bg-grain opacity-25 mix-blend-overlay" />
    </div>
  );
}
The marble field

Draw small, scale up

Computing noise for every pixel would be slow. The field is drawn in 4-pixel cells and softened with a light CSS blur, which hides the blocks and costs almost nothing. A grain overlay at low opacity adds paper texture.

Cheap to run

  • A shared hook caps the loop at 12 frames a second and pauses it off screen and in hidden tabs.
  • Reduced motion draws one still frame.
  • Pale inks keep it calm; our first version was too strong and we toned it down.

More articles