{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "cursor-pixel-trail",
  "type": "registry:component",
  "title": "Cursor Pixel Trail",
  "description": "Cells on a pixel grid light up under the pointer and fade out in hard steps, like an old LED matrix.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "hover",
    "cursor",
    "pixel",
    "grid",
    "trail",
    "retro",
    "canvas"
  ],
  "docs": "https://hyperblur.dev/effects/cursor-pixel-trail",
  "files": [
    {
      "path": "registry/hyperblur/cursor-pixel-trail.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/cursor-pixel-trail.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface CursorPixelTrailProps extends React.HTMLAttributes<HTMLDivElement> {\n  color?: string;\n  cell?: number;\n  gap?: number;\n  steps?: number;\n  stepMs?: number;\n  radius?: number;\n  showGrid?: boolean;\n  idle?: boolean;\n}\n\nconst css = `\n.hb-qi7 { position: relative; width: 100%; height: 100%; overflow: hidden; box-sizing: border-box; isolation: isolate; font-family: inherit; }\n.hb-qi7 *, .hb-qi7 *::before, .hb-qi7 *::after { box-sizing: border-box; }\n.hb-qi7-canvas { position: absolute; inset: 0; z-index: 0; display: block; width: 100%; height: 100%; pointer-events: none; }\n.hb-qi7-content { position: relative; z-index: 1; width: 100%; height: 100%; }\n`;\n\nexport function CursorPixelTrail({\n  color = \"#2f7bff\",\n  cell = 14,\n  gap = 2,\n  steps = 6,\n  stepMs = 70,\n  radius = 1,\n  showGrid = true,\n  idle = true,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: CursorPixelTrailProps) {\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 size = Math.max(4, cell);\n    const pad = Math.max(0, Math.min(size - 2, gap));\n    const levels = Math.max(1, Math.floor(steps));\n    const tick = Math.max(16, stepMs) / 1000;\n    const rr = Math.max(0, radius);\n    let cols = 0;\n    let rows = 0;\n    let grid = new Uint8Array(0);\n    let w = 0;\n    let h = 0;\n    let px = 0;\n    let py = 0;\n    let lx = 0;\n    let ly = 0;\n    let active = false;\n    let mix = 0;\n    let raf = 0;\n    let last = 0;\n    let t = 0;\n    let acc = 0;\n    let visible = true;\n    let placed = false;\n\n    const stamp = (x: number, y: number) => {\n      const c0 = Math.floor(x / size);\n      const r0 = Math.floor(y / size);\n      const span = Math.ceil(rr);\n      for (let dy = -span; dy <= span; dy++) {\n        for (let dx = -span; dx <= span; dx++) {\n          if (dx * dx + dy * dy > rr * rr + 0.01) continue;\n          const c = c0 + dx;\n          const r = r0 + dy;\n          if (c < 0 || r < 0 || c >= cols || r >= rows) continue;\n          grid[r * cols + c] = levels;\n        }\n      }\n    };\n\n    const step = (dt: number) => {\n      t += dt;\n      mix += ((active ? 1 : 0) - mix) * (1 - Math.exp(-dt * 4));\n      const ix = idle ? w * (0.5 + 0.38 * Math.sin(t * 0.7) + 0.04 * Math.sin(t * 2.3)) : px;\n      const iy = idle ? h * (0.5 + 0.32 * Math.sin(t * 1.15 + 0.9)) : py;\n      const tx = ix + (px - ix) * mix;\n      const ty = iy + (py - iy) * mix;\n      acc += dt;\n      while (acc >= tick) {\n        acc -= tick;\n        for (let i = 0; i < grid.length; i++) if (grid[i]) grid[i]--;\n      }\n      const dx = tx - lx;\n      const dy = ty - ly;\n      const d = Math.hypot(dx, dy);\n      if (d > 0.5) {\n        const k = Math.max(1, Math.ceil(d / (size * 0.5)));\n        for (let i = 1; i <= k; i++) stamp(lx + (dx * i) / k, ly + (dy * i) / k);\n      }\n      lx = tx;\n      ly = ty;\n    };\n\n    const draw = () => {\n      ctx.clearRect(0, 0, w, h);\n      const inner = size - pad;\n      const off = pad / 2;\n      if (showGrid) {\n        ctx.fillStyle = \"rgba(255,255,255,0.03)\";\n        for (let r = 0; r < rows; r++) {\n          for (let c = 0; c < cols; c++) ctx.fillRect(c * size + off, r * size + off, inner, inner);\n        }\n      }\n      ctx.fillStyle = color;\n      for (let r = 0; r < rows; r++) {\n        for (let c = 0; c < cols; c++) {\n          const v = grid[r * cols + c];\n          if (!v) continue;\n          ctx.globalAlpha = v / levels;\n          ctx.fillRect(c * size + off, r * size + off, inner, inner);\n        }\n      }\n      ctx.fillStyle = \"#ffffff\";\n      for (let r = 0; r < rows; r++) {\n        for (let c = 0; c < cols; c++) {\n          if (grid[r * cols + c] !== levels) continue;\n          ctx.globalAlpha = 0.35;\n          ctx.fillRect(c * size + off, r * size + off, inner, inner);\n        }\n      }\n      ctx.globalAlpha = 1;\n    };\n\n    const frame = (now: number) => {\n      const dt = Math.min(0.05, Math.max(0.001, (now - last) / 1000));\n      last = now;\n      step(dt);\n      draw();\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      cols = Math.ceil(w / size);\n      rows = Math.ceil(h / size);\n      grid = new Uint8Array(cols * rows);\n      if (!placed) {\n        placed = true;\n        px = w / 2;\n        py = h / 2;\n        lx = idle ? w * 0.5 : px;\n        ly = idle ? h * (0.5 + 0.32 * Math.sin(0.9)) : py;\n      }\n      if (reduced) {\n        if (idle) {\n          for (let i = 0; i < 24; i++) step(1 / 60);\n        }\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      if (!active && !idle && mix < 0.02) {\n        lx = px;\n        ly = py;\n      }\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, cell, gap, steps, stepMs, radius, showGrid, idle]);\n\n  return (\n    <div ref={rootRef} className={`hb-qi7 ${className}`} style={style} {...rest}>\n      <style href=\"hb-cursor-pixel-trail\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-qi7-canvas\" aria-hidden=\"true\" />\n      <div className=\"hb-qi7-content\">{children}</div>\n    </div>\n  );\n}\n"
    }
  ]
}