{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "cursor-trail",
  "type": "registry:component",
  "title": "Cursor Trail",
  "description": "A fading trail of dots or a tapered ribbon that follows the cursor on a canvas.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "hover",
    "cursor",
    "trail",
    "ribbon",
    "canvas",
    "pointer"
  ],
  "docs": "https://hyperblur.dev/effects/cursor-trail",
  "files": [
    {
      "path": "registry/hyperblur/cursor-trail.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/cursor-trail.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface CursorTrailProps extends React.HTMLAttributes<HTMLDivElement> {\n  color?: string;\n  length?: number;\n  size?: number;\n  fade?: number;\n  variant?: \"dots\" | \"ribbon\";\n}\n\nconst css = `\n.hb-ctr { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }\n.hb-ctr-c { display: block; width: 100%; height: 100%; }\n@media (prefers-reduced-motion: reduce) {\n  .hb-ctr { display: none; }\n}\n`;\n\nexport function CursorTrail({\n  color = \"#2f7bff\",\n  length = 24,\n  size = 6,\n  fade = 0.9,\n  variant = \"ribbon\",\n  className = \"\",\n  style,\n  ...rest\n}: CursorTrailProps) {\n  const wrapRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n\n  React.useEffect(() => {\n    const wrap = wrapRef.current;\n    const host = wrap?.parentElement;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!wrap || !host || !canvas || !ctx) return;\n\n    const TAU = Math.PI * 2;\n    const STEP = 1000 / 60;\n    const cap = Math.max(2, Math.min(120, Math.floor(length)));\n    const f = Math.min(1, Math.max(0, fade));\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    const pts: number[] = [];\n    let w = 0;\n    let h = 0;\n    let mx = 0;\n    let my = 0;\n    let pointerOn = false;\n    let raf = 0;\n    let prev = 0;\n    let acc = 0;\n\n    const drawDots = (n: number) => {\n      ctx.fillStyle = color;\n      for (let i = 0; i < n; i++) {\n        const t = (i + 1) / n;\n        ctx.globalAlpha = 1 - f * (1 - t);\n        ctx.beginPath();\n        ctx.arc(pts[i * 2], pts[i * 2 + 1], size * 0.5 * (0.3 + 0.7 * t), 0, TAU);\n        ctx.fill();\n      }\n      ctx.globalAlpha = 1;\n    };\n\n    const drawRibbon = (n: number) => {\n      const sx: number[] = [pts[0]];\n      const sy: number[] = [pts[1]];\n      const st: number[] = [0];\n      for (let i = 0; i < n; i++) {\n        const cx = pts[i * 2];\n        const cy = pts[i * 2 + 1];\n        const ax = i === 0 ? cx : (pts[(i - 1) * 2] + cx) / 2;\n        const ay = i === 0 ? cy : (pts[(i - 1) * 2 + 1] + cy) / 2;\n        const bx = i === n - 1 ? cx : (cx + pts[(i + 1) * 2]) / 2;\n        const by = i === n - 1 ? cy : (cy + pts[(i + 1) * 2 + 1]) / 2;\n        for (let k = 1; k <= 4; k++) {\n          const u = k / 4;\n          const v = 1 - u;\n          sx.push(v * v * ax + 2 * v * u * cx + u * u * bx);\n          sy.push(v * v * ay + 2 * v * u * cy + u * u * by);\n          st.push((i + u) / n);\n        }\n      }\n      const m = sx.length;\n      const lx: number[] = [];\n      const ly: number[] = [];\n      const rx: number[] = [];\n      const ry: number[] = [];\n      let nx = 0;\n      let ny = 1;\n      for (let j = 0; j < m; j++) {\n        const a = j === 0 ? 0 : j - 1;\n        const b = j === m - 1 ? m - 1 : j + 1;\n        const dx = sx[b] - sx[a];\n        const dy = sy[b] - sy[a];\n        const len = Math.hypot(dx, dy);\n        if (len > 1e-6) {\n          nx = -dy / len;\n          ny = dx / len;\n        }\n        const hw = size * 0.5 * st[j];\n        lx.push(sx[j] + nx * hw);\n        ly.push(sy[j] + ny * hw);\n        rx.push(sx[j] - nx * hw);\n        ry.push(sy[j] - ny * hw);\n      }\n      ctx.fillStyle = color;\n      ctx.beginPath();\n      ctx.moveTo(lx[0], ly[0]);\n      for (let j = 1; j < m; j++) ctx.lineTo(lx[j], ly[j]);\n      for (let j = m - 1; j >= 0; j--) ctx.lineTo(rx[j], ry[j]);\n      ctx.closePath();\n      ctx.fill();\n      ctx.beginPath();\n      ctx.arc(sx[m - 1], sy[m - 1], size * 0.5, 0, TAU);\n      ctx.fill();\n      ctx.globalCompositeOperation = \"destination-in\";\n      if (Math.hypot(sx[m - 1] - sx[0], sy[m - 1] - sy[0]) > 1) {\n        const g = ctx.createLinearGradient(sx[0], sy[0], sx[m - 1], sy[m - 1]);\n        g.addColorStop(0, `rgba(0,0,0,${1 - f})`);\n        g.addColorStop(1, \"rgba(0,0,0,1)\");\n        ctx.fillStyle = g;\n      } else {\n        ctx.fillStyle = `rgba(0,0,0,${1 - f * 0.5})`;\n      }\n      ctx.fillRect(0, 0, w, h);\n      ctx.globalCompositeOperation = \"source-over\";\n    };\n\n    const draw = () => {\n      ctx.clearRect(0, 0, w, h);\n      const n = pts.length / 2;\n      if (n < 2) return;\n      if (variant === \"dots\") drawDots(n);\n      else drawRibbon(n);\n    };\n\n    const tick = () => {\n      if (pointerOn) {\n        pts.push(mx, my);\n        if (pts.length > cap * 2) pts.splice(0, pts.length - cap * 2);\n      } else if (pts.length) {\n        pts.splice(0, 2);\n      }\n    };\n\n    const alive = () => {\n      const n = pts.length / 2;\n      if (!pointerOn) return n > 0;\n      for (let i = 0; i < n; i++) {\n        if (Math.abs(pts[i * 2] - mx) + Math.abs(pts[i * 2 + 1] - my) > 0.5) return true;\n      }\n      return false;\n    };\n\n    const frame = (now: number) => {\n      acc += Math.min(100, Math.max(0, now - prev));\n      prev = now;\n      let ticks = 0;\n      while (acc >= STEP && ticks < 6) {\n        acc -= STEP;\n        ticks++;\n        tick();\n      }\n      if (acc > STEP) acc = 0;\n      draw();\n      if (alive()) {\n        raf = requestAnimationFrame(frame);\n      } else {\n        raf = 0;\n        pts.length = 0;\n        ctx.clearRect(0, 0, w, h);\n      }\n    };\n\n    const resize = () => {\n      const nw = wrap.clientWidth;\n      const nh = wrap.clientHeight;\n      if (!nw || !nh) return;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      w = nw;\n      h = nh;\n      canvas.width = Math.max(1, Math.round(w * dpr));\n      canvas.height = Math.max(1, Math.round(h * dpr));\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      draw();\n    };\n\n    const move = (ev: Event) => {\n      const e = ev as PointerEvent;\n      if (e.pointerType === \"touch\" || motion.matches) return;\n      const r = wrap.getBoundingClientRect();\n      mx = e.clientX - r.left;\n      my = e.clientY - r.top;\n      if (!pointerOn) {\n        pts.length = 0;\n        pointerOn = true;\n      }\n      if (!raf) {\n        prev = performance.now();\n        acc = STEP;\n        raf = requestAnimationFrame(frame);\n      }\n    };\n\n    const leave = () => {\n      pointerOn = false;\n      if (pts.length && !raf) {\n        prev = performance.now();\n        acc = 0;\n        raf = requestAnimationFrame(frame);\n      }\n    };\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(wrap);\n    host.addEventListener(\"pointermove\", move, { passive: true });\n    host.addEventListener(\"pointerleave\", leave);\n    resize();\n\n    return () => {\n      if (raf) cancelAnimationFrame(raf);\n      ro.disconnect();\n      host.removeEventListener(\"pointermove\", move);\n      host.removeEventListener(\"pointerleave\", leave);\n    };\n  }, [color, length, size, fade, variant]);\n\n  return (\n    <div ref={wrapRef} className={`hb-ctr ${className}`} style={style} aria-hidden=\"true\" {...rest}>\n      <style href=\"hb-cursor-trail\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-ctr-c\" />\n    </div>\n  );\n}\n"
    }
  ]
}