Particle Sea
A dark ocean of glowing particles under a black sky, filmed with a real lens. Rows of fine cyan particles ride slow rolling swells, the crests catching the light, and the shot has true depth of field: a band of the sea sits crisp in focus, the far rows melt into a soft haze and the nearest rows swell into big, soft bokeh. It moves like real water: your cursor trails a wake across the surface and the rings spread and die out, a click drops a stone into the sea, and the lens racks focus to wherever your cursor touches the water. Scrolling glides you forward over the swells, coasting on with momentum before easing back to a slow drift, and on load the sea rises out of the dark.
Tweak
LIVELIVESea
Lens
Interactive
Trail a wake with the cursor, click to drop a stone, scroll to glide
Usage
Follows your tweaks.
import { ParticleSea } from "@/components/inreact/particle-sea";
export function Hero() {
return (
<ParticleSea
className="h-screen w-full"
>
{/* optional: your hero content, drawn above the sea */}
</ParticleSea>
);
}Install with the CLI
npx shadcn@latest add https://inreact.vercel.app/r/particle-sea.jsonOr copy the source
Install the dependencies, then save this as components/inreact/particle-sea.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);
/**
* Particle Sea: a dark ocean of glowing particles, filmed with a real lens.
*
* Rows of fine cyan particles ride slow rolling swells toward a black sky. The
* shot has true depth of field: a band of the sea sits crisp in focus, the far
* rows melt into a soft haze, and the nearest rows swell into big, soft bokeh
* discs, all from one thin-lens blur per particle.
*
* It moves like real water: the surface is a small wave-equation height field.
* Your cursor trails a wake across it and the rings spread, reflect and die out;
* a click drops a stone into the sea. The lens racks focus to wherever your
* cursor touches the water, the camera leans gently toward it, and scrolling
* glides you forward over the swells, coasting on with momentum before easing
* back to a slow drift. When you leave it alone, the odd drop falls. On load
* the sea rises out of the dark.
*
* <ParticleSea className="h-screen w-full">
* <YourHero />
* </ParticleSea>
*
* WebGL2: every particle is one point drawn by one shader (no geometry upload),
* in HDR with a real bloom. Pauses off-screen; reduced motion shows a still
* frame; a CSS gradient without WebGL2.
*/
export interface ParticleSeaProps {
/** Colour of the particles. */
color?: string;
/** Colour the crests and ripples catch. */
highlight?: string;
/** Sky and deep background. */
background?: string;
/** How many particles: 1 is about a hundred thousand on a desktop screen. */
density?: number;
/** Height of the swells. */
waves?: number;
/** Speed of the swells and the idle drift. */
speed?: number;
/** Lens aperture: how strongly the near and far rows blur. */
blur?: number;
/** Where the lens focuses when idle, 0 near to 1 far. */
focus?: number;
/** Where the horizon sits, 0 top to 1 bottom. */
horizon?: number;
/** Strength of the bloom. */
glow?: number;
/** Cursor wake, click drop, focus pull, scroll glide. */
interactive?: boolean;
/** Scroll container if the page does not scroll the window. */
scroller?: Element | null;
/** Foreground content, drawn above the sea. */
children?: ReactNode;
className?: string;
}
// ripple grid, camera-relative world rect
const RX = 112, RZ = 96;
const RX0 = -7, RX1 = 7, RZ0 = 1, RZ1 = 13;
const ZNEAR = 0.9, ZFAR = 15, ROWS = 64;
const CAMH = 1;
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 POINTS_VS = `#version 300 es
precision highp float;
uniform float uNX, uDZ, uTravel, uTime, uAmp, uTanV, uTanH, uPitch, uYaw, uFocus, uK, uCore, uMaxSize, uIntro, uSpeed;
uniform sampler2D uRip;
out vec3 vCol;
out float vSoft;
uniform vec3 uColor, uHi;
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); }
// slow rolling swells: a few long travelling waves with crests running across the view
float swell(vec2 w, float t) {
float y = 0.50 * sin(w.y * 1.15 + w.x * 0.22 + t * 0.55);
y += 0.32 * sin(w.y * 0.62 - w.x * 0.41 + t * 0.37 + 1.3);
y += 0.22 * sin(w.x * 0.83 + w.y * 0.35 - t * 0.46 + 4.1);
y += 0.12 * sin(w.y * 2.3 + w.x * 1.1 + t * 0.9 + 2.2);
y += 0.07 * sin(w.x * 2.7 - w.y * 1.9 + t * 1.2);
return y;
}
float ripple(vec2 rel) {
vec2 uv = vec2((rel.x - ${RX0.toFixed(1)}) / ${(RX1 - RX0).toFixed(1)}, (rel.y - ${RZ0.toFixed(1)}) / ${(RZ1 - RZ0).toFixed(1)});
vec2 e = smoothstep(vec2(0.0), vec2(0.12), uv) * smoothstep(vec2(1.0), vec2(0.88), uv);
// a soft cap: however hard the water is stirred, the surface rounds off instead of spiking
float r = texture(uRip, uv).r * e.x * e.y;
return 0.32 * tanh(r / 0.32);
}
void hide() { gl_Position = vec4(2.0, 2.0, 2.0, 1.0); gl_PointSize = 0.0; vCol = vec3(0.0); vSoft = 0.0; }
void main() {
float id = float(gl_VertexID);
float row = floor(id / uNX);
float i = id - row * uNX;
// rows drift toward the camera and wrap from near back to far
float range = float(${ROWS}) * uDZ;
float a = row * uDZ - uTravel;
float lap = floor(a / range);
float rid = row + lap * float(${ROWS});
float z = ${ZNEAR.toFixed(1)} + (a - lap * range);
z += (h21(vec2(rid, i * 1.31)) - 0.5) * uDZ * 0.3;
// across: each row spans the view at its depth, with a grainy jitter
float u = (i + h21(vec2(i * 0.73, rid * 1.7))) / uNX * 2.0 - 1.0;
float halfW = z * uTanH * 1.25 + 0.6;
float x = u * halfW;
vec2 w = vec2(x, z + uTravel);
float t = uTime * uSpeed * 0.5;
float amp = uAmp * (0.35 + 0.65 * uIntro);
float y = swell(w, t) * amp;
float rip = ripple(vec2(x, z));
y += rip;
y += (h21(vec2(i * 1.9, rid * 2.3)) - 0.5) * 0.035; // a little grain, like spray over the surface
// slope toward the camera: faces turned to us catch more light
float yb = swell(w + vec2(0.0, 0.08), t) * amp + ripple(vec2(x, z + 0.08));
float face = clamp((y - yb) / 0.08 * 1.6, -1.0, 1.0);
// camera: lean (yaw) and pitch, then a perspective lens
vec3 pc = vec3(x, y - ${CAMH.toFixed(1)}, z);
float cy = cos(uYaw), sy = sin(uYaw);
pc.xz = vec2(pc.x * cy - pc.z * sy, pc.x * sy + pc.z * cy);
float cp = cos(uPitch), sp = sin(uPitch);
vec3 c = vec3(pc.x, pc.y * cp + pc.z * sp, pc.z * cp - pc.y * sp);
if (c.z < 0.25) { hide(); return; }
vec2 ndc = vec2(c.x / (c.z * uTanH), c.y / (c.z * uTanV));
if (abs(ndc.x) > 1.3 || abs(ndc.y) > 1.3) { hide(); return; }
// thin-lens depth of field: the circle of confusion grows with distance from the focal plane
float coc = uK * abs(c.z - uFocus) / c.z * (c.z > uFocus ? 0.4 : 1.0);
float size = max(uCore, length(vec2(uCore, coc)));
float energy = (uCore * uCore) / (size * size);
// blurred particles are thinned out and the rest carry their light, so the fill stays bounded
float keep = clamp(energy * 14.0, 0.05, 1.0);
if (h21(vec2(rid * 2.1 + 0.5, i * 0.37)) > keep) { hide(); return; }
if (size > uMaxSize) { energy *= uMaxSize * uMaxSize / (size * size); size = uMaxSize; }
// light: the crests and our-facing slopes glow, ripples catch the highlight; far rows fade into the dark
float lit = 0.55 + 0.45 * smoothstep(-0.6, 0.9, y / max(amp, 1e-3)) + 0.35 * max(face, 0.0) - 0.2 * max(-face, 0.0);
float spark = h21(vec2(i * 3.7, rid * 0.91));
lit *= 0.55 + 0.9 * spark * spark;
float fog = smoothstep(${ZFAR.toFixed(1)}, ${(ZFAR * 0.4).toFixed(1)}, z) * smoothstep(${ZNEAR.toFixed(1)}, ${(ZNEAR + 0.6).toFixed(1)}, z);
float rise = smoothstep(0.0, 1.0, uIntro * 1.6 - (1.0 - z / ${ZFAR.toFixed(1)}) * 0.6);
float catchHi = clamp(abs(rip) * 10.0 + max(face, 0.0) * 0.25, 0.0, 1.0);
lit *= 1.0 + min(abs(rip) * 6.0, 1.2);
vCol = mix(uColor, uHi, catchHi) * lit * fog * rise * energy / keep;
vSoft = smoothstep(uCore * 1.5, uCore * 6.0, size);
gl_Position = vec4(ndc, 0.0, 1.0);
gl_PointSize = max(size, 1.5);
}`;
const POINTS_FS = `#version 300 es
precision highp float;
in vec3 vCol;
in float vSoft;
out vec4 o;
void main() {
vec2 d = gl_PointCoord * 2.0 - 1.0;
float r2 = dot(d, d);
if (r2 > 1.0) discard;
// a sharp particle is a tiny gaussian; a defocused one is a soft-edged disc, like real bokeh
float dot0 = exp(-r2 * 4.5) * 2.6;
float disc = (1.0 - smoothstep(0.05, 1.0, r2)) * 1.9;
o = vec4(vCol * mix(dot0, disc, vSoft), 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.25, 0.9, l), 1.0);
}`;
const COMPOSITE = `#version 300 es
precision highp float;
uniform sampler2D uScene, uBloom, uWide;
uniform float uGlow;
uniform vec3 uBg;
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.8 + texture(uWide, vUv).rgb * 0.35;
vec3 x = sc + bl * uGlow;
// a soft shoulder keeps hot crests from clipping; blacks stay black
vec3 col = x / (1.0 + max(x - 0.8, 0.0) * 0.9);
col = uBg + col * (1.0 - uBg);
col += (h(gl_FragCoord.xy) + h(gl_FragCoord.yx + 17.0) - 1.0) / 255.0 * step(0.004, max(col.r, max(col.g, col.b)));
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];
}
export function ParticleSea({
color = "#2fb8e6",
highlight = "#c8f6ff",
background = "#000000",
density = 1,
waves = 1,
speed = 1,
blur = 1,
focus = 0.45,
horizon = 0.48,
glow = 0.7,
interactive = true,
scroller,
children,
className,
}: ParticleSeaProps) {
const hostRef = useRef<HTMLDivElement>(null);
const props = useRef({ color, highlight, background, density, waves, speed, blur, focus, horizon, glow, interactive });
const redraw = useRef<(() => void) | null>(null);
useEffect(() => {
props.current = { color, highlight, background, density, waves, speed, blur, focus, horizon, glow, interactive };
redraw.current?.();
}, [color, highlight, background, density, waves, speed, blur, focus, horizon, glow, 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("webgl2", { alpha: false, antialias: false, powerPreference: "high-performance" });
if (!gl) {
const p = props.current;
host.style.background = `radial-gradient(120% 60% at 50% 100%, ${p.color}55, transparent 70%), ${p.background}`;
canvas.remove();
return;
}
const hdr = !!gl.getExtension("EXT_color_buffer_float");
const maxPoint = (gl.getParameter(gl.ALIASED_POINT_SIZE_RANGE) as Float32Array)[1] || 64;
const build = (vs: string, fs: string) => {
const pr = 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(pr, sh);
gl.deleteShader(sh);
}
gl.bindAttribLocation(pr, 0, "aQ");
gl.linkProgram(pr);
if (!gl.getProgramParameter(pr, gl.LINK_STATUS)) console.error(gl.getProgramInfoLog(pr));
return pr;
};
const pointProg = build(POINTS_VS, POINTS_FS);
const blurProg = build(QUAD, BLUR);
const brightProg = build(QUAD, BRIGHT);
const compProg = build(QUAD, COMPOSITE);
const uni = (pr: WebGLProgram, names: string[]) =>
Object.fromEntries(names.map((n) => [n, gl.getUniformLocation(pr, n)])) as Record<string, WebGLUniformLocation | null>;
const UP = uni(pointProg, [
"uNX", "uDZ", "uTravel", "uTime", "uAmp", "uTanV", "uTanH", "uPitch", "uYaw", "uFocus", "uK", "uCore", "uMaxSize",
"uIntro", "uSpeed", "uRip", "uColor", "uHi",
]);
const UB = uni(blurProg, ["uTex", "uStep"]);
const UR = uni(brightProg, ["uTex"]);
const UC = uni(compProg, ["uScene", "uBloom", "uWide", "uGlow", "uBg"]);
const quadVao = gl.createVertexArray();
gl.bindVertexArray(quadVao);
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 pointVao = gl.createVertexArray(); // particles are generated from gl_VertexID: no buffers
const drawQuad = () => {
gl.bindVertexArray(quadVao);
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);
if (hdr) gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, tw, th, 0, gl.RGBA, gl.HALF_FLOAT, null);
else 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;
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);
const sw = canvas.width, sh = canvas.height;
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();
// ---- the water surface: a wave-equation height field (camera-relative, world units) ----
let hNow = new Float32Array(RX * RZ);
let hOld = new Float32Array(RX * RZ);
let hTmp = new Float32Array(RX * RZ);
const ripTex = gl.createTexture()!;
gl.bindTexture(gl.TEXTURE_2D, ripTex);
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.R16F, RX, RZ, 0, gl.RED, gl.FLOAT, hNow);
let ripLive = false;
const cellX = (RX1 - RX0) / RX, cellZ = (RZ1 - RZ0) / RZ;
const poke = (wx: number, wz: number, amt: number, radius: number) => {
const ci = (wx - RX0) / cellX - 0.5, cj = (wz - RZ0) / cellZ - 0.5;
const rr = Math.ceil(radius * 2.5);
for (let j = Math.max(1, Math.floor(cj - rr)); j <= Math.min(RZ - 2, Math.ceil(cj + rr)); j++)
for (let i = Math.max(1, Math.floor(ci - rr)); i <= Math.min(RX - 2, Math.ceil(ci + rr)); i++) {
const d2 = (i - ci) ** 2 + (j - cj) ** 2;
hNow[j * RX + i] += amt * Math.exp(-d2 / (radius * radius));
}
ripLive = true;
};
// drops are pressed into the water over about half a second, never as a one-frame kick
const pending: { x: number; z: number; amt: number; r: number; left: number }[] = [];
const press = (x: number, z: number, amt: number, r: number, steps = 30) => {
pending.push({ x, z, amt: amt / steps, r, left: steps });
ripLive = true;
};
const shiftField = (dz: number) => {
// the sea slides toward the camera with the travel: semi-Lagrangian shift in z
const s = dz / cellZ;
if (Math.abs(s) < 1e-4) return;
for (const buf of [hNow, hOld]) {
for (let j = 0; j < RZ; j++) {
const sj = j + s;
const ja = Math.floor(sj), f = sj - ja;
for (let i = 0; i < RX; i++) {
const va = ja >= 0 && ja < RZ ? buf[ja * RX + i] : 0;
const vb = ja + 1 >= 0 && ja + 1 < RZ ? buf[(ja + 1) * RX + i] : 0;
hTmp[j * RX + i] = va + (vb - va) * f;
}
}
buf.set(hTmp);
}
};
const stepWater = () => {
// c^2 = 0.045 (slow, gliding rings), a little damping so the rings die out like real water
const c2 = 0.045, damp = 0.995;
let live = false;
for (let j = 1; j < RZ - 1; j++)
for (let i = 1; i < RX - 1; i++) {
const k = j * RX + i;
const v = (2 * hNow[k] - hOld[k] + c2 * (hNow[k - 1] + hNow[k + 1] + hNow[k - RX] + hNow[k + RX] - 4 * hNow[k])) * damp;
hTmp[k] = v;
if (v > 2e-4 || v < -2e-4) live = true;
}
const o = hOld;
hOld = hNow;
hNow = hTmp;
hTmp = o;
if (!live) {
hNow.fill(0);
hOld.fill(0);
}
return live;
};
// ---- state ----
const s = { intro: still ? 1 : 0 };
let time = still ? 7 : 0, travel = 0, travelV = 0;
let lastST = 0;
let yaw = 0, focusZ = 0, camLean = 0;
const ptr = { nx: 0, ny: 0, in: false, wx: 0, wz: 0, hit: false, t: 0, lastWx: 0, lastWz: 0, moved: 0 };
let lastActive = performance.now();
let acc = 0;
const cruise = () => 0.1 * Math.max(0, props.current.speed);
const view = () => {
const p = props.current;
const tanV = Math.tan((48 * Math.PI) / 360);
const tanH = tanV * (w / h);
const hf = Math.min(0.95, Math.max(0.05, p.horizon));
const pitch = Math.atan((1 - 2 * hf) * tanV);
return { tanV, tanH, pitch };
};
const idleFocus = () => 2.2 + Math.min(1, Math.max(0, props.current.focus)) * 9;
// cursor ray onto the sea plane, camera-relative world coords
const hitSea = (nx: number, ny: number) => {
const { tanV, tanH, pitch } = view();
const cx = nx * tanH, cyv = ny * tanV, cz = 1;
const cp = Math.cos(pitch), sp = Math.sin(pitch);
const y = cyv * cp - cz * sp;
let z = cyv * sp + cz * cp;
let x = cx;
const cyw = Math.cos(-yaw), syw = Math.sin(-yaw);
[x, z] = [x * cyw - z * syw, x * syw + z * cyw];
if (y > -0.02) return null;
const t = CAMH / -y;
return { x: x * t, z: z * t };
};
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) return;
const p = props.current;
const X = T;
const { tanV, tanH, pitch } = view();
if (focusZ === 0) focusZ = idleFocus();
const nx = Math.round(Math.min(1500, Math.max(320, w * 0.62)) * Math.max(0.2, Math.min(2.5, p.density)));
const count = nx * ROWS;
gl.bindFramebuffer(gl.FRAMEBUFFER, X.scene.fb);
gl.viewport(0, 0, X.scene.w, X.scene.h);
gl.clearColor(0, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.enable(gl.BLEND);
gl.blendFunc(gl.ONE, gl.ONE);
gl.useProgram(pointProg);
gl.uniform1f(UP.uNX, nx);
gl.uniform1f(UP.uDZ, (ZFAR - ZNEAR) / ROWS);
gl.uniform1f(UP.uTravel, travel);
gl.uniform1f(UP.uTime, time);
gl.uniform1f(UP.uAmp, 0.48 * Math.max(0, p.waves));
gl.uniform1f(UP.uTanV, tanV);
gl.uniform1f(UP.uTanH, tanH);
gl.uniform1f(UP.uPitch, pitch + camLean);
gl.uniform1f(UP.uYaw, yaw);
gl.uniform1f(UP.uFocus, focusZ);
gl.uniform1f(UP.uK, 0.04 * canvas.height * Math.max(0, p.blur));
gl.uniform1f(UP.uCore, 1.6 * dpr);
gl.uniform1f(UP.uMaxSize, Math.min(maxPoint, 90 * dpr));
gl.uniform1f(UP.uIntro, s.intro);
gl.uniform1f(UP.uSpeed, Math.max(0, p.speed));
gl.uniform3fv(UP.uColor, rgb(p.color));
gl.uniform3fv(UP.uHi, rgb(p.highlight));
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, ripTex);
gl.uniform1i(UP.uRip, 0);
gl.bindVertexArray(pointVao);
gl.drawArrays(gl.POINTS, 0, count);
gl.disable(gl.BLEND);
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);
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));
gl.uniform3fv(UC.uBg, rgb(p.background));
drawQuad();
gl.activeTexture(gl.TEXTURE0);
};
const ro = new ResizeObserver(() => {
resize();
draw();
});
ro.observe(host);
// ---- cursor: a wake on the water, and the lens follows it ----
const local = (e: PointerEvent) => {
const r = host.getBoundingClientRect();
return { nx: ((e.clientX - r.left) / r.width) * 2 - 1, ny: 1 - ((e.clientY - r.top) / r.height) * 2 };
};
const move = (e: PointerEvent) => {
if (still || !props.current.interactive) return;
const l = local(e);
ptr.nx = l.nx;
ptr.ny = l.ny;
ptr.in = true;
ptr.t = performance.now();
lastActive = ptr.t;
};
const leave = () => {
ptr.in = false;
};
// ---- click: a stone dropped into the sea ----
const down = (e: PointerEvent) => {
if (still || !props.current.interactive) return;
const l = local(e);
const hit = hitSea(l.nx, l.ny);
lastActive = performance.now();
if (!hit) return;
press(hit.x, hit.z, -0.75, 3.4);
press(hit.x, hit.z, 0.25, 6.5);
};
host.addEventListener("pointermove", move);
host.addEventListener("pointerleave", leave);
host.addEventListener("pointerdown", down);
// ---- scroll glides you forward over the swells, coasting with momentum ----
let lastScroll = 0;
const st = still
? null
: ScrollTrigger.create({
trigger: host,
scroller: scroller ?? undefined,
start: "top bottom",
end: "bottom top",
onUpdate: (self) => {
const y = self.scroll();
const dy = y - lastScroll;
lastScroll = y;
lastST = performance.now();
if (!props.current.interactive || Math.abs(dy) > 2000) return;
travelV = Math.max(-4, Math.min(6, travelV + dy * 0.0022));
},
});
if (st) lastScroll = st.scroll();
const wheel = (e: WheelEvent) => {
if (still || !props.current.interactive) return;
if (performance.now() - lastST < 80) return;
const dy = e.deltaMode === 1 ? e.deltaY * 16 : e.deltaY;
travelV = Math.max(-4, Math.min(6, travelV + Math.max(-200, Math.min(200, dy)) * 0.0022));
lastActive = performance.now();
};
host.addEventListener("wheel", wheel, { passive: true });
// ---- loop ----
let visible = true;
let prev = performance.now();
let intro: gsap.core.Tween | null = null;
let nextDrop = 2.5;
const tick = () => {
const now = performance.now();
const dt = Math.min(0.05, (now - prev) / 1000);
prev = now;
if (!visible) return;
const p = props.current;
time += dt;
// a glide: momentum, then a gentle drag back to the slow drift (no springs)
travelV += (cruise() - travelV) * (1 - Math.exp(-dt * 0.5));
const dz = travelV * dt;
travel += dz;
if (travel > 1e5) travel = 0;
// the camera leans toward the cursor and the lens racks focus to where it meets the water
const on = ptr.in && p.interactive;
const ease = 1 - Math.exp(-dt * 0.8);
yaw += ((on ? ptr.nx * 0.07 : 0) - yaw) * ease;
camLean += ((on ? -ptr.ny * 0.025 : 0) - camLean) * ease;
let fTarget = idleFocus();
if (on) {
const hit = hitSea(ptr.nx, ptr.ny);
if (hit) {
fTarget = Math.min(ZFAR * 0.7, Math.max(1.6, Math.hypot(hit.x, hit.z)));
// the wake: drag a finger through the water; faster strokes push more
if (ptr.hit) {
const sp = Math.hypot(hit.x - ptr.lastWx, hit.z - ptr.lastWz) / Math.max(dt, 1e-3);
if (sp > 0.05) poke(hit.x, hit.z, -Math.min(sp, 5) * 0.0035, 3.2);
}
ptr.hit = true;
ptr.lastWx = hit.x;
ptr.lastWz = hit.z;
} else ptr.hit = false;
} else ptr.hit = false;
focusZ += (fTarget - focusZ) * (1 - Math.exp(-dt * 0.65));
// idle: now and then a drop falls somewhere on the sea
if (p.interactive && now - lastActive > 3500) {
nextDrop -= dt;
if (nextDrop <= 0) {
nextDrop = 3 + Math.random() * 3;
press(-4 + Math.random() * 8, 3 + Math.random() * 6, -0.35, 2.8, 36);
}
}
if (ripLive) shiftField(dz);
// fixed 60 Hz water steps
acc += dt;
let live = ripLive;
let n = 0;
while (acc >= 1 / 60 && n < 3) {
acc -= 1 / 60;
n++;
for (let k = pending.length - 1; k >= 0; k--) {
const d = pending[k];
poke(d.x, d.z, d.amt, d.r);
if (--d.left <= 0) pending.splice(k, 1);
}
if (ripLive) live = stepWater() || pending.length > 0;
}
if (n === 3) acc = 0;
if (ripLive) {
gl.bindTexture(gl.TEXTURE_2D, ripTex);
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, RX, RZ, gl.RED, gl.FLOAT, hNow);
}
ripLive = live;
draw();
};
const io = new IntersectionObserver(([en]) => {
visible = en.isIntersecting;
prev = performance.now();
});
if (still) {
draw();
redraw.current = draw;
} else {
travelV = 0.9; // the sea rolls in, slowly
intro = gsap.to(s, { intro: 1, duration: 3.8, ease: "power2.out", delay: 0.05 });
io.observe(host);
gsap.ticker.add(tick);
}
return () => {
redraw.current = null;
gsap.ticker.remove(tick);
intro?.kill();
st?.kill();
io.disconnect();
ro.disconnect();
host.removeEventListener("pointermove", move);
host.removeEventListener("pointerleave", leave);
host.removeEventListener("pointerdown", down);
host.removeEventListener("wheel", wheel);
gsap.killTweensOf(s);
freeTargets();
gl.deleteTexture(ripTex);
gl.deleteProgram(pointProg);
gl.deleteProgram(blurProg);
gl.deleteProgram(brightProg);
gl.deleteProgram(compProg);
gl.deleteBuffer(vbo);
gl.deleteVertexArray(quadVao);
gl.deleteVertexArray(pointVao);
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>
);
}
export default ParticleSea;
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 propscolorstringColour of the particles.default "#2fb8e6"default "#2fb8e6"highlightstringColour the crests and ripples catch.default "#c8f6ff"default "#c8f6ff"backgroundstringSky and deep background.default "#000000"default "#000000"densitynumberHow many particles (1 is about a hundred thousand on a desktop screen).default 1default 1wavesnumberHeight of the swells.default 1default 1speednumberSpeed of the swells and the idle drift.default 1default 1blurnumberLens aperture: how strongly the near and far rows blur.default 1default 1focusnumberWhere the lens focuses when idle, 0 near to 1 far.default 0.45default 0.45horizonnumberWhere the horizon sits, 0 top to 1 bottom.default 0.48default 0.48glownumberStrength of the bloom.default 0.7default 0.7interactivebooleanCursor wake, click drop, focus pull, scroll glide.default truedefault truescrollerElement | nullScroll container, if the page does not scroll the window.childrenReactNodeForeground content, drawn above the sea.classNamestringSize it, e.g. h-screen w-full.