Particle Text
Particle text effect: a title sampled into thousands of canvas particles that the cursor parts and a click detonates and reforms.
- Text Effects
- Music / Events
- React only · 0 deps
- 1 file
Browser support · All evergreen browsers (Canvas 2D). Pauses off-screen; reduced motion renders static particles.
Install. One command, or copy the file.
$
npx shadcn@latest add https://freedesignmd.com/r/particle-text.json- Byte-identical to the preview
- No Tailwind or config needed
- SSR-safe · keyboard accessible
- Respects reduced motion & transparency
components/text/particle-text.tsx
1/**2 * ParticleText, text (or any canvas-drawable mark) sampled into particles.3 * The cursor pushes particles aside and they spring home; a click detonates4 * the word and it reforms. Particles keep a faint idle shimmer.5 *6 * Props7 * - text: string Word or short title. Use "\n" for two lines.8 * - font?: string Canvas font weight + family. Default 800 + system display stack.9 * - colors?: string[] Particle palette, picked by x position. Default warm gradient.10 * - gap?: number Sampling step in CSS px (smaller = more particles). Default auto by size.11 * - radius?: number Cursor influence radius in px. Default 90.12 * - draw?: (ctx, w, h) => void Draw your own mark (logo) in white instead of text.13 * - label?: string Accessible text. Default `text`.14 * - className / style Size the box; the canvas fills it (min-height 240px).15 *16 * Physics: per particle spring to home (k 0.06/frame-normalised, damping 0.86)17 * plus a radial push from the pointer. Typed arrays, one draw call per colour18 * bucket, DPR capped at 2. The loop pauses off-screen and when the tab is19 * hidden. Reduced motion: static particles, no explosion.20 * Accessibility: canvas is aria-hidden; a visually hidden heading/text holds21 * the label. Click / Enter / Space on the focusable stage trigger the burst.22 * Browser support: all evergreen browsers (Canvas 2D).23 */24import { useEffect, useRef } from "react";25import type { CSSProperties } from "react";26 27export interface ParticleTextProps {28 text: string;29 font?: string;30 colors?: string[];31 gap?: number;32 radius?: number;33 draw?: (ctx: CanvasRenderingContext2D, w: number, h: number) => void;34 label?: string;35 className?: string;36 style?: CSSProperties;37}38 39const DISPLAY = '800 100px "SF Pro Display", ui-sans-serif, -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif';40 41export function ParticleText({42 text,43 font = DISPLAY,44 colors = ["#FFD166", "#FF8A5B", "#FF4D8D", "#B66DFF", "#5B8CFF"],45 gap,46 radius = 90,47 draw,48 label,49 className,50 style,51}: ParticleTextProps) {52 const wrap = useRef<HTMLDivElement>(null);53 const cv = useRef<HTMLCanvasElement>(null);54 const burstRef = useRef<() => void>(() => {});55 56 useEffect(() => {57 const host = wrap.current;58 const canvas = cv.current;59 if (!host || !canvas) return;60 const ctx = canvas.getContext("2d");61 if (!ctx) return;62 const reduced = window.matchMedia?.("(prefers-reduced-motion: reduce)").matches ?? false;63 64 let W = 0;65 let H = 0;66 let dpr = 1;67 let n = 0;68 let hx = new Float32Array(0);69 let hy = new Float32Array(0);70 let x = new Float32Array(0);71 let y = new Float32Array(0);72 let vx = new Float32Array(0);73 let vy = new Float32Array(0);74 let col = new Uint8Array(0);75 let sz = new Float32Array(0);76 let ph = new Float32Array(0);77 let px = -1e5;78 let py = -1e5;79 let raf = 0;80 let visible = true;81 let lastT = 0;82 let first = true;83 let burstAt = -1e9;84 85 const sample = () => {86 const r = host.getBoundingClientRect();87 W = Math.max(1, Math.round(r.width));88 H = Math.max(1, Math.round(r.height));89 dpr = Math.min(2, window.devicePixelRatio || 1);90 canvas.width = Math.round(W * dpr);91 canvas.height = Math.round(H * dpr);92 // rasterise mark at 1x into an offscreen canvas93 const off = document.createElement("canvas");94 off.width = W;95 off.height = H;96 const o = off.getContext("2d", { willReadFrequently: true });97 if (!o) return;98 o.fillStyle = "#fff";99 if (draw) draw(o, W, H);100 else {101 const lines = text.split("\n");102 o.font = font;103 const widest = Math.max(...lines.map((l) => o.measureText(l).width));104 const sizeByW = (100 * W * 0.9) / widest;105 const sizeByH = (H * 0.86) / (lines.length * 0.92);106 const fs = Math.min(sizeByW, sizeByH);107 o.font = font.replace(/\d+px/, `${fs.toFixed(1)}px`);108 o.textAlign = "center";109 o.textBaseline = "middle";110 const lh = fs * 0.92;111 lines.forEach((l, i) => o.fillText(l, W / 2, H / 2 + (i - (lines.length - 1) / 2) * lh));112 }113 const data = o.getImageData(0, 0, W, H).data;114 const step = gap ?? Math.max(3, Math.round(Math.sqrt((W * H) / 14000)));115 const pts: number[] = [];116 for (let yy = 0; yy < H; yy += step) {117 for (let xx = (yy / step) % 2 ? step / 2 : 0; xx < W; xx += step) {118 const i = ((yy | 0) * W + (xx | 0)) * 4 + 3;119 if (data[i] > 128) pts.push(xx, yy);120 }121 }122 const prevN = n;123 const ox = x;124 const oy = y;125 n = pts.length / 2;126 hx = new Float32Array(n);127 hy = new Float32Array(n);128 const nx = new Float32Array(n);129 const ny = new Float32Array(n);130 vx = new Float32Array(n);131 vy = new Float32Array(n);132 col = new Uint8Array(n);133 sz = new Float32Array(n);134 ph = new Float32Array(n);135 for (let i = 0; i < n; i++) {136 hx[i] = pts[i * 2];137 hy[i] = pts[i * 2 + 1];138 if (i < prevN) {139 nx[i] = ox[i];140 ny[i] = oy[i];141 } else if (first && !reduced) {142 // initial assembly: particles drift in from a wide ring143 const a = Math.random() * Math.PI * 2;144 const d = Math.max(W, H) * (0.6 + Math.random() * 0.5);145 nx[i] = W / 2 + Math.cos(a) * d;146 ny[i] = H / 2 + Math.sin(a) * d;147 } else {148 nx[i] = hx[i];149 ny[i] = hy[i];150 }151 const t = hx[i] / W + (Math.random() - 0.5) * 0.18;152 col[i] = Math.max(0, Math.min(colors.length - 1, Math.floor(t * colors.length)));153 sz[i] = step * (0.42 + Math.random() * 0.22);154 ph[i] = Math.random() * Math.PI * 2;155 }156 x = nx;157 y = ny;158 first = false;159 };160 161 const render = (t: number) => {162 ctx.setTransform(dpr, 0, 0, dpr, 0, 0);163 ctx.clearRect(0, 0, W, H);164 for (let c = 0; c < colors.length; c++) {165 ctx.fillStyle = colors[c];166 ctx.beginPath();167 for (let i = 0; i < n; i++) {168 if (col[i] !== c) continue;169 const s = sz[i] * (reduced ? 1 : 0.9 + 0.12 * Math.sin(t * 0.002 + ph[i]));170 ctx.rect(x[i] - s / 2, y[i] - s / 2, s, s);171 }172 ctx.fill();173 }174 };175 176 const tick = (t: number) => {177 raf = 0;178 const f = Math.min(2, (t - lastT) / 16.667 || 1);179 lastT = t;180 const R = radius;181 const R2 = R * R;182 // after a burst the spring relaxes and air drag drops, then both ease back183 const since = t - burstAt;184 const relax = since < 1400 ? Math.min(1, Math.max(0.02, (since - 380) / 1000)) : 1;185 const k = 0.06 * f * relax * relax;186 const damp = Math.pow(0.86 + 0.06 * (1 - relax), f);187 for (let i = 0; i < n; i++) {188 const dx = x[i] - px;189 const dy = y[i] - py;190 const d2 = dx * dx + dy * dy;191 if (d2 < R2) {192 const d = Math.sqrt(d2) || 1;193 const push = (1 - d / R) * 7 * f;194 vx[i] += (dx / d) * push;195 vy[i] += (dy / d) * push;196 }197 vx[i] = (vx[i] + (hx[i] - x[i]) * k) * damp;198 vy[i] = (vy[i] + (hy[i] - y[i]) * k) * damp;199 x[i] += vx[i] * f;200 y[i] += vy[i] * f;201 }202 render(t);203 if (visible && !document.hidden) raf = requestAnimationFrame(tick);204 };205 const start = () => {206 if (!raf && visible && !reduced) {207 lastT = performance.now();208 raf = requestAnimationFrame(tick);209 }210 };211 212 sample();213 if (reduced) render(0);214 else start();215 216 const ro = new ResizeObserver(() => {217 const r = host.getBoundingClientRect();218 if (Math.round(r.width) === W && Math.round(r.height) === H) return;219 sample();220 if (reduced) render(0);221 });222 ro.observe(host);223 document.fonts?.ready.then(() => {224 sample();225 if (reduced) render(0);226 }).catch(() => {});227 228 const io = new IntersectionObserver((es) => {229 visible = es.some((e) => e.isIntersecting);230 if (visible) start();231 });232 io.observe(host);233 const onVis = () => !document.hidden && start();234 document.addEventListener("visibilitychange", onVis);235 236 const onMove = (e: PointerEvent) => {237 const r = canvas.getBoundingClientRect();238 px = e.clientX - r.left;239 py = e.clientY - r.top;240 };241 const onLeave = () => {242 px = py = -1e5;243 };244 burstRef.current = () => {245 if (reduced) return;246 const cx = px > -1e4 ? px : W / 2;247 const cy = py > -1e4 ? py : H / 2;248 for (let i = 0; i < n; i++) {249 const dx = x[i] - cx;250 const dy = y[i] - cy;251 const d = Math.hypot(dx, dy) || 1;252 const p = 6 + Math.random() * 9 + Math.min(10, 1600 / (d + 60));253 vx[i] += (dx / d) * p + (Math.random() - 0.5) * 8;254 vy[i] += (dy / d) * p + (Math.random() - 0.5) * 8;255 }256 burstAt = performance.now();257 start();258 };259 host.addEventListener("pointermove", onMove);260 host.addEventListener("pointerleave", onLeave);261 return () => {262 cancelAnimationFrame(raf);263 ro.disconnect();264 io.disconnect();265 document.removeEventListener("visibilitychange", onVis);266 host.removeEventListener("pointermove", onMove);267 host.removeEventListener("pointerleave", onLeave);268 };269 }, [text, font, colors.join(","), gap, radius, draw]); // eslint-disable-line react-hooks/exhaustive-deps270 271 return (272 <div273 ref={wrap}274 className={`fd-pt${className ? " " + className : ""}`}275 style={style}276 role="button"277 tabIndex={0}278 aria-label={`${label ?? text.replace(/\n/g, " ")}, press to scatter`}279 onClick={() => burstRef.current()}280 onKeyDown={(e) => {281 if (e.key === "Enter" || e.key === " ") {282 e.preventDefault();283 burstRef.current();284 }285 }}286 >287 <style>{css}</style>288 <canvas ref={cv} className="fd-pt-cv" aria-hidden="true" />289 </div>290 );291}292 293const css = `294.fd-pt,.fd-pt *{box-sizing:border-box}295.fd-pt{position:relative;display:block;margin:0;padding:0;min-height:240px;width:100%;cursor:pointer;touch-action:pan-y;-webkit-tap-highlight-color:transparent;outline:0;border-radius:20px}296.fd-pt:focus-visible{outline:2px solid rgba(10,132,255,.7);outline-offset:2px}297.fd-pt-cv{position:absolute;inset:0;display:block;width:100%;height:100%;margin:0;padding:0}298`;299 300/* ───────────────────────────── demo ───────────────────────────── */301 302export function Demo() {303 return (304 <div className="fd-ptd">305 <style>{demoCss}</style>306 <ParticleText text="Particles" label="Particles" colors={["#1d1d1f", "#2a2a2e", "#38383d"]} gap={4} className="fd-ptd-stage" />307 </div>308 );309}310 311const F = `ui-sans-serif,-apple-system,BlinkMacSystemFont,"SF Pro Text","Inter","Segoe UI",Roboto,sans-serif`;312 313const demoCss = `314.fd-ptd,.fd-ptd *,.fd-ptd *::before,.fd-ptd *::after{box-sizing:border-box}315.fd-ptd{display:flex;align-items:center;justify-content:center;min-height:100vh;width:100%;margin:0;padding:32px 20px;overflow:hidden;background:#f2f2f2;color:#1d1d1f;font:400 15px/1.4 ${F};letter-spacing:normal}316.fd-ptd .fd-ptd-stage{width:100%;max-width:760px;height:clamp(200px,38vh,300px);margin:0;padding:0}317`;More Text Effects. Same file contract.
Questions about Particle Text
Run npx shadcn@latest add https://freedesignmd.com/r/particle-text.json, or open the page and copy the file into your project. It has zero dependencies besides React. What you copy is the exact file that renders the live demo.
Particle text effect: a title sampled into thousands of canvas particles that the cursor parts and a click detonates and reforms. It is a free, MIT-licensed React text effects component from the freedesignmd library.
Yes. It is free and MIT-licensed, use, modify and ship it in personal and commercial projects without attribution.
All evergreen browsers (Canvas 2D). Pauses off-screen; reduced motion renders static particles.
Yes. It is a plain React component with its own styles, so it runs in Next.js, Vite, Remix and Lovable projects, and it needs no Tailwind setup. It has zero dependencies besides React.


