Thinking UI

Ink Bloom

The image spreads outward from a few seed points, like ink blooming in water.

Code

components/thinking-ui/image-bloom.tsx
"use client";

import { AnimatePresence, motion } from "motion/react";
import { useEffect, useRef } from "react";
import { TextShimmer } from "@/components/thinking-ui/text-shimmer";
import { useMotion } from "@/lib/motion";
import { cn } from "@/lib/utils";
import { useShader } from "@/lib/webgl";

export type ImageBloomProps = {
  /** 0–100. At 100 the bloom has covered the whole frame. */
  progress: number;
  /** The final image. Same-origin or CORS-enabled, since it is drawn into WebGL. */
  src: string;
  alt?: string;
  className?: string;
};

/* Ink dropped in water: the image spreads from three seed points along a
   domain-warped distance field, so the frontier follows slow currents and
   frays into filaments instead of growing as circles. Ahead of it, a dark
   latent with the image's colour masses and drifting caustics; on it, a
   hairline of light; just behind it, pigment that is still settling. */
const FRAGMENT = `
precision highp float;
varying vec2 vUv;
uniform sampler2D uImage;
uniform vec2 uCover;
uniform vec2 uResolution;
uniform float uTime;
uniform float uP;
uniform float uRun;

float hash(vec2 p) {
  vec3 p3 = fract(vec3(p.xyx) * 0.1031);
  p3 += dot(p3, p3.yzx + 33.33);
  return fract((p3.x + p3.y) * p3.z);
}

float noise(vec2 p) {
  vec2 i = floor(p);
  vec2 f = fract(p);
  vec2 u = f * f * (3.0 - 2.0 * f);
  return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), u.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), u.x), u.y);
}

float fbm(vec2 p) {
  float v = 0.0;
  float a = 0.5;
  for (int i = 0; i < 4; i++) {
    v += a * noise(p);
    p = p * 2.03 + 11.7;
    a *= 0.5;
  }
  return v;
}

vec3 tex(vec2 uv, float bias) {
  return texture2D(uImage, clamp((uv - 0.5) * uCover + 0.5, 0.001, 0.999), bias).rgb;
}

// Per-run jitter, so every generation blooms from slightly different places
vec2 jitter(float i) {
  return vec2(hash(vec2(uRun * 7.13, i)), hash(vec2(i, uRun * 3.71))) - 0.5;
}

void main() {
  float t = uP;
  float T = uTime;
  vec2 asp = vec2(uResolution.x / uResolution.y, 1.0);
  vec2 q = vUv * asp;
  float px = 1.0 / uResolution.y;

  // Two levels of domain warp: currents bend the bloom, eddies fray its edge
  vec2 w1 = vec2(
    fbm(q * 2.1 + uRun * 1.7 + vec2(0.0, T * 0.03)),
    fbm(q * 2.1 + uRun * 1.7 + vec2(5.2, 1.3 - T * 0.025))
  ) - 0.5;
  vec2 qw = q + w1 * 0.3;
  vec2 w2 = vec2(fbm(qw * 5.0 + 1.7), fbm(qw * 5.0 + 9.2)) - 0.5;
  qw += w2 * 0.07;

  // Three drops; the later ones land a beat behind the first
  vec2 s0 = vec2(0.40 * asp.x, 0.62) + jitter(1.0) * 0.14;
  vec2 s1 = vec2(0.72 * asp.x, 0.30) + jitter(2.0) * 0.14;
  vec2 s2 = vec2(0.22 * asp.x, 0.20) + jitter(3.0) * 0.14;
  float d = min(length(qw - s0), min(length(qw - s1) + 0.07, length(qw - s2) + 0.13));
  d += (fbm(q * 14.0 - T * 0.04) - 0.5) * 0.035;

  // Radius grows roughly with covered area; the last corners fill at the very end
  float r = 0.78 * t - 0.03 + 0.27 * pow(t, 5.0);
  float edge = d - r;
  float inside = 1.0 - smoothstep(-1.5 * px - 0.002, 1.5 * px + 0.002, edge);
  float full = smoothstep(0.97, 1.0, t);

  // Pigment that has just landed is still settling: soft, and a little bright
  float settle = 1.0 - smoothstep(0.0, 0.14, max(-edge, 0.0));
  vec3 img = tex(vUv, settle * 2.5);
  img = mix(img, img * 1.06 + 0.025, settle * 0.7);

  // The latent: dark, holding the image's colour masses, with slow caustic filaments
  vec3 ghost = tex(vUv + w1 * 0.04, 6.0);
  vec2 lq = q * 3.0 + w1 * 1.2;
  float ca = fbm(lq + vec2(T * 0.07, -T * 0.05));
  float cb = fbm(lq * 1.6 + vec2(3.1 - T * 0.04, 1.7 + T * 0.06));
  float caustic = pow(1.0 - min(abs(ca - cb) * 3.0, 1.0), 5.0);
  vec3 latent = ghost * 0.14 + 0.02;
  latent += (ghost * 0.6 + 0.35) * caustic * 0.06;
  latent += (hash(gl_FragCoord.xy + floor(T * 12.0) * 31.7) - 0.5) * 0.018;

  // Colour diffusing into the water just ahead of the frontier
  float ahead = max(edge, 0.0);
  latent = mix(latent, tex(vUv, 4.0) * 0.5, exp(-ahead * 16.0) * 0.55 * smoothstep(0.0, 0.04, t + 0.02));

  vec3 col = mix(latent, img, inside);

  // A hairline of light riding the frontier, with a faint glow either side
  float rimAmt = smoothstep(0.0, 0.04, t) * (1.0 - smoothstep(0.88, 0.99, t));
  vec3 rimCol = mix(tex(vUv, 3.0), vec3(1.0), 0.6);
  float rx = (edge - 0.0015) / (1.6 * px + 0.0015);
  float rim = exp(-rx * rx);
  float glow = exp(-abs(edge) * 45.0);
  col += rimCol * (rim * 0.55 + glow * 0.08) * rimAmt;

  col = mix(col, tex(vUv, 0.0), full);
  gl_FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);
}
`;

