{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "cursor-trail-velocity",
  "type": "registry:component",
  "title": "Cursor Trail Velocity",
  "description": "A canvas cursor trail that reads pointer speed: slow moves leave small round dots, fast flicks stretch into long thin streaks that shift in hue.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "hover",
    "cursor",
    "trail",
    "velocity",
    "canvas",
    "streak",
    "pointer"
  ],
  "docs": "https://hyperblur.dev/effects/cursor-trail-velocity",
  "files": [
    {
      "path": "registry/hyperblur/cursor-trail-velocity.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/cursor-trail-velocity.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface CursorTrailVelocityProps extends React.HTMLAttributes<HTMLDivElement> {\n  slowHue?: number;\n  fastHue?: number;\n  size?: number;\n  life?: number;\n  maxSpeed?: number;\n  idle?: boolean;\n}\n\nconst css = `\n.hb-zb4 {\n  position: relative;\n  width: 100%;\n  height: 100%;\n  min-height: 120px;\n  overflow: hidden;\n  box-sizing: border-box;\n  font-family: inherit;\n  touch-action: pan-y;\n}\n.hb-zb4-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-zb4-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: none; }\n.hb-zb4-in > * { pointer-events: auto; }\n`;\n\ninterface Dot {\n  x: number;\n  y: number;\n  r: number;\n  hue: number;\n  s: number;\n  born: number;\n  life: number;\n  joined: boolean;\n}\n\nconst CAP = 720;\n\nfunction smooth(a: number, b: number, v: number) {\n  const k = Math.min(1, Math.max(0, (v - a) / (b - a)));\n  return k * k * (3 - 2 * k);\n}\n\nexport function CursorTrailVelocity({\n  slowHue = 200,\n  fastHue = 330,\n  size = 5,\n  life = 650,\n  maxSpeed = 1400,\n  idle = true,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: CursorTrailVelocityProps) {\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\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    const TAU = Math.PI * 2;\n    let dots: Dot[] = [];\n    let w = 0;\n    let h = 0;\n    let t = 0;\n    let phase = 0;\n    let raf = 0;\n    let prev = 0;\n    let visible = true;\n    let pointerOn = false;\n    let tx = 0;\n    let ty = 0;\n    let lx = 0;\n    let ly = 0;\n    let ex = 0;\n    let ey = 0;\n    let hasLast = false;\n    let speed = 0;\n    let headX = 0;\n    let headY = 0;\n    let headOn = false;\n\n    const idlePoint = () => {\n      const x = w * 0.5 + w * 0.38 * Math.sin(phase + 0.5);\n      const y = h * 0.5 + h * 0.32 * Math.sin(phase * 1.7);\n      return [x, y];\n    };\n\n    const step = (dt: number) => {\n      t += dt * 1000;\n      let x = 0;\n      let y = 0;\n      let live = false;\n      if (pointerOn) {\n        x = tx;\n        y = ty;\n        live = true;\n      } else if (idle) {\n        const wave = Math.sin(t * 0.00045 + 0.4);\n        phase += dt * (0.8 + 6 * wave * wave * wave * wave);\n        const p = idlePoint();\n        x = p[0];\n        y = p[1];\n        live = true;\n      }\n      headOn = live;\n      if (live) {\n        headX = x;\n        headY = y;\n        if (!hasLast) {\n          lx = x;\n          ly = y;\n          ex = x;\n          ey = y;\n          hasLast = true;\n          speed = 0;\n          dots.push({ x, y, r: size * 0.6, hue: slowHue, s: 0, born: t, life, joined: false });\n        } else {\n          const raw = Math.hypot(x - lx, y - ly) / Math.max(dt, 0.001);\n          speed += (raw - speed) * 0.22;\n          lx = x;\n          ly = y;\n          const sn = Math.min(1, speed / Math.max(1, maxSpeed));\n          const gap = 11 - 8 * sn;\n          let guard = 0;\n          let dx = x - ex;\n          let dy = y - ey;\n          let d = Math.hypot(dx, dy);\n          while (d >= gap && guard < 160) {\n            ex += (dx / d) * gap;\n            ey += (dy / d) * gap;\n            dots.push({\n              x: ex,\n              y: ey,\n              r: size * 0.6 * (1 - 0.7 * sn),\n              hue: slowHue + (fastHue - slowHue) * Math.pow(sn, 0.8),\n              s: sn,\n              born: t,\n              life: life * (0.5 + 1.5 * sn),\n              joined: sn > 0.22,\n            });\n            dx = x - ex;\n            dy = y - ey;\n            d = Math.hypot(dx, dy);\n            guard++;\n          }\n        }\n      } else {\n        hasLast = false;\n      }\n      if (dots.length > CAP) dots = dots.slice(dots.length - CAP);\n      dots = dots.filter((p) => t - p.born < p.life);\n    };\n\n    const draw = () => {\n      ctx.clearRect(0, 0, w, h);\n      ctx.globalCompositeOperation = \"lighter\";\n      ctx.lineCap = \"round\";\n      ctx.lineJoin = \"round\";\n      for (let i = 0; i < dots.length; i++) {\n        const p = dots[i];\n        const f = Math.max(0, 1 - (t - p.born) / p.life);\n        const e = Math.pow(f, 1.4);\n        if (p.joined && i > 0) {\n          const q = dots[i - 1];\n          if (Math.hypot(q.x - p.x, q.y - p.y) < 60) {\n            const a = smooth(0.22, 0.7, p.s) * e;\n            ctx.strokeStyle = `hsla(${p.hue.toFixed(1)}, 100%, 66%, ${a.toFixed(3)})`;\n            ctx.lineWidth = Math.max(0.7, size * 0.3 * (1.25 - p.s)) * (0.4 + 0.6 * f);\n            ctx.beginPath();\n            ctx.moveTo(q.x, q.y);\n            ctx.lineTo(p.x, p.y);\n            ctx.stroke();\n          }\n        }\n        const da = (1 - 0.55 * p.s) * e;\n        ctx.fillStyle = `hsla(${p.hue.toFixed(1)}, 100%, 64%, ${da.toFixed(3)})`;\n        ctx.beginPath();\n        ctx.arc(p.x, p.y, Math.max(0.4, p.r * (0.35 + 0.65 * f)), 0, TAU);\n        ctx.fill();\n      }\n      ctx.globalCompositeOperation = \"source-over\";\n      if (headOn) {\n        ctx.fillStyle = \"rgba(255,255,255,0.92)\";\n        ctx.beginPath();\n        ctx.arc(headX, headY, Math.max(1.4, size * 0.34), 0, TAU);\n        ctx.fill();\n      }\n    };\n\n    const still = () => {\n      dots = [];\n      t = 0;\n      phase = 0;\n      hasLast = false;\n      if (idle) {\n        for (let k = 0; k < 150; k++) step(1 / 60);\n      }\n      draw();\n    };\n\n    const frame = (now: number) => {\n      const dt = Math.min(0.05, Math.max(0.001, (now - prev) / 1000));\n      prev = now;\n      step(dt);\n      draw();\n      raf = requestAnimationFrame(frame);\n    };\n\n    const start = () => {\n      if (raf || motion.matches || !visible) return;\n      prev = performance.now();\n      raf = requestAnimationFrame(frame);\n    };\n\n    const stop = () => {\n      if (!raf) return;\n      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      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      if (motion.matches) still();\n    };\n\n    const move = (ev: Event) => {\n      const e = ev as PointerEvent;\n      if (motion.matches) return;\n      const r = root.getBoundingClientRect();\n      tx = e.clientX - r.left;\n      ty = e.clientY - r.top;\n      if (!pointerOn) {\n        pointerOn = true;\n        hasLast = false;\n      }\n    };\n\n    const leave = () => {\n      if (!pointerOn) return;\n      pointerOn = false;\n      hasLast = false;\n    };\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(root);\n    const io = new IntersectionObserver((entries) => {\n      visible = entries[entries.length - 1].isIntersecting;\n      if (visible) start();\n      else stop();\n    });\n    io.observe(root);\n    root.addEventListener(\"pointermove\", move);\n    root.addEventListener(\"pointerdown\", move);\n    root.addEventListener(\"pointerleave\", leave);\n    root.addEventListener(\"pointercancel\", leave);\n    const onMotion = () => {\n      if (motion.matches) {\n        stop();\n        still();\n      } else {\n        dots = [];\n        hasLast = false;\n        start();\n      }\n    };\n    motion.addEventListener(\"change\", onMotion);\n    resize();\n    if (!motion.matches) start();\n\n    return () => {\n      stop();\n      ro.disconnect();\n      io.disconnect();\n      root.removeEventListener(\"pointermove\", move);\n      root.removeEventListener(\"pointerdown\", move);\n      root.removeEventListener(\"pointerleave\", leave);\n      root.removeEventListener(\"pointercancel\", leave);\n      motion.removeEventListener(\"change\", onMotion);\n    };\n  }, [slowHue, fastHue, size, life, maxSpeed, idle]);\n\n  return (\n    <div ref={rootRef} className={`hb-zb4 ${className}`.trim()} style={style} {...rest}>\n      <style href=\"hb-cursor-trail-velocity\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-zb4-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-zb4-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}