Particle Reveal
The page renders as fine grayscale dust. Bring the cursor close and the grains merge back into crisp, full-color UI.
Move your cursor over the scene to reveal the UI.
Install
npx shadcn@latest add @canvas-ui/particle-reveal-reactOr copy the source below into your project. Prefer WGSL? Switch the renderer above to the WebGPU build.
Dependencies
The install command above adds these automatically. If you copy the source by hand, install them yourself.
# No dependencies for the WebGL build.Code
"use client";
import {
useEffect,
useRef,
useState,
useSyncExternalStore,
type ReactNode,
} from "react";
import { createRectCache } from "../rect-cache";
export interface ParticleRevealOptions {
/** Reveal radius around the cursor in CSS pixels. */
radius?: number;
/** Feather of the reveal edge as a fraction of the radius (0 to 1). */
softness?: number;
/** Particle grain size in CSS pixels. */
size?: number;
/** How far grains wander from their home pixel in CSS pixels. Bright content spawns the farthest specks. */
scatter?: number;
/** Speed of the idle grain shimmer (0 freezes the dust). */
drift?: number;
/** Chromatic aberration strength at the reveal edge in CSS pixels. */
aberration?: number;
/** How strongly unrevealed content smears around the reveal edge in CSS pixels. */
bend?: number;
/** How strongly dust specks stand out from the background (0 to 1). */
fade?: number;
/** Contrast against the background above which a pixel counts as UI and dissolves into dust. Pixels close to the background color are left untouched. */
threshold?: number;
/** Color of the backdrop behind the content, as any CSS color. Used to tell UI pixels apart from empty space. */
background?: string;
/** Seconds the reveal takes to catch up with the cursor. Higher feels more damped. */
smoothing?: number;
}
export interface ParticleRevealElements {
/** Canvas with layoutsubtree that hosts the HTML content. */
source: HTMLCanvasElement;
/** The element inside the source canvas that gets captured. */
content: HTMLElement;
/** Canvas the WebGL effect renders to. */
output: HTMLCanvasElement;
}
export interface ParticleRevealInstance {
/** Update effect options live. */
setOptions: (options: ParticleRevealOptions) => void;
/** Re-read canvas size. Call when the element is resized. */
resize: () => void;
/** Stop the loop and release all GPU resources. */
destroy: () => void;
}
const DEFAULTS: Required<ParticleRevealOptions> = {
radius: 500,
softness: 0.75,
size: 1,
scatter: 25,
drift: 1,
aberration: 40,
bend: 50,
fade: 0.85,
threshold: 0.1,
background: "#000000",
smoothing: 0.25,
};
type PaintableCanvas = HTMLCanvasElement & {
onpaint?: (() => void) | null;
requestPaint?: () => void;
};
type ElementImageContext = CanvasRenderingContext2D & {
drawElementImage?: (element: Element, x: number, y: number) => void;
};
const VERT = `#version 300 es
precision highp float;
layout(location = 0) in vec2 aPos;
out vec2 vUv;
void main () {
vUv = aPos * 0.5 + 0.5;
gl_Position = vec4(aPos, 0.0, 1.0);
}`;
const FRAG = `#version 300 es
precision highp float;
in vec2 vUv;
out vec4 outColor;
uniform sampler2D uContent;
uniform vec2 uRes;
uniform float uDpr;
uniform vec2 uPointer;
uniform float uActive;
uniform float uRadius;
uniform float uSoftness;
uniform float uSize;
uniform float uScatter;
uniform float uDrift;
uniform float uAberration;
uniform float uBend;
uniform float uFade;
uniform float uThreshold;
uniform vec3 uBg;
uniform float uTime;
uniform float uMaxX;
uniform float uCrisp;
float hash (vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * 0.1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
vec4 samp (vec2 p) {
vec2 uv = p / uRes;
uv = clamp(uv, vec2(0.001), vec2(uMaxX - 0.001, 0.999));
return texture(uContent, uv);
}
void main () {
vec2 pc = vec2(vUv.x, 1.0 - vUv.y) * uRes;
if (pc.x > uMaxX * uRes.x) {
outColor = vec4(0.0);
return;
}
if (uCrisp > 0.5) {
outColor = samp(pc);
return;
}
float dist = length(pc - uPointer);
float radius = max(uRadius, 1.0);
float inner = radius * (1.0 - clamp(uSoftness, 0.02, 1.0));
float e = (1.0 - smoothstep(inner, radius, dist)) * uActive;
float band = radius * 0.9;
float ring = smoothstep(inner, radius, dist)
* (1.0 - smoothstep(radius, radius + band, dist))
* uActive;
vec2 dir = (pc - uPointer) / max(dist, 1e-3);
vec2 tang = vec2(-dir.y, dir.x);
vec2 warp = (dir * -1.0 + tang * 0.6) * uBend * ring;
float ca = uAberration * ring;
float cellPx = max(uSize, 0.5) * uDpr;
vec2 cell = floor(gl_FragCoord.xy / cellPx);
float n1 = hash(cell);
float n2 = hash(cell + vec2(3.1, 7.7));
float n3 = hash(cell + vec2(9.3, 1.3));
float ft = floor(uTime * (2.0 + uDrift * 6.0));
float n4 = hash(cell + vec2(ft * 0.613, ft * 0.831));
float g0 = uThreshold * 0.6;
float g1 = uThreshold * 1.6 + 0.01;
vec3 lw = vec3(0.299, 0.587, 0.114);
vec2 bp = pc + warp;
vec4 bR = samp(bp + dir * ca);
vec4 bC = samp(bp);
vec4 bB = samp(bp - dir * ca);
vec3 baseRgb = vec3(bR.r, bC.g, bB.b);
float uiHome = smoothstep(g0, g1, dot(abs(baseRgb - uBg), lw));
float rad = uScatter * pow(n1, 2.5) * (1.0 - e);
float ang = n2 * 6.2832 + uTime * uDrift * (0.5 + n3 * 1.5);
vec2 dustP = bp + vec2(cos(ang), sin(ang)) * rad;
vec4 dR = samp(dustP + dir * ca);
vec4 dC = samp(dustP);
vec4 dB = samp(dustP - dir * ca);
vec3 dustRgb = vec3(dR.r, dC.g, dB.b);
float lumD = dot(dustRgb, lw);
float dDust = dot(abs(dustRgb - uBg), lw);
float gate = smoothstep(g0, g1, dDust);
float falloff = 1.0 - 0.7 * rad / max(uScatter, 1.0);
float prob = clamp(gate * (0.15 + 1.2 * sqrt(dDust)) * falloff, 0.0, 1.0) * uiHome;
float speck = step(n4 * 0.999, prob);
float shade = pow(lumD, 0.4) * (0.8 + 0.4 * n3);
vec3 dustCol = mix(uBg, vec3(shade), clamp(uFade, 0.0, 1.0));
vec3 unrevealed = mix(mix(baseRgb, uBg, uiHome), dustCol, speck);
vec3 col = mix(unrevealed, baseRgb, e);
float alpha = mix(bC.a, dC.a, speck * (1.0 - e));
outColor = vec4(col, alpha);
}`;
let colorProbe: CanvasRenderingContext2D | null = null;
function parseColor(input: string): [number, number, number] {
if (typeof document === "undefined") return [0, 0, 0];
if (!colorProbe) {
const probe = document.createElement("canvas");
probe.width = 1;
probe.height = 1;
colorProbe = probe.getContext("2d", { willReadFrequently: true });
}
if (!colorProbe) return [0, 0, 0];
colorProbe.fillStyle = "#000000";
colorProbe.fillStyle = input;
colorProbe.clearRect(0, 0, 1, 1);
colorProbe.fillRect(0, 0, 1, 1);
const data = colorProbe.getImageData(0, 0, 1, 1).data;
return [data[0] / 255, data[1] / 255, data[2] / 255];
}
export function supportsHtmlInCanvas(): boolean {
if (typeof document === "undefined") return false;
const probe = document.createElement("canvas") as PaintableCanvas;
const ctx = probe.getContext("2d") as ElementImageContext | null;
return Boolean(
ctx &&
typeof ctx.drawElementImage === "function" &&
typeof probe.requestPaint === "function",
);
}
export function createParticleReveal(
elements: ParticleRevealElements,
options: ParticleRevealOptions = {},
): ParticleRevealInstance | null {
const config = { ...DEFAULTS, ...options };
const { source, content, output } = elements;
const gl = output.getContext("webgl2", {
alpha: true,
depth: false,
stencil: false,
antialias: false,
premultipliedAlpha: false,
});
if (!gl || gl.isContextLost()) return null;
const sourceCtx = source.getContext("2d") as ElementImageContext | null;
const paintable = source as PaintableCanvas;
const htmlInCanvas = Boolean(
sourceCtx &&
typeof sourceCtx.drawElementImage === "function" &&
typeof paintable.requestPaint === "function",
);
let contentDirty = false;
let wake = () => {};
if (htmlInCanvas) {
paintable.onpaint = () => {
try {
sourceCtx!.reset();
sourceCtx!.drawElementImage!(content, 0, 0);
contentDirty = true;
wake();
} catch {}
};
}
function compile(type: number, text: string): WebGLShader {
const shader = gl!.createShader(type)!;
gl!.shaderSource(shader, text);
gl!.compileShader(shader);
if (!gl!.getShaderParameter(shader, gl!.COMPILE_STATUS)) {
console.error(
"ParticleReveal shader error:",
gl!.getShaderInfoLog(shader),
);
}
return shader;
}
const vertexShader = compile(gl.VERTEX_SHADER, VERT);
const fragmentShader = compile(gl.FRAGMENT_SHADER, FRAG);
const program = gl.createProgram()!;
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
const uniforms: Record<string, WebGLUniformLocation> = {};
const count = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
for (let i = 0; i < count; i++) {
const info = gl.getActiveUniform(program, i)!;
uniforms[info.name] = gl.getUniformLocation(program, info.name)!;
}
const quad = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, quad);
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]),
gl.STATIC_DRAW,
);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
const contentTexture = gl.createTexture()!;
gl.bindTexture(gl.TEXTURE_2D, contentTexture);
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,
1,
1,
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
new Uint8Array([0, 0, 0, 0]),
);
let contentMaxX = 1;
function syncCanvasSize() {
const dpr = Math.min(window.devicePixelRatio || 1, 2);
const width = Math.max(1, Math.round(output.clientWidth * dpr));
const height = Math.max(1, Math.round(output.clientHeight * dpr));
if (output.width !== width || output.height !== height) {
output.width = width;
output.height = height;
}
contentMaxX = Math.min(
1,
Math.max(0.05, content.clientWidth / Math.max(output.clientWidth, 1)),
);
if (htmlInCanvas) {
const cssWidth = Math.max(1, Math.round(source.clientWidth));
const cssHeight = Math.max(1, Math.round(source.clientHeight));
if (source.width !== cssWidth * dpr || source.height !== cssHeight * dpr) {
source.width = cssWidth * dpr;
source.height = cssHeight * dpr;
}
paintable.requestPaint!();
}
}
const pointer = {
x: -1e5,
y: -1e5,
tx: -1e5,
ty: -1e5,
active: 0,
target: 0,
};
let time = 0;
let bgKey = "";
let bg: [number, number, number] = [0, 0, 0];
const motionQuery = window.matchMedia("(prefers-reduced-motion: reduce)");
let reducedMotion = motionQuery.matches;
syncCanvasSize();
function uploadContent() {
if (!htmlInCanvas || !contentDirty) return;
contentDirty = false;
gl!.bindTexture(gl!.TEXTURE_2D, contentTexture);
gl!.texImage2D(
gl!.TEXTURE_2D,
0,
gl!.RGBA,
gl!.RGBA,
gl!.UNSIGNED_BYTE,
source,
);
}
function render() {
uploadContent();
const w = Math.max(output.clientWidth, 1);
const h = Math.max(output.clientHeight, 1);
const dpr = output.width / w;
gl!.useProgram(program);
gl!.activeTexture(gl!.TEXTURE0);
gl!.bindTexture(gl!.TEXTURE_2D, contentTexture);
gl!.uniform1i(uniforms.uContent, 0);
gl!.uniform2f(uniforms.uRes, w, h);
gl!.uniform1f(uniforms.uDpr, dpr);
gl!.uniform2f(uniforms.uPointer, pointer.x, pointer.y);
gl!.uniform1f(uniforms.uActive, pointer.active);
gl!.uniform1f(uniforms.uRadius, Math.max(config.radius, 1));
gl!.uniform1f(uniforms.uSoftness, config.softness);
gl!.uniform1f(uniforms.uSize, Math.max(config.size, 0.5));
gl!.uniform1f(uniforms.uScatter, Math.max(config.scatter, 0));
gl!.uniform1f(uniforms.uDrift, Math.max(config.drift, 0));
gl!.uniform1f(uniforms.uAberration, Math.max(config.aberration, 0));
gl!.uniform1f(uniforms.uBend, Math.max(config.bend, 0));
gl!.uniform1f(uniforms.uFade, config.fade);
gl!.uniform1f(uniforms.uThreshold, Math.max(config.threshold, 0));
if (config.background !== bgKey) {
bgKey = config.background;
bg = parseColor(config.background);
}
gl!.uniform3f(uniforms.uBg, bg[0], bg[1], bg[2]);
gl!.uniform1f(uniforms.uTime, time);
gl!.uniform1f(uniforms.uMaxX, contentMaxX);
gl!.uniform1f(uniforms.uCrisp, reducedMotion || !htmlInCanvas ? 1 : 0);
gl!.bindFramebuffer(gl!.FRAMEBUFFER, null);
gl!.viewport(0, 0, output.width, output.height);
gl!.drawArrays(gl!.TRIANGLE_STRIP, 0, 4);
}
let raf = 0;
let lastTime = performance.now();
let destroyed = false;
let running = false;
let visible = true;
function frame(now: number) {
if (destroyed) return;
if (!visible) {
running = false;
return;
}
const delta = Math.min((now - lastTime) / 1000, 1 / 30);
lastTime = now;
time += delta;
const tau = Math.max(config.smoothing, 1e-4);
const k = reducedMotion ? 1 : 1 - Math.exp(-delta / tau);
pointer.x += (pointer.tx - pointer.x) * k;
pointer.y += (pointer.ty - pointer.y) * k;
pointer.active += (pointer.target - pointer.active) * k;
render();
const settled =
Math.abs(pointer.tx - pointer.x) < 0.1 &&
Math.abs(pointer.ty - pointer.y) < 0.1 &&
Math.abs(pointer.target - pointer.active) < 1e-3;
if (
settled &&
!contentDirty &&
(reducedMotion || !htmlInCanvas || config.drift <= 0)
) {
pointer.x = pointer.tx;
pointer.y = pointer.ty;
pointer.active = pointer.target;
running = false;
return;
}
raf = requestAnimationFrame(frame);
}
function start() {
if (destroyed || running || !visible) return;
running = true;
lastTime = performance.now();
raf = requestAnimationFrame(frame);
}
wake = start;
start();
function onMotionChange() {
reducedMotion = motionQuery.matches;
start();
}
motionQuery.addEventListener("change", onMotionChange);
const observer = new ResizeObserver(() => {
syncCanvasSize();
start();
});
observer.observe(output);
observer.observe(content);
const intersection = new IntersectionObserver((entries) => {
visible = entries[entries.length - 1]?.isIntersecting ?? true;
if (visible) start();
});
intersection.observe(output);
const listenTarget = output.parentElement ?? output;
const rectCache = createRectCache(output);
function onPointerMove(event: PointerEvent) {
const rect = rectCache.current;
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
if (pointer.target === 0 && pointer.active < 1e-3) {
pointer.x = x;
pointer.y = y;
}
pointer.tx = x;
pointer.ty = y;
pointer.target = 1;
start();
}
function onPointerLeave() {
pointer.target = 0;
start();
}
listenTarget.addEventListener("pointermove", onPointerMove, { passive: true });
listenTarget.addEventListener("pointerleave", onPointerLeave, { passive: true });
return {
setOptions(next) {
if (
!Object.entries(next).some(
([key, value]) =>
config[key as keyof ParticleRevealOptions] !== value,
)
)
return;
Object.assign(config, next);
start();
},
resize() {
syncCanvasSize();
start();
},
destroy() {
destroyed = true;
rectCache.destroy();
cancelAnimationFrame(raf);
observer.disconnect();
intersection.disconnect();
motionQuery.removeEventListener("change", onMotionChange);
listenTarget.removeEventListener("pointermove", onPointerMove);
listenTarget.removeEventListener("pointerleave", onPointerLeave);
gl!.deleteTexture(contentTexture);
gl!.deleteProgram(program);
gl!.deleteShader(vertexShader);
gl!.deleteShader(fragmentShader);
gl!.deleteBuffer(quad);
if (htmlInCanvas) paintable.onpaint = null;
},
};
}
export interface ParticleRevealProps extends ParticleRevealOptions {
children: ReactNode;
className?: string;
style?: React.CSSProperties;
}
const emptySubscribe = () => () => {};
export function ParticleReveal({
children,
className,
style,
...options
}: ParticleRevealProps) {
const sourceRef = useRef<HTMLCanvasElement>(null);
const contentRef = useRef<HTMLDivElement>(null);
const outputRef = useRef<HTMLCanvasElement>(null);
const instanceRef = useRef<ParticleRevealInstance | null>(null);
const [initialOptions] = useState(options);
const [failed, setFailed] = useState(false);
const supported = useSyncExternalStore(
emptySubscribe,
supportsHtmlInCanvas,
() => false,
);
const native = supported && !failed;
useEffect(() => {
const source = sourceRef.current;
const content = contentRef.current;
const output = outputRef.current;
if (!source || !content || !output) return;
instanceRef.current = createParticleReveal(
{ source, content, output },
initialOptions,
);
if (native && !instanceRef.current) setFailed(true);
return () => {
instanceRef.current?.destroy();
instanceRef.current = null;
};
}, [initialOptions, native]);
useEffect(() => {
instanceRef.current?.setOptions(options);
});
return (
<div className={className} style={{ position: "relative", ...style }}>
<canvas
ref={sourceRef}
// @ts-expect-error experimental html-in-canvas attribute
layoutsubtree="true"
suppressHydrationWarning
style={
native
? { position: "absolute", inset: 0, width: "100%", height: "100%" }
: { display: "none" }
}
>
{native ? (
<div
ref={contentRef}
style={{
position: "relative",
width: "100%",
height: "100%",
overflow: "auto",
}}
>
{children}
</div>
) : null}
</canvas>
{!native ? (
<div
ref={contentRef}
style={{
position: "relative",
width: "100%",
height: "100%",
overflow: "auto",
}}
>
{children}
</div>
) : null}
<canvas
ref={outputRef}
aria-hidden
style={{
position: "absolute",
inset: 0,
width: "100%",
height: "100%",
pointerEvents: "none",
}}
/>
</div>
);
}
export default ParticleReveal;
API reference
| Property | Type | Default |
|---|---|---|
radiusReveal radius around the cursor in CSS pixels. | number | 500 |
softnessFeather of the reveal edge as a fraction of the radius (0 to 1). | number | 0.75 |
sizeParticle grain size in CSS pixels. | number | 1 |
scatterHow far grains wander from their home pixel in CSS pixels. Bright content spawns the farthest specks. | number | 25 |
driftSpeed of the idle grain shimmer (0 freezes the dust). | number | 1 |
aberrationChromatic aberration strength at the reveal edge in CSS pixels. | number | 40 |
bendHow strongly unrevealed content smears around the reveal edge in CSS pixels. | number | 50 |
fadeHow strongly dust specks stand out from the background (0 to 1). | number | 0.85 |
thresholdContrast against the background above which a pixel counts as UI and dissolves into dust. Pixels close to the background color are left untouched. | number | 0.1 |
backgroundColor of the backdrop behind the content, as any CSS color. Used to tell UI pixels apart from empty space. | string | "#000000" |
smoothingSeconds the reveal takes to catch up with the cursor. Higher feels more damped. | number | 0.25 |
classNameClasses applied to the wrapper element. | string |