/** The image blooms outward from a few seed points, like ink spreading in water. */
export function ImageBloom({ progress, src, alt = "", className }: ImageBloomProps) {
  const m = useMotion();
  const p = Math.min(100, Math.max(0, progress));
  const done = p >= 100;

  const target = useRef(p / 100);
  const current = useRef(p / 100);
  const run = useRef(1);
  const reduce = m.reduce;

  const { ref, invalidate } = useShader({
    fragment: FRAGMENT,
    image: src,
    mipmaps: true,
    uniforms: ({ delta }) => {
      // Ease toward reported progress; a big step back is a new run, so snap and reseed
      const to = target.current;
      if (to < current.current - 0.25) run.current += 1;
      if (to < current.current - 0.25 || reduce) current.current = to;
      current.current += (to - current.current) * (1 - Math.exp(-delta * 4));
      if (Math.abs(to - current.current) < 0.0005) current.current = to;
      return { uP: current.current, uRun: run.current };
    },
    // The latent keeps drifting until the bloom has covered everything
    animate: () => current.current !== target.current || (!reduce && current.current < 1),
  });

  useEffect(() => {
    target.current = p / 100;
    invalidate();
  }, [p, invalidate]);

  return (
    <div
      className={cn(
        "relative aspect-[3/4] w-64 overflow-hidden rounded-card border border-card-line bg-surface-sunken shadow-card",
        className,
      )}
    >
      <div ref={ref} role="img" aria-label={alt} aria-busy={!done} className="absolute inset-0" />

      <div className="absolute inset-x-2 bottom-2 flex items-center justify-between gap-3 rounded-chip bg-[color-mix(in_oklab,var(--surface-overlay)_78%,transparent)] px-3 py-1.5 text-xs backdrop-blur-md">
        <span className="relative h-4 min-w-0 overflow-hidden">
          <AnimatePresence mode="popLayout" initial={false}>
            <motion.span key={String(done)} className="block truncate" {...m.swap()}>
              {done ? <span className="text-fg">Image ready</span> : <TextShimmer>Blooming…</TextShimmer>}
            </motion.span>
          </AnimatePresence>
        </span>
        <span className="shrink-0 font-mono tabular-nums text-fg-muted">{Math.round(p)}%</span>
      </div>
    </div>
  );
}

Motion notes

  • The frontier is a domain-warped distance field around three seeds, so it follows slow currents and frays into filaments instead of growing as circles. Each run reseeds, so no two blooms match.
  • The radius grows faster than linearly late on, so covered area tracks progress and the last corners fill right at the end instead of stalling.
  • Ahead of the frontier is a dark latent holding the image's colour masses, with drifting caustics and a faint tint where pigment is about to land. On it, a hairline of light; just behind it, a band that is still soft and bright as it settles.
  • Progress is eased on the GPU side, and drawing stops once the frame is covered. With reduced motion the latent and frontier stop drifting and the bloom follows progress directly.

Installation

  1. 1

    Install dependencies

    Terminal
    npm install motion clsx tailwind-merge
  2. 2

    Add the design tokens and motion system (once per project)

    Copy styles/tokens/*, lib/motion.tsx and lib/utils.ts, plus lib/webgl.ts from Get started.

  3. 3

    Add the components it uses

    Text Shimmer

  4. 4

    Paste the source into components/thinking-ui/image-bloom.tsx

    Copy it from the Code tab, or use Copy prompt to let your coding agent do all of this.

Usage

Example
<ImageBloom progress={job.progress} src={job.url} alt={prompt} />

Props

PropTypeDefaultDescription
progressnumber—0–100. At 100 the bloom covers the whole frame.
srcstring—Final image URL. Same-origin or CORS-enabled.
altstring—Alt text.
classNamestring—Size it here; the default is w-64 at 3:4.