Backgrounds04 / 118
Cloud Veil
You look down through great masses of drifting cloud at a living tropical coast: the sea ripples and glints, the jungle sways as gusts roll over it, cloud shadows and mist drift past and gulls fly below. The clouds behave like air: your cursor drags and thins them into soft windows, gusts of wind sweep across on their own, a click blows a gust, and scrolling carries you down through the layer as it parts.
WebGL cloudsWebGL cloudsLiving water and treesLiving water and treesGSAP ScrollTriggerGSAP ScrollTriggerCursor windCursor windReduced-motion safeReduced-motion safe
Push the clouds asideScroll to descend
Tweak
LIVELIVEClouds
1.00
1.00
#ffffff
#5f9ae6
Cursor
150px
3.0s
Gusts
Wind sweeps across on its own
World
1.00
9
Usage
Follows your tweaks.
usage.tsx
import { CloudVeil } from "@/components/inreact/cloud-veil";
export function Hero() {
return (
<CloudVeil
image="/your-photo.jpg"
className="h-[250vh] w-full"
>
{/* optional: your hero content, pinned above the clouds */}
</CloudVeil>
);
}Install with the CLI
TS + Tailwind
npx shadcn@latest add https://inreact.vercel.app/r/cloud-veil.jsonOr copy the source
Install the dependencies, then save this as components/inreact/cloud-veil.tsx.
terminal
npm install gsapcomponents/inreact/cloud-veil.tsx
"use client";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import { useEffect, useRef, type ReactNode } from "react";
gsap.registerPlugin(ScrollTrigger);
/**
* Cloud Veil: a sky of heavy cloud over a living world.
*
* You look down through great masses of drifting cloud at a coastline (any
* photo you pass in), and the world below is alive: water ripples and
* glints in the sun, the forest canopy sways as gusts of wind roll across
* it, cloud shadows slide over land and sea, thin mist drifts, and gulls
* fly beneath the clouds, each with a shadow on the ground.
*
* The clouds behave like air: your cursor drags and swirls them along its
* path and thins them, so you only glimpse the world through the haze
* before they close again. A click blows a gust outward. Scrolling carries
* you down through the layer as it parts, until the whole scene is open.
*
* <CloudVeil image="/coast.jpg" className="h-[250vh] w-full">
* <YourHero />
* </CloudVeil>
*
* Make it taller than the screen: the view stays pinned while you scroll
* through it. Two WebGL layers (world, clouds) and a canvas of birds; GSAP
* drives the reveal, the scroll descent and every cursor motion. The image
* must allow CORS (same origin, or a CDN like Unsplash). Pauses off-screen;
* reduced motion shows a still, partly open sky.
*/
export interface CloudVeilProps {
/** The world under the clouds: a photo, ideally seen from above. Must be same-origin or CORS-enabled. */
image: string;
/** Describes the image for screen readers. */
alt?: string;
/** How thick the clouds are before you clear them (0.4 to 1.3). */
cover?: number;
/** Reach of your cursor's wind, in px. */
brush?: number;
/** Seconds the clouds take to settle back after you disturb them. */
heal?: number;
/** Number of birds below the clouds. */
birds?: number;
/** Speed of the clouds, wind and water. */
speed?: number;
/** How alive the world is: water, wind in the trees, shadows, mist (0 to 2). */
life?: number;
/** Gusts of wind that sweep across on their own, clearing a path for a while. */
gusts?: boolean;
/** Colour of the sunlit side of the clouds. */
light?: string;
/** Colour of the shaded side of the clouds. */
shade?: string;
/** Scroll container if the page does not scroll the window. */
scroller?: Element | null;
/** Foreground content, pinned above the clouds. */
children?: ReactNode;
className?: string;
}
const VERT = `attribute vec2 aPos; void main(){ gl_Position = vec4(aPos, 0.0, 1.0); }`;
const NOISE = `
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
float noise(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, 0.0)), f.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);
}
float fbm(vec2 p) {
float v = 0.0, a = 0.5;
mat2 m = mat2(1.6, 1.2, -1.2, 1.6);
for (int i = 0; i < 5; i++) { v += a * noise(p); p = m * p; a *= 0.5; }
return v;
}`;
// ---- the world: the photo, brought to life ----
const WORLD = `
precision highp float;
uniform vec2 uRes, uImg, uPan;
uniform float uTime, uZoom, uLife;
uniform sampler2D uTex;
${NOISE}
vec2 cover(vec2 uv) {
float ra = uRes.x / uRes.y, ia = uImg.x / uImg.y;
vec2 s = ra > ia ? vec2(1.0, ia / ra) : vec2(ra / ia, 1.0);
return (uv - 0.5) * s / uZoom + 0.5 + uPan;
}
void main() {
vec2 uv = gl_FragCoord.xy / uRes;
uv.y = 1.0 - uv.y;
float t = uTime;
vec2 tuv = cover(uv);
// read the land: water is blue-green, canopy is green, from a slightly blurred sample
float px = 2.5 / uImg.x;
vec3 b = (texture2D(uTex, tuv) + texture2D(uTex, tuv + vec2(px, 0.0)) + texture2D(uTex, tuv - vec2(px, 0.0))
+ texture2D(uTex, tuv + vec2(0.0, px)) + texture2D(uTex, tuv - vec2(0.0, px))).rgb / 5.0;
float water = smoothstep(0.05, 0.16, b.b - b.r) * smoothstep(-0.12, 0.02, b.b - b.g + 0.06);
float leaf = smoothstep(0.015, 0.09, b.g - max(b.r, b.b)) * (1.0 - water);
// water: the surface ripples and refracts what is under it
vec2 wq = tuv * vec2(46.0, 38.0);
vec2 ripple = vec2(fbm(wq + vec2(t * 0.35, t * 0.2)), fbm(wq * 1.1 - vec2(t * 0.28, -t * 0.22) + 7.0)) - 0.5;
// trees: gusts of wind roll across the canopy and the leaves sway
float gust = 0.5 + 0.5 * sin(dot(tuv, vec2(7.0, 4.0)) - t * 1.1 + fbm(tuv * 3.0) * 4.0);
gust *= gust;
vec2 sway = vec2(sin(t * 2.3 + tuv.y * 90.0 + tuv.x * 30.0), cos(t * 1.9 + tuv.x * 80.0)) * (0.25 + gust);
vec2 off = ripple * 0.0055 * water + sway * 0.0011 * leaf;
vec3 col = texture2D(uTex, tuv + off * uLife).rgb;
// sunlight shimmering across the water in soft moving sheets, never specks
float sheen = fbm(tuv * vec2(9.0, 7.0) + ripple * 1.5 + vec2(t * 0.12, -t * 0.08));
col += vec3(0.85, 0.97, 1.0) * smoothstep(0.5, 0.8, sheen) * 0.07 * water * uLife;
// the swell: soft bands of light moving over the water
col *= 1.0 + water * 0.05 * uLife * sin(dot(tuv, vec2(30.0, 18.0)) - t * 1.4 + ripple.x * 6.0);
// leaves catch the light as each gust passes
col *= 1.0 + leaf * 0.07 * uLife * (gust - 0.4);
// cloud shadows sliding over land and sea
float cs = smoothstep(0.48, 0.72, fbm(tuv * 2.2 + vec2(t * 0.025, t * 0.012)));
col *= 1.0 - cs * 0.3 * min(uLife, 1.2);
// thin mist drifting low over everything
float mist = smoothstep(0.52, 0.85, fbm(uv * vec2(1.8, 2.6) + vec2(t * 0.05, t * 0.01)));
col = mix(col, vec3(0.9, 0.94, 0.97), mist * 0.08 * min(uLife, 1.5));
gl_FragColor = vec4(col, 1.0);
}`;
// ---- the clouds: big masses, dragged and thinned by your cursor's wind ----
const CLOUDS = `
precision highp float;
uniform vec2 uRes;
uniform float uTime, uCover, uClear, uReveal;
uniform sampler2D uWind;
uniform vec3 uLight, uShade;
${NOISE}
float billow(vec2 p) {
float v = 0.0, a = 0.5;
mat2 m = mat2(1.6, 1.2, -1.2, 1.6);
for (int i = 0; i < 5; i++) { v += a * (1.0 - abs(noise(p) * 2.0 - 1.0)); p = m * p; a *= 0.5; }
return v;
}
float cloud(vec2 q, float t) {
float warp = fbm(q * 0.55 + vec2(t * 0.02, -t * 0.013));
float mass = fbm(q * 0.7 + warp * 1.3 + vec2(t * 0.03, t * 0.006));
float puff = billow(q * 1.5 + warp * 0.8 + vec2(t * 0.045, 0.0));
return mass * 0.64 + puff * 0.46 - 0.08;
}
void main() {
vec2 uv = gl_FragCoord.xy / uRes;
vec2 p = (gl_FragCoord.xy - 0.5 * uRes) / uRes.y;
float t = uTime;
// the wind your cursor left behind: rg = direction, b = how much the cloud was thinned
vec3 wk = texture2D(uWind, vec2(uv.x, 1.0 - uv.y)).rgb;
vec2 wind = (wk.rg - 0.5) * 2.0;
wind.y = -wind.y;
float thin = wk.b;
// big masses; scrolling carries you down through them as they part
vec2 q = p * 0.78 / (1.0 + uClear * 1.6) + vec2(3.7, 1.3);
// the cloud is dragged along the stroke and curls as it goes
float curl = fbm(q * 3.0 + t * 0.1);
q -= wind * (0.2 + 0.16 * curl);
float dens = cloud(q, t);
vec2 sun = vec2(-0.55, 0.75) * 0.12;
float lit = cloud(q + sun, t);
float light = clamp(0.78 + (dens - lit) * 3.2 + (dens - 0.55) * 0.35, 0.0, 1.0);
// clearing is a dissolve, never a cut: the wind erodes the cloud a little into wisps,
// then fades what is left, thinnest where the wisps are thin
float erode = fbm(q * 2.6 + t * 0.05) * 0.6 + fbm(q * 4.8 - t * 0.04) * 0.4;
float wisp = smoothstep(0.25, 0.85, erode);
float part = uClear * (1.3 - length(p * vec2(0.75, 1.1)) * 0.6);
float body = dens - (1.0 - uCover * uReveal) * 0.8 - thin * 0.22 * (1.0 - wisp) - part * 0.3;
float a = smoothstep(-0.03, 0.4, body);
float fade = clamp(thin * (0.62 + 0.55 * (1.0 - wisp)), 0.0, 1.0);
a *= 1.0 - fade * 0.94;
a *= 1.0 - clamp(part * (0.85 + 0.45 * (1.0 - wisp)), 0.0, 1.0);
float shadow = smoothstep(-0.3, 0.2, body) * 0.18 * (1.0 - fade) * (1.0 - uClear);
// blue and white: sunlit tops white, shaded folds sky blue,
// and thin or fading cloud turns to a soft blue haze, like real cloud edges against the light
vec3 col = mix(uShade, uLight, light * light * (3.0 - 2.0 * light));
float haze = clamp(1.0 - smoothstep(0.0, 0.45, body) + fade * 0.7, 0.0, 1.0);
col = mix(col, mix(uShade, uLight, 0.45), haze * 0.42);
gl_FragColor = vec4(col * a, a + (1.0 - a) * shadow);
}`;
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];
}
function program(gl: WebGLRenderingContext, frag: string, names: string[]) {
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);
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);
return Object.fromEntries(names.map((n) => [n, gl.getUniformLocation(prog, n)])) as Record<string, WebGLUniformLocation | null>;
}
function texture(gl: WebGLRenderingContext) {
const tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
return tex;
}
type Bird = {
x: number; y: number; heading: number; turn: number; speed: number; size: number;
phase: number; rate: number; glide: number; seed: number;
};
/** A gull seen from above: pale body, long swept wings with dark tips, the span breathing as it flaps. */
function drawGull(ctx: CanvasRenderingContext2D, b: Bird, shadow: boolean) {
const S = b.size;
const flap = Math.cos(b.phase); // 1 = wings level, toward -1 = wings beating down (foreshortened)
const half = S * (0.62 + 0.38 * Math.abs(flap));
const sweep = S * (0.3 - 0.1 * flap);
const wrist = half * 0.46;
ctx.save();
ctx.translate(b.x, b.y);
ctx.rotate(b.heading); // forward is +x
// long narrow wings: the arm reaches straight out, the hand bends back at the wrist to a pointed tip
const wing = (side: 1 | -1) => {
ctx.beginPath();
ctx.moveTo(S * 0.08, side * S * 0.04);
ctx.quadraticCurveTo(S * 0.11, side * wrist * 0.6, S * 0.09, side * wrist);
ctx.quadraticCurveTo(S * 0.02, side * (wrist + (half - wrist) * 0.55), -sweep, side * half);
ctx.quadraticCurveTo(-sweep * 0.55, side * (wrist + (half - wrist) * 0.35), -S * 0.07, side * wrist * 0.96);
ctx.quadraticCurveTo(-S * 0.11, side * wrist * 0.5, -S * 0.1, side * S * 0.04);
ctx.closePath();
};
const body = () => {
ctx.beginPath();
ctx.ellipse(-S * 0.02, 0, S * 0.24, S * 0.05, 0, 0, Math.PI * 2);
// head
ctx.moveTo(S * 0.27, 0);
ctx.arc(S * 0.22, 0, S * 0.045, 0, Math.PI * 2);
// a short fanned tail
ctx.moveTo(-S * 0.2, -S * 0.04);
ctx.lineTo(-S * 0.33, -S * 0.06);
ctx.lineTo(-S * 0.33, S * 0.06);
ctx.lineTo(-S * 0.2, S * 0.04);
};
if (shadow) {
ctx.fillStyle = "rgba(0, 28, 34, 0.2)";
wing(1);
ctx.fill();
wing(-1);
ctx.fill();
body();
ctx.fill();
ctx.restore();
return;
}
for (const side of [1, -1] as const) {
const g = ctx.createLinearGradient(0, 0, -sweep, side * half);
// grey mantle, paler inner wing, black tips: a gull at a glance
g.addColorStop(0, "#dfe4e8");
g.addColorStop(0.45, "#c9d1d7");
g.addColorStop(0.7, "#b7c0c7");
g.addColorStop(0.78, "#2d3338");
g.addColorStop(1, "#1d2226");
ctx.fillStyle = g;
wing(side);
ctx.fill();
}
ctx.fillStyle = "#fbfaf6";
body();
ctx.fill();
ctx.restore();
}
export function CloudVeil({
image,
alt = "",
cover = 1,
brush = 150,
heal = 3,
birds = 9,
speed = 1,
life = 1,
gusts = true,
light = "#ffffff",
shade = "#5f9ae6",
scroller,
children,
className,
}: CloudVeilProps) {
const hostRef = useRef<HTMLDivElement>(null);
const viewRef = useRef<HTMLDivElement>(null);
const imgRef = useRef<HTMLImageElement>(null);
const props = useRef({ cover, brush, heal, birds, speed, life, gusts, light, shade });
useEffect(() => {
props.current = { cover, brush, heal, birds, speed, life, gusts, light, shade };
}, [cover, brush, heal, birds, speed, life, gusts, light, shade]);
useEffect(() => {
const host = hostRef.current;
const view = viewRef.current;
const img = imgRef.current;
if (!host || !view || !img) return;
if (getComputedStyle(host).position === "static") host.style.position = "relative";
const still = isReducedMotion();
// ---- layers, bottom to top: the living world, birds, clouds ----
const worldCanvas = document.createElement("canvas");
const birdCanvas = document.createElement("canvas");
const cloudCanvas = document.createElement("canvas");
for (const c of [worldCanvas, birdCanvas, cloudCanvas]) {
c.setAttribute("aria-hidden", "true");
c.style.cssText = "position:absolute;inset:0;width:100%;height:100%;display:block;pointer-events:none;";
}
worldCanvas.style.opacity = "0";
img.after(worldCanvas, birdCanvas, cloudCanvas);
const bctx = birdCanvas.getContext("2d")!;
const wgl = worldCanvas.getContext("webgl", { alpha: false, antialias: false });
const gl = cloudCanvas.getContext("webgl", { premultipliedAlpha: true, alpha: true, antialias: false });
// the wind field: a small grid computed exactly in JS (no canvas gradients, so no dithering).
// vx/vy = which way the air is moving, th = how much the cloud has been thinned; it calms back to still.
const GW = 96;
let GH = 54;
let fvx = new Float32Array(GW * GH), fvy = new Float32Array(GW * GH), fth = new Float32Array(GW * GH);
let windData = new Uint8Array(GW * GH * 4);
let w = 1, h = 1, dpr = 1;
const sizeView = () => {
const vh = scroller ? scroller.clientHeight : window.innerHeight;
host.style.setProperty("--cv-view", `${vh}px`);
};
sizeView();
const resize = () => {
w = Math.max(1, view.clientWidth);
h = Math.max(1, view.clientHeight);
dpr = Math.min(window.devicePixelRatio || 1, 2);
birdCanvas.width = Math.round(w * dpr);
birdCanvas.height = Math.round(h * dpr);
// the photo keeps its detail; the soft clouds need far fewer pixels
const ws = Math.min(dpr, 1.5);
worldCanvas.width = Math.round(w * ws);
worldCanvas.height = Math.round(h * ws);
wgl?.viewport(0, 0, worldCanvas.width, worldCanvas.height);
const cs = Math.min(1, dpr) * 0.7;
cloudCanvas.width = Math.round(w * cs);
cloudCanvas.height = Math.round(h * cs);
gl?.viewport(0, 0, cloudCanvas.width, cloudCanvas.height);
const gh = Math.max(32, Math.round((GW * h) / w));
if (gh !== GH) {
GH = gh;
fvx = new Float32Array(GW * GH);
fvy = new Float32Array(GW * GH);
fth = new Float32Array(GW * GH);
windData = new Uint8Array(GW * GH * 4);
}
};
resize();
// ---- world ----
let UW: Record<string, WebGLUniformLocation | null> = {};
let photo: WebGLTexture | null = null;
let photoSize: [number, number] = [1, 1];
let worldReady = false;
if (wgl) {
UW = program(wgl, WORLD, ["uRes", "uImg", "uPan", "uTime", "uZoom", "uLife", "uTex"]);
photo = texture(wgl);
wgl.uniform1i(UW.uTex, 0);
const load = () => {
try {
wgl.bindTexture(wgl.TEXTURE_2D, photo);
wgl.texImage2D(wgl.TEXTURE_2D, 0, wgl.RGBA, wgl.RGBA, wgl.UNSIGNED_BYTE, img);
photoSize = [img.naturalWidth, img.naturalHeight];
worldReady = true;
gsap.to(worldCanvas, { opacity: 1, duration: 0.8, ease: "power2.out" });
} catch {
// the image does not allow CORS: keep the plain photo underneath
worldCanvas.remove();
}
};
if (img.complete && img.naturalWidth) load();
else img.addEventListener("load", load, { once: true });
}
// ---- clouds ----
let UC: Record<string, WebGLUniformLocation | null> = {};
let windTex: WebGLTexture | null = null;
if (gl) {
UC = program(gl, CLOUDS, ["uRes", "uTime", "uCover", "uClear", "uReveal", "uWind", "uLight", "uShade"]);
windTex = texture(gl);
gl.uniform1i(UC.uWind, 0);
} else {
cloudCanvas.style.background = `linear-gradient(180deg, ${props.current.light}, ${props.current.shade})`;
cloudCanvas.style.opacity = "0.85";
}
// ---- state, moved by GSAP ----
const s = { reveal: still ? 1 : 0, clear: still ? 0.45 : 0, zoom: still ? 1.05 : 1.22, px: 0, py: 0 };
let time = 20;
const drawWorld = () => {
if (!wgl || !worldReady) return;
wgl.uniform2f(UW.uRes, worldCanvas.width, worldCanvas.height);
wgl.uniform2f(UW.uImg, photoSize[0], photoSize[1]);
wgl.uniform2f(UW.uPan, s.px, s.py);
wgl.uniform1f(UW.uTime, time);
wgl.uniform1f(UW.uZoom, s.zoom);
wgl.uniform1f(UW.uLife, props.current.life);
wgl.drawArrays(wgl.TRIANGLES, 0, 3);
};
const drawClouds = () => {
if (!gl) return;
const p = props.current;
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.bindTexture(gl.TEXTURE_2D, windTex);
for (let i = 0, o = 0; i < fth.length; i++, o += 4) {
windData[o] = 128 + fvx[i] * 127;
windData[o + 1] = 128 + fvy[i] * 127;
windData[o + 2] = fth[i] * 255;
windData[o + 3] = 255;
}
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, GW, GH, 0, gl.RGBA, gl.UNSIGNED_BYTE, windData);
gl.uniform2f(UC.uRes, cloudCanvas.width, cloudCanvas.height);
gl.uniform1f(UC.uTime, time);
gl.uniform1f(UC.uCover, p.cover);
gl.uniform1f(UC.uClear, s.clear);
gl.uniform1f(UC.uReveal, s.reveal);
gl.uniform3fv(UC.uLight, rgb(p.light));
gl.uniform3fv(UC.uShade, rgb(p.shade));
gl.drawArrays(gl.TRIANGLES, 0, 3);
};
// ---- birds: loose flocks of gulls crossing below the clouds ----
const flock: Bird[] = [];
const spawn = (b: Bird | null, anywhere: boolean): Bird => {
const unit = Math.min(w, h);
const heading = (Math.random() < 0.5 ? 0 : Math.PI) + (Math.random() - 0.5) * 0.9;
const fromLeft = Math.cos(heading) > 0;
const nb: Bird = {
x: anywhere ? Math.random() * w : fromLeft ? -unit * 0.15 : w + unit * 0.15,
y: Math.random() * h,
heading,
turn: 0,
speed: unit * (0.05 + Math.random() * 0.03),
size: unit * (0.045 + Math.random() * 0.025),
phase: Math.random() * Math.PI * 2,
rate: 5 + Math.random() * 2.2,
glide: Math.random() * 3,
seed: Math.random() * 100,
};
if (b) Object.assign(b, nb);
return nb;
};
const syncFlock = (anywhere: boolean) => {
const n = Math.max(0, Math.round(props.current.birds));
while (flock.length < n) flock.push(spawn(null, anywhere));
flock.length = n;
};
syncFlock(true);
const drawBirds = (dt: number) => {
syncFlock(false);
bctx.setTransform(dpr, 0, 0, dpr, 0, 0);
bctx.clearRect(0, 0, w, h);
const unit = Math.min(w, h);
for (const b of flock) {
// wander gently, flap for a while, then glide
b.turn += (Math.sin(time * 0.6 + b.seed) * 0.25 - b.turn) * Math.min(1, dt * 0.8);
b.heading += b.turn * dt * 0.35;
b.x += Math.cos(b.heading) * b.speed * dt;
b.y += Math.sin(b.heading) * b.speed * dt;
b.glide -= dt;
if (b.glide > 0) b.phase += b.rate * dt;
else {
b.phase += (Math.round(b.phase / (Math.PI * 2)) * Math.PI * 2 - b.phase) * Math.min(1, dt * 4);
if (b.glide < -1.6 - (b.seed % 1.5)) b.glide = 1.2 + Math.random() * 2.5;
}
const m = unit * 0.2;
if (b.x < -m || b.x > w + m || b.y < -m || b.y > h + m) spawn(b, false);
}
bctx.save();
bctx.filter = `blur(${Math.max(1, unit * 0.004)}px)`;
for (const b of flock) drawGull(bctx, { ...b, x: b.x + b.size * 0.9, y: b.y + b.size * 1.3 }, true);
bctx.restore();
for (const b of flock) drawGull(bctx, b, false);
};
// ---- wind: the cursor pushes and thins the clouds; the air calms back ----
const calm = (dt: number) => {
const k = Math.exp((-dt * 2.6) / Math.max(0.3, props.current.heal));
for (let i = 0; i < fth.length; i++) {
fvx[i] *= k;
fvy[i] *= k;
fth[i] *= k;
}
};
/** Blow at (x, y) px with air moving (vx, vy) in -1..1; strength is how much of the way it pushes. */
const blow = (x: number, y: number, radius: number, vx: number, vy: number, strength: number) => {
const sx = GW / w;
const cx = x * sx, cy = y * sx, r = Math.max(1.5, radius * sx);
const tx = gsap.utils.clamp(-1, 1, vx), ty = gsap.utils.clamp(-1, 1, vy);
const x0 = Math.max(0, Math.floor(cx - r)), x1 = Math.min(GW - 1, Math.ceil(cx + r));
const y0 = Math.max(0, Math.floor(cy - r)), y1 = Math.min(GH - 1, Math.ceil(cy + r));
for (let gy = y0; gy <= y1; gy++)
for (let gx = x0; gx <= x1; gx++) {
const d2 = ((gx - cx) ** 2 + (gy - cy) ** 2) / (r * r);
if (d2 >= 1) continue;
const f = (1 - d2) * (1 - d2) * strength; // smooth falloff, no edge
const i = gy * GW + gx;
fvx[i] += (tx - fvx[i]) * f;
fvy[i] += (ty - fvy[i]) * f;
fth[i] += (1 - fth[i]) * f;
}
};
const draw = (dt: number) => {
if (!still) calm(dt);
drawWorld();
drawBirds(still ? 0 : dt);
drawClouds();
};
const ro = new ResizeObserver(() => {
sizeView();
resize();
if (still) draw(0);
ScrollTrigger.refresh();
});
ro.observe(host);
if (scroller) ro.observe(scroller);
if (still) {
draw(0);
const redraw = () => requestAnimationFrame(() => draw(0));
img.addEventListener("load", redraw, { once: true });
return () => {
ro.disconnect();
img.removeEventListener("load", redraw);
worldCanvas.remove();
birdCanvas.remove();
cloudCanvas.remove();
wgl?.getExtension("WEBGL_lose_context")?.loseContext();
gl?.getExtension("WEBGL_lose_context")?.loseContext();
};
}
// ---- reveal: the clouds roll in, the birds appear below ----
const intro = gsap
.timeline()
.fromTo(s, { reveal: 0.55 }, { reveal: 1, duration: 2.4, ease: "power2.out" }, 0)
.fromTo(birdCanvas, { autoAlpha: 0 }, { autoAlpha: 1, duration: 1.6, ease: "power2.out" }, 0.4);
// ---- cursor ----
const panX = gsap.quickTo(s, "px", { duration: 1.6, ease: "power3" });
const panY = gsap.quickTo(s, "py", { duration: 1.6, ease: "power3" });
let last: { x: number; y: number; t: number } | null = null;
const local = (e: PointerEvent) => {
const r = view.getBoundingClientRect();
return { x: e.clientX - r.left, y: e.clientY - r.top };
};
const move = (e: PointerEvent) => {
const p = local(e);
const now = performance.now();
const r = props.current.brush;
if (last) {
const dt = Math.max(8, now - last.t);
const vx = ((p.x - last.x) / dt) * 0.12;
const vy = ((p.y - last.y) / dt) * 0.12;
const d = Math.hypot(p.x - last.x, p.y - last.y);
const steps = Math.min(16, Math.ceil(d / (r * 0.3)));
// stronger strokes push harder and thin more
const k = Math.min(1, 0.35 + Math.hypot(vx, vy) * 0.8);
for (let i = 1; i <= steps; i++)
blow(last.x + ((p.x - last.x) * i) / steps, last.y + ((p.y - last.y) * i) / steps, r, vx, vy, 0.16 * k);
}
last = { ...p, t: now };
// the world below drifts a little against the cursor: depth
panX(-(p.x / w - 0.5) * 0.012);
panY(-(p.y / h - 0.5) * 0.012);
};
const leave = () => {
last = null;
panX(0);
panY(0);
};
const down = (e: PointerEvent) => {
// a gust: air bursts outward from the click and the cloud thins around it
const p = local(e);
const g = { r: 0 };
const R = props.current.brush * 1.8;
gsap.to(g, {
r: 1,
duration: 1.1,
ease: "expo.out",
onUpdate: () => {
for (let i = 0; i < 10; i++) {
const a = (i / 10) * Math.PI * 2;
blow(p.x + Math.cos(a) * R * g.r, p.y + Math.sin(a) * R * g.r, R * 0.45, Math.cos(a), Math.sin(a), 0.08);
}
},
});
};
// ---- gusts: every few seconds the wind sweeps a curving path across the sky, then the clouds close again ----
let nextGust: gsap.core.Tween | null = null;
let gustTween: gsap.core.Tween | null = null;
const gust = () => {
nextGust = gsap.delayedCall(gsap.utils.random(3.5, 7), gust);
if (!props.current.gusts || !visible) return;
const ltr = Math.random() < 0.5;
const y0 = h * gsap.utils.random(0.15, 0.85), y1 = h * gsap.utils.random(0.15, 0.85);
const bend = h * gsap.utils.random(-0.25, 0.25);
const R = Math.min(w, h) * gsap.utils.random(0.2, 0.32);
const g = { t: 0 };
let px = ltr ? -R : w + R, py = y0;
gustTween = gsap.to(g, {
t: 1,
duration: gsap.utils.random(2.2, 3.4),
ease: "sine.inOut",
onUpdate: () => {
const u = g.t;
const x = ltr ? -R + (w + 2 * R) * u : w + R - (w + 2 * R) * u;
const y = y0 + (y1 - y0) * u + Math.sin(u * Math.PI) * bend;
const dx = x - px, dy = y - py;
const len = Math.hypot(dx, dy) || 1;
blow(x, y, R, dx / len, dy / len, 0.12);
px = x;
py = y;
},
});
};
nextGust = gsap.delayedCall(1.8, gust);
view.addEventListener("pointermove", move);
view.addEventListener("pointerleave", leave);
view.addEventListener("pointerdown", down);
// ---- scroll: descend through the layer as it parts ----
const clearTo = gsap.quickTo(s, "clear", { duration: 1.1, ease: "power3" });
const zoomTo = gsap.quickTo(s, "zoom", { duration: 1.1, ease: "power3" });
const st = ScrollTrigger.create({
trigger: host,
scroller: scroller ?? undefined,
start: "top top",
end: () => `+=${Math.max(host.offsetHeight - view.offsetHeight, view.offsetHeight * 0.8)}`,
invalidateOnRefresh: true,
onUpdate: (self) => {
clearTo(gsap.utils.clamp(0, 1, self.progress * 1.15));
zoomTo(1.22 - self.progress * 0.2);
},
});
// ---- loop, only while on screen ----
let visible = true;
let prev = performance.now();
const tick = () => {
const now = performance.now();
const dt = Math.min(0.05, (now - prev) / 1000);
prev = now;
if (!visible) return;
time += dt * props.current.speed;
draw(dt);
};
const io = new IntersectionObserver(([en]) => {
visible = en.isIntersecting;
prev = performance.now();
});
io.observe(host);
gsap.ticker.add(tick);
return () => {
gsap.ticker.remove(tick);
intro.kill();
nextGust?.kill();
gustTween?.kill();
st.kill();
io.disconnect();
ro.disconnect();
view.removeEventListener("pointermove", move);
view.removeEventListener("pointerleave", leave);
view.removeEventListener("pointerdown", down);
gsap.killTweensOf(s);
gsap.killTweensOf(worldCanvas);
worldCanvas.remove();
birdCanvas.remove();
cloudCanvas.remove();
wgl?.getExtension("WEBGL_lose_context")?.loseContext();
gl?.getExtension("WEBGL_lose_context")?.loseContext();
};
}, [scroller, image]);
return (
<div ref={hostRef} className={["isolate", className].filter(Boolean).join(" ")}>
<div
ref={viewRef}
className="sticky top-0 h-[var(--cv-view,100vh)] max-h-full touch-pan-y overflow-hidden bg-[#a9cdf2]"
>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
ref={imgRef}
src={image}
alt={alt}
crossOrigin="anonymous"
draggable={false}
className="absolute inset-0 h-full w-full scale-[1.12] object-cover select-none"
/>
{children && <div className="relative z-10 h-full">{children}</div>}
</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
14 propsimagestringThe world under the clouds: a photo, ideally seen from above. Same-origin or CORS-enabled.altstringDescribes the image for screen readers.default ""default ""covernumberHow thick the clouds are before you clear them, 0 to 1.3.default 1default 1brushnumberReach of your cursor's wind, in px.default 150default 150healnumberSeconds the clouds take to settle back after you disturb them.default 3default 3birdsnumberNumber of birds below the clouds.default 9default 9speednumberSpeed of the clouds, wind and water.default 1default 1lifenumberHow alive the world is: water, wind in the trees, cloud shadows, mist. 0 to 2.default 1default 1gustsbooleanGusts of wind that sweep across on their own, clearing a path for a while.default truedefault truelightstringColour of the sunlit side of the clouds.default "#ffffff"default "#ffffff"shadestringColour of the shaded side of the clouds.default "#5f9ae6"default "#5f9ae6"scrollerElement | nullScroll container, if the page does not scroll the window.childrenReactNodeForeground content, pinned above the clouds.classNamestringMake it taller than the screen, e.g. h-[250vh] w-full: the view stays pinned while you scroll through it.