{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "cursor-ghost",
  "type": "registry:component",
  "title": "Cursor Ghost",
  "description": "A faint ghost that trails the pointer on a spring, leans into its motion, looks where it is going and leaves a fading afterimage.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "hover",
    "cursor",
    "ghost",
    "spring",
    "follower",
    "afterimage",
    "canvas"
  ],
  "docs": "https://hyperblur.dev/effects/cursor-ghost",
  "files": [
    {
      "path": "registry/hyperblur/cursor-ghost.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/cursor-ghost.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface CursorGhostProps extends React.HTMLAttributes<HTMLDivElement> {\n  color?: string;\n  size?: number;\n  opacity?: number;\n  stiffness?: number;\n  damping?: number;\n  trail?: number;\n  idle?: boolean;\n}\n\nconst css = `\n.hb-qi2 { position: relative; width: 100%; height: 100%; overflow: hidden; box-sizing: border-box; isolation: isolate; font-family: inherit; }\n.hb-qi2 *, .hb-qi2 *::before, .hb-qi2 *::after { box-sizing: border-box; }\n.hb-qi2-content { position: relative; z-index: 1; width: 100%; height: 100%; }\n.hb-qi2-canvas { position: absolute; inset: 0; z-index: 2; display: block; width: 100%; height: 100%; pointer-events: none; }\n`;\n\ninterface Pose {\n  x: number;\n  y: number;\n  angle: number;\n  lean: number;\n}\n\nexport function CursorGhost({\n  color = \"#e8ecff\",\n  size = 56,\n  opacity = 0.62,\n  stiffness = 60,\n  damping = 9,\n  trail = 6,\n  idle = true,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: CursorGhostProps) {\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n\n  React.useEffect(() => {\n    const root = rootRef.current;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!root || !canvas || !ctx) return;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const count = Math.max(0, Math.min(16, Math.floor(trail)));\n    const history: Pose[] = [];\n    let w = 0;\n    let h = 0;\n    let px = 0;\n    let py = 0;\n    let x = 0;\n    let y = 0;\n    let vx = 0;\n    let vy = 0;\n    let active = false;\n    let mix = 0;\n    let raf = 0;\n    let last = 0;\n    let t = 0;\n    let since = 0;\n    let visible = true;\n    let placed = false;\n\n    const shape = (pose: Pose, alpha: number, eyes: boolean) => {\n      const s = size;\n      const r = s * 0.42;\n      const top = -s * 0.5;\n      const headY = top + r;\n      const bottom = s * 0.5;\n      const lean = pose.lean;\n      ctx.save();\n      ctx.translate(pose.x, pose.y + Math.sin(t * 2.2) * s * 0.05);\n      ctx.rotate(pose.angle);\n      ctx.globalAlpha = alpha;\n      ctx.beginPath();\n      ctx.moveTo(-r, headY);\n      ctx.arc(0, headY, r, Math.PI, 0);\n      ctx.quadraticCurveTo(r + lean * 0.3, (headY + bottom) / 2, r + lean, bottom);\n      for (let k = 0; k < 3; k++) {\n        const xa = r - ((2 * r) / 3) * k + lean;\n        const xb = r - ((2 * r) / 3) * (k + 1) + lean;\n        const drop = s * 0.13 * (1 + 0.35 * Math.sin(t * 5 + k * 2.1));\n        ctx.quadraticCurveTo((xa + xb) / 2, bottom + drop, xb, bottom);\n      }\n      ctx.quadraticCurveTo(-r + lean * 0.3, (headY + bottom) / 2, -r, headY);\n      ctx.closePath();\n      ctx.fill();\n      if (eyes) {\n        const sp = Math.hypot(vx, vy);\n        const lx = sp > 1 ? (vx / sp) * r * 0.12 : 0;\n        const ly = sp > 1 ? (vy / sp) * r * 0.1 : 0;\n        ctx.shadowBlur = 0;\n        ctx.globalAlpha = Math.min(1, alpha + 0.2);\n        ctx.fillStyle = \"#0a0a0a\";\n        for (const side of [-1, 1]) {\n          ctx.beginPath();\n          ctx.ellipse(side * r * 0.36 + lx, headY + r * 0.05 + ly, r * 0.14, r * 0.2, 0, 0, Math.PI * 2);\n          ctx.fill();\n        }\n      }\n      ctx.restore();\n    };\n\n    const draw = () => {\n      ctx.clearRect(0, 0, w, h);\n      ctx.fillStyle = color;\n      for (let i = 0; i < history.length; i++) {\n        shape(history[i], opacity * 0.2 * ((i + 1) / (history.length + 1)), false);\n      }\n      ctx.shadowColor = color;\n      ctx.shadowBlur = size * 0.45;\n      ctx.fillStyle = color;\n      const pose = {\n        x,\n        y,\n        angle: Math.max(-0.35, Math.min(0.35, vx * 0.0011)),\n        lean: Math.max(-size * 0.3, Math.min(size * 0.3, -vx * 0.04)),\n      };\n      shape(pose, opacity, true);\n      ctx.shadowBlur = 0;\n      return pose;\n    };\n\n    const step = (dt: number) => {\n      t += dt;\n      mix += ((active ? 1 : 0) - mix) * (1 - Math.exp(-dt * 3));\n      const ix = idle ? w * (0.5 + 0.32 * Math.sin(t * 0.47) + 0.06 * Math.sin(t * 1.6)) : px;\n      const iy = idle ? h * (0.5 + 0.26 * Math.sin(t * 0.71 + 1.1)) : py;\n      const tx = ix + (px - ix) * mix;\n      const ty = iy + (py - iy) * mix;\n      vx += ((tx - x) * stiffness - vx * damping) * dt;\n      vy += ((ty - y) * stiffness - vy * damping) * dt;\n      x += vx * dt;\n      y += vy * dt;\n    };\n\n    const frame = (now: number) => {\n      const dt = Math.min(0.04, Math.max(0.001, (now - last) / 1000));\n      last = now;\n      step(dt);\n      const pose = draw();\n      since += dt;\n      if (since > 0.05 && count) {\n        since = 0;\n        history.push(pose);\n        if (history.length > count) history.shift();\n      }\n      raf = requestAnimationFrame(frame);\n    };\n\n    const start = () => {\n      if (raf || reduced || !visible || !w) return;\n      last = performance.now();\n      raf = requestAnimationFrame(frame);\n    };\n    const stop = () => {\n      if (raf) cancelAnimationFrame(raf);\n      raf = 0;\n    };\n\n    const resize = () => {\n      const nw = root.clientWidth;\n      const nh = root.clientHeight;\n      if (!nw || !nh) return;\n      w = nw;\n      h = nh;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\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      if (!placed) {\n        placed = true;\n        px = w / 2;\n        py = h / 2;\n        x = idle ? w * 0.5 : px;\n        y = idle ? h * (0.5 + 0.26 * Math.sin(1.1)) : py;\n      }\n      if (reduced) {\n        x = w * 0.5;\n        y = h * 0.5;\n        vx = 0;\n        vy = 0;\n        draw();\n      } else {\n        start();\n      }\n    };\n\n    const onMove = (e: PointerEvent) => {\n      const r = root.getBoundingClientRect();\n      const s = r.width ? w / r.width : 1;\n      px = (e.clientX - r.left) * s;\n      py = (e.clientY - r.top) * s;\n      active = true;\n    };\n    const onLeave = () => {\n      active = false;\n    };\n    const onUp = (e: PointerEvent) => {\n      if (e.pointerType === \"touch\") onLeave();\n    };\n\n    root.addEventListener(\"pointermove\", onMove);\n    root.addEventListener(\"pointerdown\", onMove);\n    root.addEventListener(\"pointerleave\", onLeave);\n    root.addEventListener(\"pointercancel\", onLeave);\n    root.addEventListener(\"pointerup\", onUp);\n    const ro = new ResizeObserver(resize);\n    ro.observe(root);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible) start();\n      else stop();\n    });\n    io.observe(root);\n    resize();\n\n    return () => {\n      stop();\n      ro.disconnect();\n      io.disconnect();\n      root.removeEventListener(\"pointermove\", onMove);\n      root.removeEventListener(\"pointerdown\", onMove);\n      root.removeEventListener(\"pointerleave\", onLeave);\n      root.removeEventListener(\"pointercancel\", onLeave);\n      root.removeEventListener(\"pointerup\", onUp);\n    };\n  }, [color, size, opacity, stiffness, damping, trail, idle]);\n\n  return (\n    <div ref={rootRef} className={`hb-qi2 ${className}`} style={style} {...rest}>\n      <style href=\"hb-cursor-ghost\" precedence=\"default\">{css}</style>\n      <div className=\"hb-qi2-content\">{children}</div>\n      <canvas ref={canvasRef} className=\"hb-qi2-canvas\" aria-hidden=\"true\" />\n    </div>\n  );\n}\n"
    }
  ]
}