Backgrounds20 / 118
Grain Ribbon
A broad ribbon of warm cream light winds and folds across a dark frame, a cool slate haze rising off it and deep black beneath, its soft edges dissolving into a fine, living film grain like a photograph pushed in the darkroom. It flows on its own, bends toward your cursor as its light gathers there, carries a ripple of light when you click, and stretches out as you scroll. On load it develops out of the grain.
WebGLWebGLFilm grainFilm grainFlowing lightFlowing lightClick rippleClick rippleGSAPGSAPReduced-motion safeReduced-motion safe
Bend the ribbonClick for a ripple
Tweak
LIVELIVEColour
#ece6dc
#38485e
#050607
Ribbon
1.00
1.00
1.00
Motion
1.00
Interactive
The ribbon bends toward the cursor, clicks ripple light along it
Usage
Follows your tweaks.
usage.tsx
import { GrainRibbon } from "@/components/inreact/grain-ribbon";
export function Hero() {
return (
<GrainRibbon
className="h-screen w-full"
>
{/* optional: your hero content, drawn above the ribbon */}
</GrainRibbon>
);
}Install with the CLI
TS + Tailwind
npx shadcn@latest add https://inreact.vercel.app/r/grain-ribbon.jsonOr copy the source
Install the dependencies, then save this as components/inreact/grain-ribbon.tsx.
terminal
npm install gsapcomponents/inreact/grain-ribbon.tsx
"use client";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import { useEffect, useRef, type ReactNode } from "react";
gsap.registerPlugin(ScrollTrigger);
/**
* Grain Ribbon: a soft ribbon of light flowing through the dark, printed in film grain.
*
* A broad ribbon of warm cream light winds across the frame, folding as it goes,
* with a cool slate haze rising off its upper side and deep black beneath. Its
* edges never cut: they dissolve into a fine, living film grain that is heaviest
* in the half-tones, like a photograph pushed in the darkroom.
*
* It is always moving: the ribbon flows and folds slowly and the grain shimmers.
* It reacts: the ribbon bends toward your cursor and its light gathers there; a
* click sends a ripple of light running along it; scrolling stretches and drifts
* it. On load it develops out of the grain like a print in the tray.
*
* <GrainRibbon className="h-screen w-full">
* <YourHero />
* </GrainRibbon>
*
* WebGL: one shader (a flowing curve, soft light, slate haze, grain shaped by tone).
* GSAP drives every motion. Pauses off-screen; reduced motion shows a still frame;
* a CSS gradient without WebGL.
*/
export interface GrainRibbonProps {
/** Colour of the ribbon of light. */
light?: string;
/** Colour of the haze above it. */
haze?: string;
/** Background colour. */
background?: string;
/** Width of the ribbon. */
width?: number;
/** Amount of film grain. */
grain?: number;
/** Brightness. */
intensity?: number;
/** Speed of the flow. */
speed?: number;
/** Cursor bend, click ripple. */
interactive?: boolean;
/** Scroll container if the page does not scroll the window. */
scroller?: Element | null;
/** Foreground content, drawn above the ribbon. */
children?: ReactNode;
className?: string;
}
const VERT = `attribute vec2 aPos; void main(){ gl_Position = vec4(aPos, 0.0, 1.0); }`;
const FRAG = `
precision highp float;
uniform vec2 uRes, uMouse, uRipC;
uniform float uTime, uFlow, uIn, uWidth, uGrain, uIntensity, uHover, uRip, uRipAmp, uStretch, uDrift;
uniform vec3 uLight, uHaze, uBg;
// a hash that stays random at large coordinates, so the grain never shows a pattern
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 vnoise(vec2 p) {
vec2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
return mix(mix(hash(i), hash(i + vec2(1, 0)), f.x), mix(hash(i + vec2(0, 1)), hash(i + vec2(1, 1)), f.x), f.y);
}
// the ribbon's centre line: a slow S that folds, flowing over time, bent toward the cursor
float path(float x, float seed) {
float f = uFlow + seed * 7.0;
// a nested phase folds the curve back on itself in places, like cloth, instead of a plain sine
float fold = sin(x * 2.1 / uStretch + f * 0.21 + seed) * 1.4;
float y = 0.42 - seed * 0.18
+ 0.12 * sin(x * 2.6 / uStretch - f * 0.35 + 0.8)
+ 0.08 * sin(x * 4.4 / uStretch - f * 0.42 + fold)
+ 0.03 * sin(x * 9.1 - f * 0.8)
+ uDrift;
float dx = x - uMouse.x;
y += (uMouse.y - y) * 0.45 * exp(-dx * dx / 0.06) * uHover;
return y;
}
void main() {
float H = uRes.y;
vec2 p = gl_FragCoord.xy / H;
float x = p.x;
// distance to the curve, corrected for its slope so the ribbon keeps an even width
// a gentle organic warp, so the edges are never perfectly smooth
vec2 wp = p + (vec2(vnoise(p * 3.0 + uFlow * 0.08), vnoise(p * 3.0 + 9.0 - uFlow * 0.07)) - 0.5) * 0.05;
x = wp.x;
float y0 = path(x, 0.0);
float e = 0.002;
float slope = (path(x + e, 0.0) - path(x - e, 0.0)) / (2.0 * e);
float d = (wp.y - y0) / sqrt(1.0 + slope * slope);
// the ribbon swells and thins along its length, its edge breathing irregularly
float w = uWidth * (0.075 + 0.035 * sin(x * 3.7 + uFlow * 0.3) + 0.02 * sin(x * 8.0 - uFlow * 0.45));
w *= 0.85 + 0.3 * vnoise(wp * 5.0 + uFlow * 0.15);
float core = exp(-pow(d / (w * 1.35), 2.0));
// a softer shoulder, wider on the upper side where the light spills
float shoulder = exp(-pow(d / (w * (d > 0.0 ? 3.6 : 2.2)), 2.0)) * 0.3;
// the slate haze rising off the ribbon, and darkness beneath it
float above = exp(-max(d, 0.0) / 0.22) * smoothstep(-0.02, 0.08, d) * 0.42;
float below = smoothstep(0.0, -0.25, d);
// a click's ripple of light running along the ribbon
float along = length(p - uRipC);
float ring = exp(-pow((along - uRip * 1.6) / 0.07, 2.0)) * uRipAmp;
// light gathers under the cursor
vec2 dm = p - uMouse;
float near = exp(-dot(dm, dm) / 0.04) * uHover;
// the light pools in places along the ribbon and thins in others, drifting with the flow
float pool = 0.7 + 0.45 * vnoise(vec2(x * 2.2 - uFlow * 0.25, 3.0));
float v = core * pool * (0.8 + 0.2 * near + 0.45 * ring) + shoulder * (0.8 + 0.3 * pool + near * 0.4);
// a faint second ribbon drifting behind, for depth
float y1 = path(x * 0.9 + 0.4, 1.0);
float d1 = wp.y - y1;
v += exp(-pow(d1 / (w * 2.6), 2.0)) * 0.16 * (1.0 - core);
float hz = above * (1.0 - below);
vec3 col = uBg + uHaze * hz + uLight * v;
col *= uIntensity;
// a soft shoulder instead of clipping, so the ribbon stays cream rather than burning white
col = col / (1.0 + max(col - 0.8, 0.0) * 1.6);
// film grain, heaviest in the half-tones; it shimmers a few times a second, like grain in a projector
float lum = dot(col, vec3(0.3, 0.55, 0.15));
float g = hash(floor(gl_FragCoord.xy) + vec2(floor(uTime * 12.0) * 37.0, 0.0)) - 0.5;
float mid = 4.0 * lum * (1.0 - min(lum, 1.0));
col += g * uGrain * (0.025 + 0.2 * mid * mid);
// on load the image develops out of the grain
float dev = smoothstep(0.0, 1.0, uIn * 1.3 - hash(floor(gl_FragCoord.xy * 0.5)) * 0.3);
col = mix(uBg + g * 0.08 * uGrain, col, dev);
gl_FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);
}`;
const isReducedMotion = () =>
typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
function rgb(hex: string): [number, number, number] {
let h = hex.replace("#", "");
if (h.length === 3) h = h.replace(/./g, (c) => c + c);
const n = parseInt(h, 16) || 0;
return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255];
}
export function GrainRibbon({
light = "#ece6dc",
haze = "#38485e",
background = "#050607",
width = 1,
grain = 1,
intensity = 1,
speed = 1,
interactive = true,
scroller,
children,
className,
}: GrainRibbonProps) {
const hostRef = useRef<HTMLDivElement>(null);
const props = useRef({ light, haze, background, width, grain, intensity, speed, interactive });
const redraw = useRef<(() => void) | null>(null);
useEffect(() => {
props.current = { light, haze, background, width, grain, intensity, speed, interactive };
redraw.current?.();
}, [light, haze, background, width, grain, intensity, speed, interactive]);
useEffect(() => {
const host = hostRef.current;
if (!host) return;
if (getComputedStyle(host).position === "static") host.style.position = "relative";
const still = isReducedMotion();
const canvas = document.createElement("canvas");
canvas.setAttribute("aria-hidden", "true");
canvas.style.cssText = "position:absolute;inset:0;width:100%;height:100%;display:block;touch-action:pan-y;";
host.prepend(canvas);
const gl = canvas.getContext("webgl", { alpha: false, antialias: false });
if (!gl) {
const p = props.current;
host.style.background = `linear-gradient(160deg, ${p.background} 20%, ${p.haze} 45%, ${p.light} 58%, ${p.background} 80%)`;
canvas.remove();
return;
}
const prog = gl.createProgram()!;
for (const [type, src] of [
[gl.VERTEX_SHADER, VERT],
[gl.FRAGMENT_SHADER, FRAG],
] as const) {
const sh = gl.createShader(type)!;
gl.shaderSource(sh, src);
gl.compileShader(sh);
if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) console.error(gl.getShaderInfoLog(sh));
gl.attachShader(prog, sh);
}
gl.linkProgram(prog);
if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) console.error(gl.getProgramInfoLog(prog));
gl.useProgram(prog);
const buf = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buf);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
const aPos = gl.getAttribLocation(prog, "aPos");
gl.enableVertexAttribArray(aPos);
gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0);
const U = Object.fromEntries(
["uRes", "uMouse", "uRipC", "uTime", "uFlow", "uIn", "uWidth", "uGrain", "uIntensity", "uHover", "uRip", "uRipAmp", "uStretch", "uDrift", "uLight", "uHaze", "uBg"].map((n) => [n, gl.getUniformLocation(prog, n)]),
) as Record<string, WebGLUniformLocation | null>;
let w = 1, h = 1, dpr = 1;
const resize = () => {
// grain wants real pixels, so render at device resolution
dpr = Math.min(window.devicePixelRatio || 1, 2);
w = Math.max(1, host.clientWidth);
h = Math.max(1, host.clientHeight);
canvas.width = Math.round(w * dpr);
canvas.height = Math.round(h * dpr);
gl.viewport(0, 0, canvas.width, canvas.height);
};
resize();
// ---- state: every value GSAP moves (height units, y up) ----
const s = { in: still ? 1 : 0, mx: 0, my: 0.4, hover: 0, rip: 0, ripAmp: 0, rx: 0, ry: 0, stretch: 1, drift: 0, sway: 0 };
let time = still ? 4 : 0, flow = still ? 4 : 0;
const draw = () => {
const p = props.current;
gl.uniform2f(U.uRes, canvas.width, canvas.height);
gl.uniform2f(U.uMouse, s.mx, s.my);
gl.uniform2f(U.uRipC, s.rx, s.ry);
gl.uniform1f(U.uTime, time);
gl.uniform1f(U.uFlow, flow);
gl.uniform1f(U.uIn, s.in);
gl.uniform1f(U.uWidth, p.width);
gl.uniform1f(U.uGrain, p.grain);
gl.uniform1f(U.uIntensity, p.intensity);
gl.uniform1f(U.uHover, s.hover);
gl.uniform1f(U.uRip, s.rip);
gl.uniform1f(U.uRipAmp, s.ripAmp);
gl.uniform1f(U.uStretch, s.stretch * Math.max(1, Math.pow(w / h, 0.5)));
gl.uniform1f(U.uDrift, s.drift + s.sway);
gl.uniform3fv(U.uLight, rgb(p.light));
gl.uniform3fv(U.uHaze, rgb(p.haze));
gl.uniform3fv(U.uBg, rgb(p.background));
gl.drawArrays(gl.TRIANGLES, 0, 3);
};
const ro = new ResizeObserver(() => {
resize();
draw();
});
ro.observe(host);
if (still) {
draw();
redraw.current = draw;
return () => {
redraw.current = null;
ro.disconnect();
canvas.remove();
gl.getExtension("WEBGL_lose_context")?.loseContext();
};
}
// ---- on load: the image develops out of the grain ----
const intro = gsap.to(s, { in: 1, duration: 2.6, ease: "power2.inOut", delay: 0.1 });
// ---- idle: the ribbon rises and falls on a slow swell ----
const sway = gsap.to(s, { sway: 0.05, duration: 5.5, ease: "sine.inOut", yoyo: true, repeat: -1 });
// ---- cursor: the ribbon bends toward it and its light gathers there ----
const mxTo = gsap.quickTo(s, "mx", { duration: 0.9, ease: "power3" });
const myTo = gsap.quickTo(s, "my", { duration: 0.9, ease: "power3" });
const hoverTo = gsap.quickTo(s, "hover", { duration: 1, ease: "power2.inOut" });
const local = (e: PointerEvent) => {
const r = host.getBoundingClientRect();
return { x: (e.clientX - r.left) / r.height, y: (r.bottom - e.clientY) / r.height };
};
let first = true;
const move = (e: PointerEvent) => {
if (!props.current.interactive) return;
const p = local(e);
if (first) {
gsap.set(s, { mx: p.x, my: p.y });
first = false;
}
mxTo(p.x);
myTo(p.y);
hoverTo(1);
};
const leave = () => {
first = true;
hoverTo(0);
};
// ---- click: a ripple of light runs along the ribbon ----
const down = (e: PointerEvent) => {
if (!props.current.interactive) return;
const p = local(e);
s.rx = p.x;
s.ry = p.y;
gsap.killTweensOf(s, "rip,ripAmp");
gsap.fromTo(s, { rip: 0 }, { rip: 1, duration: 2, ease: "power2.out" });
gsap.timeline().fromTo(s, { ripAmp: 0 }, { ripAmp: 1, duration: 0.25, ease: "sine.out" }).to(s, { ripAmp: 0, duration: 1.8, ease: "sine.inOut" });
};
host.addEventListener("pointermove", move);
host.addEventListener("pointerleave", leave);
host.addEventListener("pointerdown", down);
// ---- scroll: the ribbon stretches out and drifts up the frame ----
const stretchTo = gsap.quickTo(s, "stretch", { duration: 1.3, ease: "power3" });
const driftTo = gsap.quickTo(s, "drift", { duration: 1.3, ease: "power3" });
const st = ScrollTrigger.create({
trigger: host,
scroller: scroller ?? undefined,
start: "top bottom",
end: "bottom top",
onUpdate: (self) => {
stretchTo(1 + Math.max(0, self.progress - 0.5) * 0.6);
driftTo((self.progress - 0.5) * 0.2);
},
});
// ---- loop, only while on screen ----
let visible = true;
let prev = performance.now();
const loop = () => {
const now = performance.now();
const dt = Math.min(0.05, (now - prev) / 1000);
prev = now;
if (!visible) return;
time += dt;
flow += dt * props.current.speed;
draw();
};
const io = new IntersectionObserver(([en]) => {
visible = en.isIntersecting;
prev = performance.now();
});
io.observe(host);
gsap.ticker.add(loop);
return () => {
gsap.ticker.remove(loop);
intro.kill();
sway.kill();
st.kill();
io.disconnect();
ro.disconnect();
host.removeEventListener("pointermove", move);
host.removeEventListener("pointerleave", leave);
host.removeEventListener("pointerdown", down);
gsap.killTweensOf(s);
canvas.remove();
gl.getExtension("WEBGL_lose_context")?.loseContext();
};
}, [scroller]);
return (
<div ref={hostRef} className={["isolate overflow-hidden", className].filter(Boolean).join(" ")} style={{ background }}>
{children && <div className="relative z-10 h-full">{children}</div>}
</div>
);
}
One prompt, any AI agent
Paste into Claude Code, Codex, Cursor or Windsurf. It carries your tweaks and the full source, so the agent installs it exactly.
Props
11 propslightstringColour of the ribbon of light.default "#ece6dc"default "#ece6dc"hazestringColour of the haze above it.default "#38485e"default "#38485e"backgroundstringBackground colour.default "#050607"default "#050607"widthnumberWidth of the ribbon.default 1default 1grainnumberAmount of film grain.default 1default 1intensitynumberBrightness.default 1default 1speednumberSpeed of the flow.default 1default 1interactivebooleanCursor bend, click ripple.default truedefault truescrollerElement | nullScroll container, if the page does not scroll the window.childrenReactNodeForeground content, drawn above the ribbon.classNamestringSize it, e.g. h-screen w-full.