{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "cursor-ribbons",
  "title": "Cursor Ribbons",
  "description": "Silky, tapered ribbons of colour that chase the pointer on springs of different stiffness, fanning apart when you move fast and gathering when you slow down, glowing where they cross and wandering on their own when idle.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/cursor-ribbons/cursor-ribbons.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport interface CursorRibbonsProps extends React.ComponentPropsWithoutRef<\"div\"> {\n  /** One colour per ribbon; the ribbon fades from this colour to the next one along its tail. */\n  colors?: string[];\n  background?: string;\n  /** Number of ribbons (1–8). */\n  count?: number;\n  /** Width of a ribbon at its head, in px. */\n  width?: number;\n  /** Number of points in each ribbon; more points make longer tails. */\n  length?: number;\n  /** Blend the ribbons additively so overlaps glow (best on dark backgrounds). */\n  glow?: boolean;\n  /** Let the ribbons wander on their own while no pointer is over them. */\n  idle?: boolean;\n}\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    (callback) => {\n      const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n      query.addEventListener(\"change\", callback);\n      return () => query.removeEventListener(\"change\", callback);\n    },\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\ntype Ribbon = { xs: Float32Array; ys: Float32Array; vx: number; vy: number; k: number; damp: number; follow: number; ox: number; oy: number; phase: number };\n\nexport function CursorRibbons({\n  colors = [\"#ff4d8d\", \"#ff9a3c\", \"#ffe14d\", \"#4dffb8\", \"#4db8ff\", \"#a64dff\"],\n  background = \"#07070a\",\n  count = 6,\n  width = 14,\n  length = 56,\n  glow = true,\n  idle = true,\n  className,\n  style,\n  children,\n  ...props\n}: CursorRibbonsProps) {\n  const reduce = useReducedMotion();\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n  const live = React.useRef({ colors, count, width, length, glow, idle });\n  const kick = React.useRef<() => void>(() => {});\n  React.useEffect(() => {\n    live.current = { colors, count, width, length, glow, idle };\n    kick.current();\n  }, [colors, count, width, length, glow, idle]);\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    let ribbons: Ribbon[] = [];\n    let w = 0;\n    let h = 0;\n    let raf = 0;\n    let visible = true;\n    let last = 0;\n    let time = 0;\n    // Where the ribbons head: the pointer, or a wandering point when idle. `pull` eases between the two, so they never jump.\n    const target = { x: 0, y: 0, px: 0, py: 0, hover: false, pull: 0 };\n\n    const build = () => {\n      const L = live.current;\n      const n = Math.max(1, Math.min(8, L.count));\n      const len = Math.max(8, Math.min(120, Math.round(L.length)));\n      if (ribbons.length === n && ribbons[0].xs.length === len) return;\n      ribbons = Array.from({ length: n }, (_, i) => {\n        const t = n === 1 ? 0.5 : i / (n - 1);\n        // Each ribbon has its own spring, so they fan apart when the pointer moves fast and gather when it slows.\n        const r: Ribbon = { xs: new Float32Array(len), ys: new Float32Array(len), vx: 0, vy: 0, k: 0.11 - t * 0.075, damp: 0.86 - t * 0.06, follow: 0.34 - t * 0.1, ox: 0, oy: 0, phase: (i / n) * Math.PI * 2 };\n        r.xs.fill(target.x);\n        r.ys.fill(target.y);\n        return r;\n      });\n    };\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      w = root.clientWidth;\n      h = root.clientHeight;\n      canvas.width = Math.round(w * dpr);\n      canvas.height = Math.round(h * dpr);\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      if (!ribbons.length) {\n        target.x = w / 2;\n        target.y = h / 2;\n      }\n    };\n    const wander = (t: number) => ({\n      x: w / 2 + Math.sin(t * 1.1) * w * 0.32 + Math.sin(t * 2.7) * w * 0.07,\n      y: h / 2 + Math.sin(t * 1.7 + 1) * h * 0.28 + Math.cos(t * 3.3) * h * 0.06,\n    });\n\n    const step = (dt: number) => {\n      const L = live.current;\n      build();\n      time += dt;\n      const s = dt * 60;\n      const wv = wander(time);\n      target.pull += ((target.hover || !L.idle ? 1 : 0) - target.pull) * (1 - Math.exp(-dt * 3));\n      const tx = target.px * target.pull + wv.x * (1 - target.pull);\n      const ty = target.py * target.pull + wv.y * (1 - target.pull);\n      target.x = tx;\n      target.y = ty;\n      const spread = L.width * 1.4;\n      for (const r of ribbons) {\n        // Each head aims at its own point on a small turning circle, so the ribbons braid around each other.\n        r.ox = Math.cos(time * 1.6 + r.phase) * spread;\n        r.oy = Math.sin(time * 1.6 + r.phase) * spread;\n        // Head: a damped spring toward the target (scaled by the frame time so it runs the same at any refresh rate).\n        r.vx = (r.vx + (tx + r.ox - r.xs[0]) * r.k * s) * Math.pow(r.damp, s);\n        r.vy = (r.vy + (ty + r.oy - r.ys[0]) * r.k * s) * Math.pow(r.damp, s);\n        r.xs[0] += r.vx * s;\n        r.ys[0] += r.vy * s;\n        // Tail: each point eases after the one in front of it.\n        const f = 1 - Math.pow(1 - r.follow, s);\n        for (let i = 1; i < r.xs.length; i++) {\n          r.xs[i] += (r.xs[i - 1] - r.xs[i]) * f;\n          r.ys[i] += (r.ys[i - 1] - r.ys[i]) * f;\n        }\n      }\n    };\n\n    const lx = new Float32Array(240);\n    const ly = new Float32Array(240);\n    const draw = () => {\n      const L = live.current;\n      ctx.clearRect(0, 0, w, h);\n      ctx.globalCompositeOperation = L.glow ? \"lighter\" : \"source-over\";\n      ribbons.forEach((r, ri) => {\n        const n = r.xs.length;\n        const c0 = L.colors[ri % L.colors.length];\n        const c1 = L.colors[(ri + 1) % L.colors.length];\n        // Offset each point sideways along the local normal; the width tapers to nothing at the tail\n        // and to a soft point at the head.\n        for (let i = 0; i < n; i++) {\n          const a = Math.max(0, i - 1);\n          const b = Math.min(n - 1, i + 1);\n          let nx = -(r.ys[b] - r.ys[a]);\n          let ny = r.xs[b] - r.xs[a];\n          const m = Math.hypot(nx, ny) || 1;\n          nx /= m;\n          ny /= m;\n          const t = i / (n - 1);\n          const half = (L.width / 2) * Math.pow(1 - t, 1.2) * Math.min(1, Math.sqrt(i / 4));\n          lx[i] = r.xs[i] + nx * half;\n          ly[i] = r.ys[i] + ny * half;\n          lx[n + (n - 1 - i)] = r.xs[i] - nx * half;\n          ly[n + (n - 1 - i)] = r.ys[i] - ny * half;\n        }\n        const grad = ctx.createLinearGradient(r.xs[0], r.ys[0], r.xs[n - 1], r.ys[n - 1]);\n        grad.addColorStop(0, c0);\n        grad.addColorStop(1, c1);\n        ctx.fillStyle = grad;\n        ctx.globalAlpha = L.glow ? 0.62 : 1;\n        // Smooth outline through the midpoints of the offset points.\n        ctx.beginPath();\n        ctx.moveTo(lx[0], ly[0]);\n        for (let i = 1; i < 2 * n - 1; i++) ctx.quadraticCurveTo(lx[i], ly[i], (lx[i] + lx[i + 1]) / 2, (ly[i] + ly[i + 1]) / 2);\n        ctx.lineTo(lx[2 * n - 1], ly[2 * n - 1]);\n        ctx.closePath();\n        ctx.fill();\n        // A thin bright core down the front half gives each ribbon a satin sheen.\n        ctx.globalAlpha = L.glow ? 0.3 : 0.3;\n        ctx.strokeStyle = \"#ffffff\";\n        ctx.lineWidth = Math.max(1, L.width * 0.12);\n        ctx.lineCap = \"round\";\n        ctx.beginPath();\n        ctx.moveTo(r.xs[0], r.ys[0]);\n        const half = Math.floor(n * 0.3);\n        for (let i = 1; i < half; i++) ctx.quadraticCurveTo(r.xs[i], r.ys[i], (r.xs[i] + r.xs[i + 1]) / 2, (r.ys[i] + r.ys[i + 1]) / 2);\n        ctx.stroke();\n      });\n      ctx.globalAlpha = 1;\n      ctx.globalCompositeOperation = \"source-over\";\n    };\n\n    const frame = (now: number) => {\n      const dt = Math.min(0.05, last ? (now - last) / 1000 : 1 / 60);\n      last = now;\n      step(dt);\n      draw();\n      if (visible && !document.hidden) raf = requestAnimationFrame(frame);\n      else last = 0;\n    };\n    const start = () => {\n      cancelAnimationFrame(raf);\n      if (reduce) {\n        // A still frame: let the ribbons settle along their wandering path, then draw once.\n        build();\n        for (let i = 0; i < 90; i++) step(1 / 60);\n        draw();\n        return;\n      }\n      raf = requestAnimationFrame(frame);\n    };\n    kick.current = start;\n\n    const local = (event: PointerEvent) => {\n      const r = root.getBoundingClientRect();\n      const s = r.width / root.offsetWidth || 1;\n      return { x: (event.clientX - r.left) / s, y: (event.clientY - r.top) / s };\n    };\n    const onMove = (event: PointerEvent) => {\n      const p = local(event);\n      target.px = p.x;\n      target.py = p.y;\n      target.hover = true;\n    };\n    const onLeave = (event: PointerEvent) => {\n      if (event.pointerType === \"touch\") return;\n      target.hover = false;\n    };\n    const ro = new ResizeObserver(() => {\n      resize();\n      if (reduce) start();\n    });\n    ro.observe(root);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible) start();\n    });\n    io.observe(root);\n    root.addEventListener(\"pointermove\", onMove);\n    root.addEventListener(\"pointerdown\", onMove);\n    root.addEventListener(\"pointerleave\", onLeave);\n    resize();\n    start();\n    return () => {\n      cancelAnimationFrame(raf);\n      kick.current = () => {};\n      ro.disconnect();\n      io.disconnect();\n      root.removeEventListener(\"pointermove\", onMove);\n      root.removeEventListener(\"pointerdown\", onMove);\n      root.removeEventListener(\"pointerleave\", onLeave);\n    };\n  }, [reduce]);\n\n  return (\n    <div ref={rootRef} className={cn(\"relative isolate overflow-hidden\", className)} style={{ background, ...style }} {...props}>\n      <canvas ref={canvasRef} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 -z-10 h-full w-full\" />\n      {children}\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}