Backgrounds05 / 118
Snow Storm
A living snowfall over a mountain at night. Thousands of flakes fall at different depths, far ones small and sharp, near ones big and soft, each with its own drift. Your cursor blows a gust through the snow, a click throws up a burst of powder, gusts sweep across on their own with blowing snow streaming over the scene, and scrolling whips the storm up as the flakes rush past.
WebGLWebGLSnow physicsSnow physicsCursor windCursor windGSAP ScrollTriggerGSAP ScrollTriggerReduced-motion safeReduced-motion safe
Blow the snowClick for a burst of powder
Tweak
LIVELIVEStorm
1.00
2600
1.00
#f4f8ff
Wind
0.35
Gusts
Wind rises and falls on its own
170px
Usage
Follows your tweaks.
usage.tsx
import { SnowStorm } from "@/components/inreact/snow-storm";
export function Hero() {
return (
<SnowStorm
image="/your-photo.jpg"
className="h-screen w-full"
>
{/* optional: your hero content, drawn above the snow */}
</SnowStorm>
);
}Install with the CLI
TS + Tailwind
npx shadcn@latest add https://inreact.vercel.app/r/snow-storm.jsonOr copy the source
Install the dependencies, then save this as components/inreact/snow-storm.tsx.
terminal
npm install gsapcomponents/inreact/snow-storm.tsx
"use client";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import { useEffect, useRef, type ReactNode } from "react";
gsap.registerPlugin(ScrollTrigger);
/**
* Snow Storm: a living snowfall over a mountain.
*
* Thousands of flakes fall at different depths over your photo: far ones
* small and sharp, near ones big and soft, each with its own fall, sway and
* drift. Real wind moves them: your cursor blows a gust through the snow,
* a click throws up a burst of powder, gusts sweep across on their own with
* blowing snow streaming over the scene, and scrolling whips the storm up
* while the flakes rush past.
*
* <SnowStorm image="/peak.jpg" className="h-screen w-full">
* <YourHero />
* </SnowStorm>
*
* WebGL: one draw call for the flakes, one for the blowing snow, over a plain
* <img>, so any image works. The flakes run a small physics step in JS; GSAP
* drives the build-up, the wind, the gusts and the scroll. Pauses off-screen,
* a gentle still snowfall for reduced motion.
*/
export interface SnowStormProps {
/** The scene behind the snow. A dark sky makes the flakes shine. */
image: string;
/** Describes the image for screen readers. */
alt?: string;
/** Strength of the storm at rest, 0 to 2. Scrolling adds to it. */
intensity?: number;
/** Maximum number of flakes. */
flakes?: number;
/** Size of the flakes. */
size?: number;
/** Steady wind: negative blows left, positive blows right. */
wind?: number;
/** Gusts that sweep across on their own, with blowing snow. */
gusts?: boolean;
/** Reach of your cursor's wind, in px. */
reach?: number;
/** Colour of the snow. */
color?: string;
/** Scroll container if the page does not scroll the window. */
scroller?: Element | null;
/** Foreground content, drawn above the snow. */
children?: ReactNode;
className?: string;
}
const FLAKE_VERT = `
attribute vec4 aFlake; // x, y (px), depth 0 far .. 1 near, twinkle seed
uniform vec2 uRes;
uniform float uDpr, uSize, uAlpha;
varying float vA, vSoft;
void main() {
float z = aFlake.z;
float px = (0.6 + pow(z, 2.5) * 9.0) * uSize;
gl_PointSize = px * 2.0 * uDpr + 2.0;
// far flakes crisp and faint, near flakes soft like out-of-focus bokeh
vSoft = smoothstep(0.55, 1.0, z);
vA = (0.35 + 0.55 * z) * (1.0 - vSoft * 0.55) * (0.8 + 0.2 * aFlake.w) * uAlpha;
vec2 clip = aFlake.xy / uRes * 2.0 - 1.0;
gl_Position = vec4(clip.x, -clip.y, 0.0, 1.0);
}`;
const FLAKE_FRAG = `
precision mediump float;
uniform vec3 uColor;
varying float vA, vSoft;
void main() {
vec2 q = gl_PointCoord * 2.0 - 1.0;
float r = length(q);
// a soft disc: crisp edge far away, a wide falloff up close
float a = 1.0 - smoothstep(mix(0.35, 0.0, vSoft), 1.0, r);
a *= vA;
gl_FragColor = vec4(uColor * a, a);
}`;
const QUAD_VERT = `attribute vec2 aPos; void main(){ gl_Position = vec4(aPos, 0.0, 1.0); }`;
// blowing snow: long streaming bands driven by the wind, heavier near the ground
const HAZE_FRAG = `
precision highp float;
uniform vec2 uRes;
uniform float uTime, uStorm, uDrift, uGust;
uniform vec3 uColor;
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;
for (int i = 0; i < 5; i++) { v += a * noise(p); p = p * 2.03 + vec2(1.7, 9.2); a *= 0.5; }
return v;
}
void main() {
vec2 uv = gl_FragCoord.xy / uRes;
float aspect = uRes.x / uRes.y;
vec2 p = vec2(uv.x * aspect, uv.y);
// streams stretched along the wind, flowing with it
vec2 s = vec2(p.x * 1.6 - uDrift, p.y * 7.0 + sin(p.x * 2.0 + uTime * 0.3) * 0.6);
float stream = fbm(s + fbm(s * 0.5 + uTime * 0.05) * 1.4);
float band = smoothstep(0.48, 0.85, stream);
float ground = 0.35 + 0.65 * smoothstep(0.85, 0.0, uv.y);
float veil = 0.05 + 0.1 * fbm(p * 2.0 + vec2(-uDrift * 0.4, uTime * 0.02));
float a = (band * 0.3 * ground * (0.6 + uGust * 0.9) + veil) * uStorm;
a = clamp(a, 0.0, 0.6);
gl_FragColor = vec4(uColor * a, a);
}`;
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 build(gl: WebGLRenderingContext, vs: string, fs: string) {
const prog = gl.createProgram()!;
for (const [type, src] of [
[gl.VERTEX_SHADER, vs],
[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(prog, sh);
}
gl.linkProgram(prog);
return prog;
}
export function SnowStorm({
image,
alt = "",
intensity = 1,
flakes = 2600,
size = 1,
wind = 0.35,
gusts = true,
reach = 170,
color = "#f4f8ff",
scroller,
children,
className,
}: SnowStormProps) {
const hostRef = useRef<HTMLDivElement>(null);
const imgRef = useRef<HTMLImageElement>(null);
const props = useRef({ intensity, flakes, size, wind, gusts, reach, color });
useEffect(() => {
props.current = { intensity, flakes, size, wind, gusts, reach, color };
}, [intensity, flakes, size, wind, gusts, reach, color]);
useEffect(() => {
const host = hostRef.current;
const img = imgRef.current;
if (!host || !img) 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;pointer-events:none;";
img.after(canvas);
const gl = canvas.getContext("webgl", { alpha: true, premultipliedAlpha: true, antialias: false });
if (!gl) return () => canvas.remove();
const flakeProg = build(gl, FLAKE_VERT, FLAKE_FRAG);
const hazeProg = build(gl, QUAD_VERT, HAZE_FRAG);
const loc = (p: WebGLProgram, n: string[]) =>
Object.fromEntries(n.map((k) => [k, gl.getUniformLocation(p, k)])) as Record<string, WebGLUniformLocation | null>;
const UF = loc(flakeProg, ["uRes", "uDpr", "uSize", "uAlpha", "uColor"]);
const UH = loc(hazeProg, ["uRes", "uTime", "uStorm", "uDrift", "uGust", "uColor"]);
const aFlake = gl.getAttribLocation(flakeProg, "aFlake");
const aPos = gl.getAttribLocation(hazeProg, "aPos");
const quad = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, quad);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
const flakeBuf = gl.createBuffer();
gl.enable(gl.BLEND);
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
// ---- size ----
let w = 1, h = 1, dpr = 1;
const resize = () => {
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();
// ---- the flakes: x, y, depth, seed, plus velocity and sway phase ----
const MAX = 6000;
const fx = new Float32Array(MAX), fy = new Float32Array(MAX), fz = new Float32Array(MAX), fs = new Float32Array(MAX);
const vx = new Float32Array(MAX), vy = new Float32Array(MAX), ph = new Float32Array(MAX);
const out = new Float32Array(MAX * 4);
const seedFlake = (i: number, anywhere: boolean) => {
// more far flakes than near ones, like real depth
fz[i] = Math.pow(Math.random(), 2.2);
fx[i] = Math.random() * w;
fy[i] = anywhere ? Math.random() * h : -20 - Math.random() * h * 0.2;
fs[i] = Math.random();
vx[i] = 0;
vy[i] = 0;
ph[i] = Math.random() * Math.PI * 2;
};
for (let i = 0; i < MAX; i++) seedFlake(i, true);
// ---- state: every value here is moved by GSAP ----
const s = { storm: still ? 0.6 : 0, gust: 0, gustDir: 1, mx: -1e4, my: -1e4, mvx: 0, mvy: 0, boost: 0, rush: 0, px: 0, py: 0 };
let time = 0;
let drift = 0;
let drawn = 0;
const step = (dt: number) => {
const p = props.current;
const storm = Math.max(0, s.storm);
const unit = Math.min(w, h);
const R = p.reach;
const R2 = R * R;
const steady = p.wind * (0.6 + storm * 0.8) + s.gust * s.gustDir * 2.4;
for (let i = 0; i < MAX; i++) {
const z = fz[i];
const depth = 0.25 + z * 0.75;
// terminal velocity: near flakes fall faster across the screen
const fall = unit * (0.05 + z * 0.16) * (0.75 + storm * 0.6) - s.rush * unit * 0.9 * depth;
const breeze = steady * unit * 0.12 * depth;
// flakes relax back toward the steady wind and their fall
vx[i] += (breeze - vx[i]) * Math.min(1, dt * 1.4);
vy[i] += (fall - vy[i]) * Math.min(1, dt * 1.4);
// the cursor's wind: a gust along the stroke, pushing flakes aside
const dx = fx[i] - s.mx, dy = fy[i] - s.my;
const d2 = dx * dx + dy * dy;
if (d2 < R2) {
const f = (1 - d2 / R2) ** 2 * depth;
const d = Math.sqrt(d2) || 1;
vx[i] += (s.mvx * 10 + (dx / d) * 120) * f * dt * 6;
vy[i] += (s.mvy * 10 + (dy / d) * 120) * f * dt * 6;
}
ph[i] += dt * (0.8 + fs[i] * 1.4);
const sway = Math.sin(ph[i]) * unit * 0.012 * depth;
fx[i] += (vx[i] + sway) * dt;
fy[i] += vy[i] * dt;
// wrap around the edges
if (fy[i] > h + 30) {
fy[i] = -30;
fx[i] = Math.random() * w;
} else if (fy[i] < -60) {
fy[i] = h + 20;
fx[i] = Math.random() * w;
}
if (fx[i] < -40) fx[i] += w + 80;
else if (fx[i] > w + 40) fx[i] -= w + 80;
}
};
const draw = () => {
const p = props.current;
const storm = Math.max(0, s.storm);
const col = rgb(p.color);
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
// blowing snow
gl.useProgram(hazeProg);
gl.bindBuffer(gl.ARRAY_BUFFER, quad);
gl.enableVertexAttribArray(aPos);
gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0);
gl.uniform2f(UH.uRes, canvas.width, canvas.height);
gl.uniform1f(UH.uTime, time);
gl.uniform1f(UH.uStorm, Math.min(1.6, storm));
gl.uniform1f(UH.uDrift, drift);
gl.uniform1f(UH.uGust, s.gust);
gl.uniform3fv(UH.uColor, col);
gl.drawArrays(gl.TRIANGLES, 0, 3);
gl.disableVertexAttribArray(aPos);
// flakes: the storm decides how many are falling; depth parallax follows the cursor
const n = Math.min(MAX, Math.round(p.flakes * Math.min(1, 0.3 + storm * 0.55)));
drawn = n;
for (let i = 0; i < n; i++) {
const z = fz[i];
const o = i * 4;
out[o] = fx[i] + s.px * (4 + z * 26);
out[o + 1] = fy[i] + s.py * (3 + z * 18);
out[o + 2] = z;
out[o + 3] = fs[i];
}
gl.useProgram(flakeProg);
gl.bindBuffer(gl.ARRAY_BUFFER, flakeBuf);
gl.bufferData(gl.ARRAY_BUFFER, out.subarray(0, n * 4), gl.DYNAMIC_DRAW);
gl.enableVertexAttribArray(aFlake);
gl.vertexAttribPointer(aFlake, 4, gl.FLOAT, false, 0, 0);
gl.uniform2f(UF.uRes, w, h);
gl.uniform1f(UF.uDpr, dpr);
gl.uniform1f(UF.uSize, p.size);
gl.uniform1f(UF.uAlpha, Math.min(1, 0.55 + storm * 0.35));
gl.uniform3fv(UF.uColor, col);
gl.drawArrays(gl.POINTS, 0, n);
gl.disableVertexAttribArray(aFlake);
};
const ro = new ResizeObserver(() => {
const ow = w, oh = h;
resize();
// keep the flakes spread over the new size
for (let i = 0; i < MAX; i++) {
fx[i] *= w / ow;
fy[i] *= h / oh;
}
if (still) draw();
});
ro.observe(host);
if (still) {
draw();
return () => {
ro.disconnect();
canvas.remove();
gl.getExtension("WEBGL_lose_context")?.loseContext();
};
}
// ---- build-up: the snow thickens and the mountain settles back ----
const intro = gsap
.timeline()
.to(s, { storm: () => props.current.intensity, duration: 3, ease: "power2.inOut" }, 0)
.fromTo(img, { scale: 1.12 }, { scale: 1.04, duration: 4, ease: "expo.out" }, 0);
// follow later changes to the intensity prop smoothly
let lastIntensity = props.current.intensity;
// ---- cursor: the wind follows your stroke, the scene shifts in depth ----
const mxTo = gsap.quickTo(s, "mx", { duration: 0.25, ease: "power2" });
const myTo = gsap.quickTo(s, "my", { duration: 0.25, ease: "power2" });
const mvxTo = gsap.quickTo(s, "mvx", { duration: 0.35, ease: "power2" });
const mvyTo = gsap.quickTo(s, "mvy", { duration: 0.35, ease: "power2" });
const pxTo = gsap.quickTo(s, "px", { duration: 1.4, ease: "power3" });
const pyTo = gsap.quickTo(s, "py", { duration: 1.4, ease: "power3" });
const still2 = gsap.delayedCall(0.1, () => {
mvxTo(0);
mvyTo(0);
}).pause();
let lp: { x: number; y: number; t: number } | null = null;
const local = (e: PointerEvent) => {
const r = host.getBoundingClientRect();
return { x: e.clientX - r.left, y: e.clientY - r.top };
};
const move = (e: PointerEvent) => {
const p = local(e);
const now = performance.now();
if (lp) {
const dt = Math.max(8, now - lp.t);
mvxTo(gsap.utils.clamp(-60, 60, ((p.x - lp.x) / dt) * 16));
mvyTo(gsap.utils.clamp(-60, 60, ((p.y - lp.y) / dt) * 16));
still2.restart(true);
} else gsap.set(s, { mx: p.x, my: p.y });
mxTo(p.x);
myTo(p.y);
pxTo(-(p.x / w - 0.5));
pyTo(-(p.y / h - 0.5));
lp = { ...p, t: now };
};
const leave = () => {
lp = null;
mvxTo(0);
mvyTo(0);
pxTo(0);
pyTo(0);
gsap.set(s, { mx: -1e4, my: -1e4 });
};
// a burst of powder: nearby snow blasts outward, and fresh powder sprays from the point
let spray = 0;
const down = (e: PointerEvent) => {
const p = local(e);
const R = props.current.reach * 1.6;
for (let i = 0; i < MAX; i++) {
const dx = fx[i] - p.x, dy = fy[i] - p.y;
const d = Math.hypot(dx, dy);
if (d < R) {
const f = (1 - d / R) * (0.4 + fz[i]);
vx[i] += (dx / (d || 1)) * 900 * f;
vy[i] += (dy / (d || 1)) * 900 * f - 260 * f;
}
}
for (let k = 0; k < 140; k++) {
const i = spray++ % Math.max(1, drawn);
const a = Math.random() * Math.PI * 2;
const v = 200 + Math.random() * 700;
fx[i] = p.x;
fy[i] = p.y;
vx[i] = Math.cos(a) * v;
vy[i] = Math.sin(a) * v - 200;
}
};
host.addEventListener("pointermove", move);
host.addEventListener("pointerleave", leave);
host.addEventListener("pointerdown", down);
// ---- gusts: the wind rises and falls on its own ----
let nextGust: gsap.core.Tween | null = null;
const gust = () => {
nextGust = gsap.delayedCall(gsap.utils.random(4, 9), gust);
if (!props.current.gusts) return;
s.gustDir = Math.random() < 0.7 ? Math.sign(props.current.wind || 1) : -Math.sign(props.current.wind || 1);
gsap
.timeline()
.to(s, { gust: gsap.utils.random(0.6, 1), duration: gsap.utils.random(1.2, 2), ease: "sine.inOut" })
.to(s, { gust: 0, duration: gsap.utils.random(2, 3.5), ease: "sine.inOut" });
};
nextGust = gsap.delayedCall(2.5, gust);
// ---- scroll: the storm whips up and the flakes rush past ----
const boostTo = gsap.quickTo(s, "boost", { duration: 1.2, ease: "power3" });
const rushTo = gsap.quickTo(s, "rush", { duration: 0.6, ease: "power2.out" });
const calm = gsap.delayedCall(0.15, () => rushTo(0)).pause();
const st = ScrollTrigger.create({
trigger: host,
scroller: scroller ?? undefined,
start: "top bottom",
end: "bottom top",
onUpdate: (self) => {
boostTo(self.progress * 0.8);
rushTo(gsap.utils.clamp(-1.5, 1.5, self.getVelocity() / 1800));
calm.restart(true);
},
});
// ---- 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;
const p = props.current;
if (p.intensity !== lastIntensity && !intro.isActive()) {
lastIntensity = p.intensity;
gsap.to(s, { storm: p.intensity, duration: 1.2, ease: "power2.out", overwrite: "auto" });
}
time += dt;
drift += dt * (0.15 + (p.wind * 0.6 + s.gust * s.gustDir * 1.8) * (0.4 + s.storm * 0.4));
const base = s.storm;
s.storm = base + s.boost * 0.5;
step(dt);
draw();
s.storm = base;
};
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();
still2.kill();
calm.kill();
st.kill();
io.disconnect();
ro.disconnect();
host.removeEventListener("pointermove", move);
host.removeEventListener("pointerleave", leave);
host.removeEventListener("pointerdown", down);
gsap.killTweensOf(s);
gsap.killTweensOf(img);
gsap.set(img, { clearProps: "transform" });
canvas.remove();
gl.getExtension("WEBGL_lose_context")?.loseContext();
};
}, [scroller, image]);
return (
<div ref={hostRef} className={["isolate overflow-hidden bg-[#141a24]", className].filter(Boolean).join(" ")}>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
ref={imgRef}
src={image}
alt={alt}
draggable={false}
className="absolute inset-0 h-full w-full object-cover select-none"
/>
{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
12 propsimagestringThe scene behind the snow. A dark sky makes the flakes shine.altstringDescribes the image for screen readers.default ""default ""intensitynumberStrength of the storm at rest, 0 to 2. Scrolling adds to it.default 1default 1flakesnumberMaximum number of flakes.default 2600default 2600sizenumberSize of the flakes.default 1default 1windnumberSteady wind: negative blows left, positive blows right.default 0.35default 0.35gustsbooleanGusts that sweep across on their own, with blowing snow.default truedefault truereachnumberReach of your cursor's wind, in px.default 170default 170colorstringColour of the snow.default "#f4f8ff"default "#f4f8ff"scrollerElement | nullScroll container, if the page does not scroll the window.childrenReactNodeForeground content, drawn above the snow.classNamestringSize it, e.g. h-screen w-full.