Image Trail
Image trail cursor effect: cards spawn along the cursor path, pop in with a tilt and fall away. A gallery you move through.
- Cursors & Interactions
- Photography
- React only · 0 deps
- 1 file
Browser support · All evergreen browsers (Web Animations API). Touch spawns along drags; off with reduced motion.
Install. One command, or copy the file.
$
npx shadcn@latest add https://freedesignmd.com/r/image-trail.json- Byte-identical to the preview
- No Tailwind or config needed
- SSR-safe · keyboard accessible
- Respects reduced motion & transparency
components/interactions/image-trail.tsx
1/**2 * ImageTrail, as the pointer travels across an area, image cards spawn at3 * fixed distance intervals along its path, pop in with a scale + rotation,4 * hold briefly, then fall away and fade. The number of live cards is capped5 * so long gestures never pile up DOM.6 *7 * Props8 * - items: ReactNode[] Card contents (images, <img>, SVG, gradients…). Cycled in order.9 * - spacing?: number Pointer travel in px between spawns. Default 90.10 * - max?: number Max cards alive at once. Default 12.11 * - life?: number ms a card lives. Default 1100.12 * - size?: [number, number] Card size [w, h] in px. Default [220, 280] (scaled to 0.62 under 640px).13 * - rotate?: number Max random tilt in degrees. Default 10.14 * - children Content of the area (e.g. a headline); stays above or below via `layer`.15 * - layer?: "under" | "over" Cards below or above children. Default "under".16 * - className / style17 * CSS vars: --fd-it-radius (card radius, default 14px), --fd-it-shadow.18 *19 * Performance: cards are a pooled set of absolutely positioned elements,20 * animated with the Web Animations API (transform/opacity only). Pointer21 * moves are coalesced into one rAF. Nothing runs while the pointer is idle.22 * Touch: spawns along drags. Reduced motion: no trail (the area still works).23 * Accessibility: the cards are decorative (aria-hidden); put real content in children.24 * Browser support: all evergreen browsers (Web Animations API).25 *26 * Demo images: /img/lux/{dunes,hotel-golden,still-vase,bamboo,lantern,hotel-dusk,still-tea}-800.webp and27 * /img/people/p3.webp, included in the repo's public/img, copy them next to your page or swap in your own.28 */29import { useEffect, useRef } from "react";30import type { CSSProperties, ReactNode } from "react";31 32export interface ImageTrailProps {33 items: ReactNode[];34 spacing?: number;35 max?: number;36 life?: number;37 size?: [number, number];38 rotate?: number;39 layer?: "under" | "over";40 children?: ReactNode;41 className?: string;42 style?: CSSProperties;43}44 45export function ImageTrail({ items, spacing = 90, max = 12, life = 1100, size = [220, 280], rotate = 10, layer = "under", children, className, style }: ImageTrailProps) {46 const root = useRef<HTMLDivElement>(null);47 const pool = useRef<(HTMLDivElement | null)[]>([]);48 49 useEffect(() => {50 const el = root.current;51 if (!el) return;52 if (window.matchMedia?.("(prefers-reduced-motion: reduce)").matches) return;53 const cards = pool.current.filter(Boolean) as HTMLDivElement[];54 if (!cards.length) return;55 let lastX = NaN;56 let lastY = NaN;57 let acc = 0;58 let next = 0;59 let z = 1;60 let pending: { x: number; y: number } | null = null;61 let raf = 0;62 const alive: { card: HTMLDivElement; anim: Animation }[] = [];63 64 const spawn = (x: number, y: number, vx: number, vy: number) => {65 const card = cards[next % cards.length];66 const content = next % items.length;67 next++;68 // only show the slot for this content69 card.dataset.i = String(content);70 const k = el.clientWidth < 640 ? 0.62 : 1;71 const w = size[0] * k;72 const h = size[1] * k;73 card.style.width = `${w}px`;74 card.style.height = `${h}px`;75 card.style.zIndex = String(z++);76 const rot = (Math.random() * 2 - 1) * rotate;77 const drift = Math.min(1, Math.hypot(vx, vy) / 40);78 const dx = vx * 1.6 * drift;79 const dy = vy * 1.6 * drift + 28;80 const x0 = x - w / 2;81 const y0 = y - h / 2;82 const old = alive.findIndex((a) => a.card === card);83 if (old >= 0) {84 alive[old].anim.cancel();85 alive.splice(old, 1);86 }87 const anim = card.animate(88 [89 { transform: `translate3d(${x0}px,${y0}px,0) scale(.35) rotate(${rot * 2}deg)`, opacity: 0, offset: 0 },90 { transform: `translate3d(${x0}px,${y0}px,0) scale(1.04) rotate(${rot}deg)`, opacity: 1, offset: 0.16, easing: "cubic-bezier(.22,1,.36,1)" },91 { transform: `translate3d(${x0}px,${y0}px,0) scale(1) rotate(${rot}deg)`, opacity: 1, offset: 0.5, easing: "cubic-bezier(.65,0,.35,1)" },92 { transform: `translate3d(${x0 + dx}px,${y0 + dy}px,0) scale(.82) rotate(${rot * 1.4}deg)`, opacity: 0, offset: 1 },93 ],94 { duration: life, easing: "linear", fill: "both" },95 );96 alive.push({ card, anim });97 anim.onfinish = () => {98 const i = alive.findIndex((a) => a.anim === anim);99 if (i >= 0) alive.splice(i, 1);100 };101 // queue limit: fade out the oldest early102 while (alive.length > max) {103 const o = alive.shift()!;104 o.anim.pause();105 const fade = o.card.animate([{ opacity: getComputedStyle(o.card).opacity }, { opacity: 0 }], { duration: 160, fill: "forwards" });106 fade.onfinish = () => {107 o.anim.cancel();108 fade.cancel();109 };110 }111 };112 113 const flush = () => {114 raf = 0;115 if (!pending) return;116 const r = el.getBoundingClientRect();117 const x = pending.x - r.left;118 const y = pending.y - r.top;119 pending = null;120 if (Number.isNaN(lastX)) {121 lastX = x;122 lastY = y;123 return;124 }125 const dx = x - lastX;126 const dy = y - lastY;127 const d = Math.hypot(dx, dy);128 acc += d;129 // spawn at exact distance intervals along the segment130 let travelled = d - (acc - spacing);131 while (acc >= spacing) {132 const t = d ? Math.min(1, Math.max(0, travelled / d)) : 1;133 spawn(lastX + dx * t, lastY + dy * t, dx, dy);134 acc -= spacing;135 travelled += spacing;136 }137 lastX = x;138 lastY = y;139 };140 const onMove = (e: PointerEvent) => {141 if (e.pointerType === "touch" && e.buttons === 0) return;142 pending = { x: e.clientX, y: e.clientY };143 if (!raf) raf = requestAnimationFrame(flush);144 };145 const onLeave = () => {146 lastX = lastY = NaN;147 acc = 0;148 };149 el.addEventListener("pointermove", onMove, { passive: true });150 el.addEventListener("pointerleave", onLeave);151 el.addEventListener("pointercancel", onLeave);152 return () => {153 el.removeEventListener("pointermove", onMove);154 el.removeEventListener("pointerleave", onLeave);155 el.removeEventListener("pointercancel", onLeave);156 cancelAnimationFrame(raf);157 alive.forEach((a) => a.anim.cancel());158 };159 }, [items.length, spacing, max, life, size[0], size[1], rotate]); // eslint-disable-line react-hooks/exhaustive-deps160 161 const slots = Math.max(4, max + 2);162 return (163 <div ref={root} className={`fd-it${className ? " " + className : ""}`} data-layer={layer} style={style}>164 <style>{css}</style>165 <div className="fd-it-cards" aria-hidden="true">166 {Array.from({ length: slots }, (_, s) => (167 <div key={s} className="fd-it-card" ref={(n) => { pool.current[s] = n; }}>168 {items.map((it, i) => (169 <div key={i} className="fd-it-slot" data-slot={i}>{it}</div>170 ))}171 </div>172 ))}173 </div>174 <div className="fd-it-content">{children}</div>175 </div>176 );177}178 179const css = `180.fd-it,.fd-it *,.fd-it *::before,.fd-it *::after{box-sizing:border-box}181.fd-it{--fd-it-radius:14px;--fd-it-shadow:0 24px 48px -16px rgba(0,0,0,.45),0 2px 6px rgba(0,0,0,.18);182 position:relative;display:block;margin:0;padding:0;overflow:hidden;isolation:isolate;touch-action:pan-y}183.fd-it-cards{position:absolute;inset:0;margin:0;padding:0;pointer-events:none;z-index:0}184.fd-it[data-layer="over"] .fd-it-cards{z-index:2}185.fd-it-content{position:relative;z-index:1;margin:0;padding:0}186.fd-it-card{position:absolute;left:0;top:0;margin:0;padding:0;opacity:0;border-radius:var(--fd-it-radius);overflow:hidden;box-shadow:var(--fd-it-shadow);will-change:transform,opacity;187 transform:translate3d(-9999px,0,0)}188.fd-it-slot{display:none;position:absolute;inset:0;margin:0;padding:0}189${Array.from({ length: 32 }, (_, i) => `.fd-it-card[data-i="${i}"] .fd-it-slot[data-slot="${i}"]`).join(",")}{display:block}190.fd-it-slot > *{display:block;width:100%;height:100%;margin:0;object-fit:cover}191`;192 193/* ───────────────────────────── demo ───────────────────────────── */194 195const SHOTS = [196 { src: "/demo/butterfly-p.webp", bg: "#dcd6c8" },197 { src: "/demo/meadow-p.webp", bg: "#cfd8c4" },198 { src: "/demo/ridge-p.webp", bg: "#c9ced8" },199 { src: "/demo/sunset-p.webp", bg: "#e6cfc2" },200 { src: "/demo/balloons-p.webp", bg: "#cfd9e4" },201 { src: "/demo/eagle-p.webp", bg: "#d6d2cc" },202];203 204export function Demo() {205 const cards = SHOTS.map((s) => (206 <div key={s.src} style={{ position: "relative", width: "100%", height: "100%", margin: 0, padding: 0, background: s.bg }}>207 <img208 src={s.src}209 width={480}210 height={640}211 alt=""212 draggable={false}213 decoding="async"214 onError={(e) => { e.currentTarget.style.display = "none"; }}215 style={{ display: "block", width: "100%", height: "100%", maxWidth: "none", margin: 0, padding: 0, objectFit: "cover" }}216 />217 </div>218 ));219 return (220 <div className="fd-itd">221 <style>{demoCss}</style>222 <ImageTrail items={cards} className="fd-itd-area" size={[190, 250]} spacing={96} max={12} life={1200} />223 </div>224 );225}226 227const demoCss = `228.fd-itd,.fd-itd *,.fd-itd *::before,.fd-itd *::after{box-sizing:border-box}229.fd-itd{margin:0;padding:0;min-height:100vh;width:100%;background:#f2f2f2;color:#1d1d1f;font:400 15px/1.4 ui-sans-serif,-apple-system,BlinkMacSystemFont,"SF Pro Text","Inter","Segoe UI",Roboto,sans-serif}230.fd-itd .fd-itd-area{--fd-it-radius:12px;--fd-it-shadow:0 18px 40px -18px rgba(0,0,0,.35),0 1px 3px rgba(0,0,0,.1);width:100%;height:100vh;min-height:420px}231`;More Cursors & Interactions. Same file contract.
Questions about Image Trail
Run npx shadcn@latest add https://freedesignmd.com/r/image-trail.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.
Image trail cursor effect: cards spawn along the cursor path, pop in with a tilt and fall away. A gallery you move through. It is a free, MIT-licensed React cursors & interactions 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 (Web Animations API). Touch spawns along drags; off with reduced motion.
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.


