Winter Canopy
Lie back in a winter forest at dusk and look up. A real photograph brought to life: bare black branches reach across a cold slate sky, the thin twigs swaying in the wind while the near trunks barely move, soft clouds drifting behind them, and fine snow falling toward you, the nearest flakes melting into soft out-of-focus discs. A depth map gives true 2.5D parallax as the cursor moves, and the cursor is a moon behind the clouds: the sky glows around it and the branches stand out as silhouettes. A click sends a gust through the trees and a flurry of snow, scrolling draws you up toward the sky and turns the view, and on load your eyes come into focus. Bring your own photo and depth map to make it yours.
Tweak
LIVELIVEWeather
Motion
Interactive
Moonlight and parallax at the cursor, click for a gust of snow
Lens
Usage
Follows your tweaks.
import { WinterCanopy } from "@/components/inreact/winter-canopy";
export function Hero() {
return (
<WinterCanopy
className="h-screen w-full"
>
{/* optional: your hero content, drawn above the forest */}
</WinterCanopy>
);
}Install with the CLI
npx shadcn@latest add https://inreact.vercel.app/r/winter-canopy.jsonOr copy the source
Install the dependencies, then save this as components/inreact/winter-canopy.tsx.
npm install gsap"use client";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import { useEffect, useRef, type ReactNode } from "react";
gsap.registerPlugin(ScrollTrigger);
/**
* Winter Canopy: lie back in a winter forest at dusk and look up.
*
* A real photograph brought to life. Bare black branches reach across a cold
* slate sky; the thin twigs sway in the wind while the near trunks barely
* move, soft clouds drift behind them, and fine snow falls toward you, the
* nearest flakes melting into soft out-of-focus discs.
*
* It reacts: a depth map makes the near branches swing past the far ones as
* the cursor moves (real 2.5D parallax), and the cursor is a moon behind the
* clouds: the sky glows around it and the branches stand out as silhouettes.
* A click sends a gust through the trees and a flurry of snow, scrolling
* draws you up toward the sky and turns the view, and on load your eyes come
* into focus.
*
* <WinterCanopy className="h-screen w-full">
* <YourHero />
* </WinterCanopy>
*
* Bring your own photo with `image`, `depthImage` (white = near) and `center`
* (where the trees converge, 0 to 1). Past the photo's edges the forest
* continues as its own soft mirror image. WebGL2, mipmapped focal blur, real
* bloom. Pauses off-screen; reduced motion shows a still frame; the plain
* photo without WebGL2.
*/
export interface WinterCanopyProps {
/** The photo. */
image?: string;
/** Its depth map, white = near. */
depthImage?: string;
/** Where the trees converge in the photo, [x, y] from 0 to 1. */
center?: [number, number];
/** How much of the screen width the photo spans before it is extended (1 = cover). */
fit?: number;
/** Strength of the wind in the branches. */
wind?: number;
/** How much snow falls, 0 for none. */
snow?: number;
/** Drifting clouds behind the branches. */
clouds?: number;
/** Colour of the moonlight around the cursor. */
moon?: string;
/** Strength of the bloom on the snow and moonlight. */
glow?: number;
/** Depth parallax strength. */
parallax?: number;
/** Speed of all the motion. */
speed?: number;
/** Background shown while the photo loads. */
background?: string;
/** Parallax, moonlight, click gust and flurry, scroll. */
interactive?: boolean;
/** Scroll container if the page does not scroll the window. */
scroller?: Element | null;
/** Foreground content, drawn above the forest. */
children?: ReactNode;
className?: string;
}
const MAXW = 4;
const QUAD = `#version 300 es
in vec2 aQ; out vec2 vUv;
void main(){ vUv = aQ * 0.5 + 0.5; gl_Position = vec4(aQ, 0.0, 1.0); }`;
const SCENE = `#version 300 es
precision highp float;
uniform sampler2D uImg, uDepth;
uniform vec2 uRes, uImgSize, uCen, uCenScr, uMouse, uPar; // px; uCen in image px
uniform float uDpr, uScale, uTime, uZoom, uRot, uHover, uWind, uGust, uSnow, uClouds, uFocus, uIntro, uLod0, uFlash;
uniform vec3 uMoon;
uniform vec4 uWave[${MAXW}];
in vec2 vUv;
out vec4 o;
float h21(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), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
return mix(mix(h21(i), h21(i + vec2(1, 0)), f.x), mix(h21(i + vec2(0, 1)), h21(i + vec2(1, 1)), f.x), f.y);
}
float fbm(vec2 p) { return noise(p) * 0.55 + noise(p * 2.03 + 7.1) * 0.3 + noise(p * 4.1 + 3.7) * 0.15; }
// past the photo's edge the forest continues as its own mirror image; 'past' grows with distance outside
vec2 inside(vec2 ip, out float past) {
vec2 t = ip / uImgSize;
vec2 out2 = max(-t, t - 1.0);
past = 1.0 + max(0.0, max(out2.x * uImgSize.x, out2.y * uImgSize.y)) / (0.12 * min(uImgSize.x, uImgSize.y));
t = 1.0 - abs(1.0 - mod(t, 2.0));
return t * uImgSize;
}
// snow falling toward you: layers of flakes rush out from the sky, the nearest ones big and soft
vec3 snowfall(vec2 uv, float minD) {
float acc = 0.0;
for (int i = 0; i < 6; i++) {
float fi = float(i);
float z = 1.0 - fract(uTime * 0.045 + fi / 6.0); // 1 = far, 0 = at your face
float sc = mix(0.35, 4.5, z); // how much sky one cell spans
vec2 g = uv * sc * 7.0 + vec2(fi * 17.3, fi * 9.1);
g.x += sin(uTime * 0.4 + fi) * 0.4 * uWind; // drift with the wind
vec2 id = floor(g);
vec2 f = fract(g) - 0.5;
float hh = h21(id + fi * 13.0);
if (hh > 0.22) continue;
vec2 c = (vec2(h21(id + 3.1), h21(id + 8.7)) - 0.5) * 0.7;
float rad = 0.025 + 0.055 * pow(h21(id + 5.5), 2.0);
// depth of field: near and far flakes are soft, those in between are crisp
float soft = 0.9 + 3.0 * smoothstep(0.4, 0.0, z) + 1.2 * smoothstep(0.6, 1.0, z);
float px = 7.0 * sc / minD; // cell units per screen px
float e = max(rad * soft, px * 1.2);
float d = length(f - c);
float flake = smoothstep(e + px, 0.0, d - rad * 0.4) ;
float fade = smoothstep(1.0, 0.82, z) * smoothstep(0.0, 0.12, z);
// soft discs get dimmer as they spread
acc += flake * fade * (0.35 + 0.45 * hh / 0.22) * mix(1.0, 0.3, smoothstep(0.4, 0.0, z));
}
return vec3(0.8, 0.85, 0.92) * acc * 0.75;
}
void main() {
vec2 p = vec2(gl_FragCoord.x, uRes.y * uDpr - gl_FragCoord.y) / uDpr;
float minD = min(uRes.x, uRes.y);
vec2 q = p - uCenScr;
float r = length(q) / minD;
// the view slowly turns and breathes
float cr = cos(uRot), sr = sin(uRot);
vec2 qv = mat2(cr, -sr, sr, cr) * q / uZoom;
vec2 ipBase = uCen + qv / uScale;
float unused;
float dep = texture(uDepth, inside(ipBase, unused) / uImgSize).r;
// depth parallax: near branches swing past the far ones
vec2 ip = ipBase - uPar * (dep - 0.3) / uScale;
// wind: the thin far twigs sway, the near trunks barely move; gusts roll across
vec2 wp = ipBase / uImgSize.y * 3.0;
float gustEnv = 0.65 + 0.35 * sin(uTime * 0.23 + wp.x * 0.8) + uGust;
vec2 sway = vec2(fbm(wp + vec2(uTime * 0.18, 0.0)) - 0.5, fbm(wp * 1.3 + vec2(4.0, uTime * 0.14)) - 0.5);
sway += 0.5 * vec2(fbm(wp * 3.0 + uTime * 0.6) - 0.5, fbm(wp * 3.0 + 9.0 - uTime * 0.55) - 0.5);
ip += sway * uWind * gustEnv * mix(1.0, 0.2, dep) * uImgSize.y * 0.006;
// click: a ring of wind rolls out through the branches
float rip = 0.0;
for (int i = 0; i < ${MAXW}; i++) {
vec4 W = uWave[i];
if (W.w <= 0.0) continue;
float age = uTime - W.z;
if (age < 0.0 || age > 3.0) continue;
vec2 dw = p - W.xy;
float rr = length(dw) / minD;
float fr = rr - age * 0.55;
float k = exp(-fr * fr / 0.01) * exp(-age * 1.2) * W.w;
ip += normalize(dw + 1e-4) * k * sin(fr * 40.0) * mix(1.0, 0.25, dep) * uImgSize.y * 0.004;
rip += k;
}
// focal blur: soft at the outer edges, and the whole frame on load until your eyes focus
float edge = smoothstep(0.35, 1.1, r);
float past;
vec2 ipc = inside(ip, past);
float lod = uLod0 + edge * 1.1 + uFocus * 3.5 + min(3.0, (past - 1.0) * 2.0);
vec3 col = textureLod(uImg, ipc / uImgSize, lod).rgb;
col *= mix(1.0, 0.5, smoothstep(1.0, 2.6, past));
// the sky is everything that is not branch
float lum = dot(col, vec3(0.299, 0.587, 0.114));
float sky = smoothstep(0.18, 0.4, lum);
// clouds drift slowly behind the branches
vec2 cp = (uCen + qv / uScale) / uImgSize.y * 1.6;
float cl = fbm(cp + vec2(uTime * 0.012, uTime * 0.004)) - 0.5;
col *= 1.0 + cl * uClouds * 0.35 * sky;
// the cursor is a moon behind the clouds: the sky glows, the branches stay black
vec2 dm = (p - uMouse) / minD;
float moon = exp(-dot(dm, dm) / 0.03) * uHover;
col += uMoon * moon * sky * (0.1 + 0.14 * (cl + 0.5));
col += uMoon * (rip * 0.12 + uFlash * 0.05) * sky;
// snow falling toward you
col += snowfall(q / minD, minD) * uSnow;
// the lens falls off gently toward the corners, as in the photo
col *= 1.0 - 0.22 * smoothstep(0.35, 1.05, r);
// on load: from darkness, as your eyes adjust
col *= smoothstep(0.0, 1.0, uIntro);
o = vec4(col, 1.0);
}`;
const BLUR = `#version 300 es
precision mediump float;
uniform sampler2D uTex; uniform vec2 uStep; in vec2 vUv; out vec4 o;
void main() {
vec4 c = texture(uTex, vUv) * 0.2270;
c += (texture(uTex, vUv + uStep * 1.3846) + texture(uTex, vUv - uStep * 1.3846)) * 0.3162;
c += (texture(uTex, vUv + uStep * 3.2308) + texture(uTex, vUv - uStep * 3.2308)) * 0.0703;
o = c;
}`;
const BRIGHT = `#version 300 es
precision mediump float;
uniform sampler2D uTex; in vec2 vUv; out vec4 o;
void main() {
vec3 c = texture(uTex, vUv).rgb;
float l = max(c.r, max(c.g, c.b));
o = vec4(c * smoothstep(0.6, 1.0, l), 1.0);
}`;
const COMPOSITE = `#version 300 es
precision highp float;
uniform sampler2D uScene, uBloom, uWide;
uniform float uGlow;
in vec2 vUv; out vec4 o;
float h(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); }
void main() {
vec3 sc = texture(uScene, vUv).rgb;
vec3 bl = texture(uBloom, vUv).rgb * 0.75 + texture(uWide, vUv).rgb * 0.35;
vec3 x = sc + bl * uGlow;
vec3 col = x / (1.0 + max(x - 0.8, 0.0) * 0.9);
// dither: the slate sky is a smooth gradient, so it must never band
col += (h(gl_FragCoord.xy) + h(gl_FragCoord.yx + 17.0) - 1.0) / 255.0;
o = 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];
}
type Spring = { x: number; v: number; goal: number };
const spring = (x: number): Spring => ({ x, v: 0, goal: x });
// a soft, slightly underdamped spring: glides and settles with a gentle overshoot
const step = (s: Spring, k: number, c: number, dt: number) => {
const n = Math.max(1, Math.ceil(dt / (1 / 120)));
const h = dt / n;
for (let i = 0; i < n; i++) {
s.v += ((s.goal - s.x) * k - s.v * c) * h;
s.x += s.v * h;
}
};
const DEFAULT_CENTER: [number, number] = [0.53, 0.55];
export function WinterCanopy({
image = "/backgrounds/winter-canopy/sky.jpg",
depthImage = "/backgrounds/winter-canopy/depth.png",
center = DEFAULT_CENTER,
fit = 1,
wind = 1,
snow = 0.5,
clouds = 1,
moon = "#cfdcff",
glow = 0.3,
parallax = 1,
speed = 1,
background = "#14171c",
interactive = true,
scroller,
children,
className,
}: WinterCanopyProps) {
const hostRef = useRef<HTMLDivElement>(null);
const props = useRef({ center, fit, wind, snow, clouds, moon, glow, parallax, speed, interactive });
const redraw = useRef<(() => void) | null>(null);
useEffect(() => {
props.current = { center, fit, wind, snow, clouds, moon, glow, parallax, speed, interactive };
redraw.current?.();
}, [center, fit, wind, snow, clouds, moon, glow, parallax, 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;opacity:0;transition:opacity .6s ease;";
host.prepend(canvas);
const gl = canvas.getContext("webgl2", { alpha: false, antialias: false, powerPreference: "high-performance" });
if (!gl) {
host.style.backgroundImage = `url(${image})`;
host.style.backgroundSize = "cover";
host.style.backgroundPosition = `${props.current.center[0] * 100}% ${props.current.center[1] * 100}%`;
canvas.remove();
return;
}
const build = (fs: string) => {
const pr = gl.createProgram()!;
for (const [type, src] of [
[gl.VERTEX_SHADER, QUAD],
[gl.FRAGMENT_SHADER, fs],
] 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(pr, sh);
}
gl.bindAttribLocation(pr, 0, "aQ");
gl.linkProgram(pr);
if (!gl.getProgramParameter(pr, gl.LINK_STATUS)) console.error(gl.getProgramInfoLog(pr));
return pr;
};
const sceneProg = build(SCENE);
const blurProg = build(BLUR);
const brightProg = build(BRIGHT);
const compProg = build(COMPOSITE);
const uni = (pr: WebGLProgram, names: string[]) =>
Object.fromEntries(names.map((n) => [n, gl.getUniformLocation(pr, n)])) as Record<string, WebGLUniformLocation | null>;
const US = uni(sceneProg, [
"uImg", "uDepth", "uRes", "uImgSize", "uCen", "uCenScr", "uMouse", "uPar", "uDpr", "uScale", "uTime", "uZoom", "uRot",
"uHover", "uWind", "uGust", "uSnow", "uClouds", "uFocus", "uIntro", "uLod0", "uFlash", "uMoon", "uWave",
]);
const UB = uni(blurProg, ["uTex", "uStep"]);
const UR = uni(brightProg, ["uTex"]);
const UC = uni(compProg, ["uScene", "uBloom", "uWide", "uGlow"]);
const vao = gl.createVertexArray();
gl.bindVertexArray(vao);
const vbo = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
const drawQuad = () => gl.drawArrays(gl.TRIANGLES, 0, 3);
type Target = { fb: WebGLFramebuffer; tex: WebGLTexture; w: number; h: number };
const target = (tw: number, th: number): Target => {
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);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, tw, th, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
const fb = gl.createFramebuffer()!;
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
return { fb, tex, w: tw, h: th };
};
let T: { scene: Target; a: Target; b: Target; c: Target; d: Target } | null = null;
const freeTargets = () => {
if (!T) return;
for (const t of Object.values(T)) {
gl.deleteFramebuffer(t.fb);
gl.deleteTexture(t.tex);
}
T = null;
};
let w = 1, h = 1, dpr = 1, rs = 1;
const resize = () => {
dpr = Math.min(window.devicePixelRatio || 1, 1.5);
w = Math.max(1, host.clientWidth);
h = Math.max(1, host.clientHeight);
canvas.width = Math.round(w * dpr);
canvas.height = Math.round(h * dpr);
const sw = Math.max(1, Math.round(canvas.width * rs)), sh = Math.max(1, Math.round(canvas.height * rs));
freeTargets();
T = {
scene: target(sw, sh),
a: target(Math.max(1, sw >> 2), Math.max(1, sh >> 2)),
b: target(Math.max(1, sw >> 2), Math.max(1, sh >> 2)),
c: target(Math.max(1, sw >> 4), Math.max(1, sh >> 4)),
d: target(Math.max(1, sw >> 4), Math.max(1, sh >> 4)),
};
};
resize();
const s = { intro: still ? 1 : 0, focus: still ? 0 : 1, mx: -9999, my: -9999, hover: 0, flash: 0, gust: 0, flurry: 0, sv: 0, climb: 0 };
const px = spring(0), py = spring(0);
const zoom = spring(still ? 1 : 1.08);
const rot = spring(0);
let flow = 0;
const waves: { x: number; y: number; t: number; s: number }[] = [];
const waveBuf = new Float32Array(MAXW * 4);
const imgTex = gl.createTexture()!;
const depTex = gl.createTexture()!;
let imgW = 1, imgH = 1, ready = 0;
let disposed = false;
// the trees' meeting point at the centre; the photo spans `fit` of the width (and at least the full height)
const layout = () => {
const p = props.current;
const scale = Math.max(h / imgH, (w * Math.max(0.3, p.fit)) / imgW) * 1.07;
return { scale, cx: w * 0.5, cy: h * 0.5 };
};
const blurPass = (src: Target, dst: Target, dx: number, dy: number) => {
gl.bindFramebuffer(gl.FRAMEBUFFER, dst.fb);
gl.viewport(0, 0, dst.w, dst.h);
gl.useProgram(blurProg);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, src.tex);
gl.uniform1i(UB.uTex, 0);
gl.uniform2f(UB.uStep, dx / src.w, dy / src.h);
drawQuad();
};
const draw = () => {
if (!T || ready < 2) return;
const p = props.current;
const X = T;
const L = layout();
const minD = Math.min(w, h);
const sceneScale = X.scene.w / canvas.width;
gl.bindFramebuffer(gl.FRAMEBUFFER, X.scene.fb);
gl.viewport(0, 0, X.scene.w, X.scene.h);
gl.useProgram(sceneProg);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, imgTex);
gl.uniform1i(US.uImg, 0);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, depTex);
gl.uniform1i(US.uDepth, 1);
gl.uniform2f(US.uRes, w, h);
gl.uniform2f(US.uImgSize, imgW, imgH);
gl.uniform2f(US.uCen, p.center[0] * imgW, p.center[1] * imgH);
gl.uniform2f(US.uCenScr, L.cx, L.cy);
gl.uniform2f(US.uMouse, s.mx, s.my);
const par = minD * 0.03 * Math.max(0, p.parallax);
gl.uniform2f(US.uPar, px.x * par, py.x * par);
gl.uniform1f(US.uDpr, dpr * sceneScale);
gl.uniform1f(US.uScale, L.scale);
gl.uniform1f(US.uTime, flow);
gl.uniform1f(US.uZoom, zoom.x);
gl.uniform1f(US.uRot, rot.x);
gl.uniform1f(US.uHover, s.hover);
gl.uniform1f(US.uWind, still ? 0 : Math.max(0, p.wind));
gl.uniform1f(US.uGust, s.gust);
gl.uniform1f(US.uSnow, Math.max(0, p.snow) * (1 + s.flurry * 2.5));
gl.uniform1f(US.uClouds, Math.max(0, p.clouds));
gl.uniform1f(US.uFocus, s.focus);
gl.uniform1f(US.uIntro, s.intro);
gl.uniform1f(US.uLod0, Math.max(0, Math.log2(1 / (L.scale * zoom.x * dpr * sceneScale))));
gl.uniform1f(US.uFlash, s.flash);
gl.uniform3fv(US.uMoon, rgb(p.moon));
waveBuf.fill(0);
waves.forEach((v, i) => waveBuf.set([v.x, v.y, v.t, v.s], i * 4));
gl.uniform4fv(US.uWave, waveBuf);
drawQuad();
gl.bindFramebuffer(gl.FRAMEBUFFER, X.a.fb);
gl.viewport(0, 0, X.a.w, X.a.h);
gl.useProgram(brightProg);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, X.scene.tex);
gl.uniform1i(UR.uTex, 0);
drawQuad();
blurPass(X.a, X.b, 1, 0);
blurPass(X.b, X.a, 0, 1);
blurPass(X.a, X.c, 1, 0);
blurPass(X.c, X.d, 0, 1);
blurPass(X.d, X.c, 1.8, 0);
blurPass(X.c, X.d, 0, 1.8);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.viewport(0, 0, canvas.width, canvas.height);
gl.useProgram(compProg);
[X.scene.tex, X.a.tex, X.d.tex].forEach((tex, i) => {
gl.activeTexture(gl.TEXTURE0 + i);
gl.bindTexture(gl.TEXTURE_2D, tex);
});
gl.uniform1i(UC.uScene, 0);
gl.uniform1i(UC.uBloom, 1);
gl.uniform1i(UC.uWide, 2);
gl.uniform1f(UC.uGlow, Math.max(0, p.glow));
drawQuad();
gl.activeTexture(gl.TEXTURE0);
};
const ro = new ResizeObserver(() => {
resize();
draw();
});
ro.observe(host);
// ---- cursor: parallax by depth, a moon glowing behind the clouds ----
const mxTo = gsap.quickTo(s, "mx", { duration: 0.6, ease: "power3" });
const myTo = gsap.quickTo(s, "my", { duration: 0.6, ease: "power3" });
const hoverTo = gsap.quickTo(s, "hover", { duration: 1.0, ease: "sine.inOut" });
let first = true;
const local = (e: PointerEvent) => {
const r = host.getBoundingClientRect();
return { x: e.clientX - r.left, y: e.clientY - r.top };
};
const move = (e: PointerEvent) => {
if (still || !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);
px.goal = (p.x / w) * 2 - 1;
py.goal = (p.y / h) * 2 - 1;
};
const leave = () => {
first = true;
hoverTo(0);
px.goal = 0;
py.goal = 0;
};
// ---- click: a gust through the trees and a flurry of snow ----
const down = (e: PointerEvent) => {
if (still || !props.current.interactive) return;
const p = local(e);
waves.push({ x: p.x, y: p.y, t: flow, s: 1 });
if (waves.length > MAXW) waves.shift();
gsap.killTweensOf(s, "flash,gust,flurry");
gsap.timeline().to(s, { flash: 1, duration: 0.25, ease: "sine.out" }).to(s, { flash: 0, duration: 1.4, ease: "sine.inOut" });
gsap.timeline().to(s, { gust: 1.4, duration: 0.5, ease: "power2.out" }).to(s, { gust: 0, duration: 2.6, ease: "sine.inOut" });
gsap.timeline().to(s, { flurry: 1, duration: 0.6, ease: "power2.out" }).to(s, { flurry: 0, duration: 3.5, ease: "sine.inOut" });
};
host.addEventListener("pointermove", move);
host.addEventListener("pointerleave", leave);
host.addEventListener("pointerdown", down);
// ---- scroll: rise toward the sky and turn with scroll speed ----
const svTo = gsap.quickTo(s, "sv", { duration: 0.6, ease: "power2.out" });
const climbTo = gsap.quickTo(s, "climb", { duration: 1.1, ease: "power3" });
let svIdle: gsap.core.Tween | null = null;
const st = still
? null
: ScrollTrigger.create({
trigger: host,
scroller: scroller ?? undefined,
start: "top bottom",
end: "bottom top",
onUpdate: (self) => {
if (!props.current.interactive) return;
climbTo(Math.max(0, self.progress - 0.5) * 0.4);
svTo(Math.max(-1.5, Math.min(1.5, self.getVelocity() / 1800)));
svIdle?.kill();
svIdle = gsap.delayedCall(0.15, () => svTo(0));
},
});
// ---- loop ----
let visible = true;
let prev = performance.now();
let slow = 0;
let intro: gsap.core.Timeline | null = null;
const tick = () => {
const t = performance.now();
const rawDt = (t - prev) / 1000;
const dt = Math.min(0.05, rawDt);
prev = t;
if (!visible || ready < 2) return;
const p = props.current;
flow += dt * Math.max(0, p.speed);
// idle: the view breathes and turns a touch, as if you were lying on the snow looking up
zoom.goal = 1 + 0.025 * (0.5 + 0.5 * Math.sin(flow * 0.17)) + s.climb + s.gust * 0.015;
rot.goal = Math.sin(flow * 0.11) * 0.03 + s.sv * 0.05;
step(zoom, 10, 5.5, dt);
step(rot, 6, 4.2, dt);
step(px, 14, 6.2, dt);
step(py, 14, 6.2, dt);
for (let i = waves.length - 1; i >= 0; i--) if (flow - waves[i].t > 3.1) waves.splice(i, 1);
slow = slow * 0.97 + (rawDt > 1 / 45 ? 0.03 : 0);
if (slow > 0.5 && rs === 1) {
rs = 0.8;
resize();
}
draw();
};
const io = new IntersectionObserver(([en]) => {
visible = en.isIntersecting;
prev = performance.now();
});
const start = () => {
canvas.style.opacity = "1";
if (still) {
draw();
redraw.current = draw;
return;
}
// on load: out of the dark, your eyes come into focus
intro = gsap
.timeline()
.to(s, { intro: 1, duration: 1.8, ease: "sine.inOut" }, 0)
.to(s, { focus: 0, duration: 2.6, ease: "power2.inOut" }, 0.2);
prev = performance.now();
io.observe(host);
gsap.ticker.add(tick);
};
const load = (src: string, tex: WebGLTexture, mip: boolean, onSize?: (iw: number, ih: number) => void) => {
const im = new Image();
im.crossOrigin = "anonymous";
im.decoding = "async";
im.onload = () => {
if (disposed) return;
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, im);
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);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
if (mip) {
gl.generateMipmap(gl.TEXTURE_2D);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
const an = gl.getExtension("EXT_texture_filter_anisotropic");
if (an) gl.texParameterf(gl.TEXTURE_2D, an.TEXTURE_MAX_ANISOTROPY_EXT, 4);
} else gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
onSize?.(im.naturalWidth, im.naturalHeight);
ready++;
if (ready === 2) start();
};
im.src = src;
};
load(image, imgTex, true, (iw, ih) => {
imgW = iw;
imgH = ih;
});
load(depthImage, depTex, false);
return () => {
disposed = true;
redraw.current = null;
gsap.ticker.remove(tick);
intro?.kill();
st?.kill();
svIdle?.kill();
io.disconnect();
ro.disconnect();
host.removeEventListener("pointermove", move);
host.removeEventListener("pointerleave", leave);
host.removeEventListener("pointerdown", down);
gsap.killTweensOf(s);
freeTargets();
gl.deleteTexture(imgTex);
gl.deleteTexture(depTex);
gl.deleteProgram(sceneProg);
gl.deleteProgram(blurProg);
gl.deleteProgram(brightProg);
gl.deleteProgram(compProg);
gl.deleteBuffer(vbo);
gl.deleteVertexArray(vao);
canvas.remove();
gl.getExtension("WEBGL_lose_context")?.loseContext();
};
}, [image, depthImage, 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>
);
}
export default WinterCanopy;
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
16 propsimagestringThe photo.default "/backgrounds/winter-canopy/sky.jpg"default "/backgrounds/winter-canopy/sky.jpg"depthImagestringIts depth map, white = near (parallax, and how much each part sways).default "/backgrounds/winter-canopy/depth.png"default "/backgrounds/winter-canopy/depth.png"center[number, number]Where the trees converge in the photo, 0 to 1.default [0.53, 0.55]default [0.53, 0.55]fitnumberHow much of the screen width the photo spans (1 = cover); below 1 the forest continues past the sides as a soft mirror image.default 1default 1windnumberStrength of the wind in the branches.default 1default 1snownumberHow much snow falls, 0 for none.default 0.5default 0.5cloudsnumberDrifting clouds behind the branches.default 1default 1moonstringColour of the moonlight around the cursor.default "#cfdcff"default "#cfdcff"glownumberStrength of the bloom on the snow and moonlight.default 0.3default 0.3parallaxnumberDepth parallax strength.default 1default 1speednumberSpeed of all the motion.default 1default 1backgroundstringShown while the photo loads.default "#14171c"default "#14171c"interactivebooleanParallax, moonlight, click gust and flurry, scroll.default truedefault truescrollerElement | nullScroll container, if the page does not scroll the window.childrenReactNodeForeground content, drawn above the forest.classNamestringSize it, e.g. h-screen w-full.