{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "warp-stars",
  "type": "registry:component",
  "title": "Warp Stars",
  "description": "Stars at warp speed with trails and depth; the cursor steers the vanishing point and holding down triggers the jump.",
  "author": "Marcelo Treviño Velázquez <https://furin.dev>",
  "dependencies": [],
  "categories": [
    "Backgrounds"
  ],
  "docs": "Installed in components/furin/warp-stars/. Docs, live controls and props table: https://furin.dev/components/warp-stars/",
  "files": [
    {
      "path": "registry/warp-stars/WarpStars.tsx",
      "content": "\"use client\";\nimport { useEffect, useRef } from \"react\";\nimport type { CSSProperties, ReactNode } from \"react\";\nimport \"./WarpStars.css\";\n\nexport interface WarpStarsProps {\n  /** Contenido que va encima de las estrellas. */\n  children?: ReactNode;\n  /** Velocidad del salto (1 = crucero). */\n  speed?: number;\n  /** Cantidad de estrellas (1 = normal). */\n  density?: number;\n  /** Largo de las estelas. */\n  trail?: number;\n  /** Qué tanto el cursor dirige el punto de fuga. */\n  steer?: number;\n  /** Color de las estrellas (se mezcla con un tinte cálido y uno frío). */\n  color?: string;\n  /** Color del fondo del espacio. */\n  background?: string;\n  /** Clase extra para el contenedor. */\n  className?: string;\n  /** Estilos extra para el contenedor. */\n  style?: CSSProperties;\n}\n\ninterface Estrella { x: number; y: number; z: number; t: number; }\n\nlet colorCtx: CanvasRenderingContext2D | null = null;\nconst RGB_CACHE = new Map<string, [number, number, number]>();\nfunction rgb(css: string, def: [number, number, number]): [number, number, number] {\n  const hit = RGB_CACHE.get(css);\n  if (hit) return hit;\n  try {\n    colorCtx ??= document.createElement(\"canvas\").getContext(\"2d\", { willReadFrequently: true })!;\n    colorCtx.clearRect(0, 0, 1, 1);\n    colorCtx.fillStyle = \"#010203\";\n    colorCtx.fillStyle = css;\n    colorCtx.fillRect(0, 0, 1, 1);\n    const d = colorCtx.getImageData(0, 0, 1, 1).data;\n    const out: [number, number, number] = [d[0], d[1], d[2]];\n    if (RGB_CACHE.size > 64) RGB_CACHE.clear();\n    RGB_CACHE.set(css, out);\n    return out;\n  } catch {\n    return def;\n  }\n}\n\nconst NIVELES = 8;\n\n/** Fondo de salto a warp: estrellas con estelas que salen del punto de fuga, y el cursor gira el rumbo. */\nexport function WarpStars({\n  children,\n  speed = 1,\n  density = 1,\n  trail = 1,\n  steer = 1,\n  color = \"#d6e4ff\",\n  background = \"#05060a\",\n  className = \"\",\n  style,\n}: WarpStarsProps) {\n  const rootRef = useRef<HTMLDivElement>(null);\n  const props = useRef({ speed, density, trail, steer, color });\n  props.current = { speed, density, trail, steer, color };\n  const wakeRef = useRef<(() => void) | null>(null);\n\n  useEffect(() => {\n    const root = rootRef.current;\n    if (!root) return;\n    const canvas = document.createElement(\"canvas\");\n    canvas.className = \"furin-warp-stars__lienzo\";\n    canvas.setAttribute(\"aria-hidden\", \"true\");\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n    root.insertBefore(canvas, root.firstChild);\n\n    const reducido = matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    let w = 0, h = 0, dpr = 1, raf = 0, visible = true, muerto = false, ult = 0;\n    let estrellas: Estrella[] = [];\n    // Punto de fuga: sigue al cursor con inercia; sin cursor se pasea despacio.\n    const vp = { x: 0, y: 0, tx: 0, ty: 0, hay: false };\n    let impulso = 0, impulsoObj = 0; // mantener presionado = salto hiperespacial\n    let tiempo = 0;\n\n    let tintes: string[][] = [];\n    const armarTintes = () => {\n      const [r, g, b] = rgb(props.current.color, [214, 228, 255]);\n      const mezcla = (m: [number, number, number], k: number): [number, number, number] => [r + (m[0] - r) * k, g + (m[1] - g) * k, b + (m[2] - b) * k];\n      const base: [number, number, number][] = [[r, g, b], mezcla([255, 214, 170], 0.55), mezcla([140, 175, 255], 0.55)];\n      tintes = base.map(([R, G, B]) => Array.from({ length: NIVELES }, (_, i) => `rgba(${R | 0},${G | 0},${B | 0},${((i + 1) / NIVELES).toFixed(3)})`));\n    };\n    armarTintes();\n\n    const nueva = (e: Estrella, alAzar: boolean) => {\n      e.x = (Math.random() * 2 - 1) * 1.15;\n      e.y = (Math.random() * 2 - 1) * 1.15;\n      // Con alAzar la estrella nace en cualquier profundidad (pantalla llena desde el primer cuadro).\n      e.z = alAzar ? 0.04 + Math.random() * 0.96 : 0.82 + Math.random() * 0.18;\n      const q = Math.random();\n      e.t = q < 0.62 ? 0 : q < 0.82 ? 1 : 2;\n    };\n    const ajustarCantidad = () => {\n      const n = Math.max(120, Math.min(2800, Math.round(props.current.density * 0.0055 * w * h)));\n      while (estrellas.length < n) { const e = { x: 0, y: 0, z: 1, t: 0 }; nueva(e, true); estrellas.push(e); }\n      if (estrellas.length > n) estrellas.length = n;\n    };\n\n    const pintar = (dt: number) => {\n      const p = props.current;\n      const f = Math.max(w, h) * 0.5;\n      // Cámara: con cursor se orienta hacia él; sin cursor traza una elipse lenta.\n      const cx0 = w / 2, cy0 = h / 2;\n      let tx = vp.tx, ty = vp.ty;\n      if (!vp.hay) { tx = cx0 + Math.sin(tiempo * 0.23) * w * 0.07; ty = cy0 + Math.sin(tiempo * 0.17 + 1) * h * 0.06; }\n      else { tx = cx0 + (vp.tx - cx0) * 0.75 * p.steer; ty = cy0 + (vp.ty - cy0) * 0.75 * p.steer; }\n      const k = 1 - Math.exp(-dt * 3.2);\n      vp.x += (tx - vp.x) * k; vp.y += (ty - vp.y) * k;\n      impulso += (impulsoObj - impulso) * (1 - Math.exp(-dt * (impulsoObj > impulso ? 4 : 2.2)));\n\n      const vel = p.speed * (0.5 + impulso * 1.6);\n      // La estela es la distancia que recorre la estrella en una fracción de segundo: más rápido, más larga.\n      const tau = (0.2 + 0.22 * impulso) * p.trail;\n\n      ctx.setTransform(1, 0, 0, 1, 0, 0);\n      ctx.clearRect(0, 0, canvas.width, canvas.height);\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      ctx.lineCap = \"round\";\n\n      // Resplandor en el punto de fuga, como la luz al final del túnel.\n      const gr = ctx.createRadialGradient(vp.x, vp.y, 0, vp.x, vp.y, Math.max(w, h) * 0.5);\n      gr.addColorStop(0, `rgba(150,180,255,${0.09 + 0.2 * impulso})`);\n      gr.addColorStop(0.35, `rgba(110,140,230,${0.03 + 0.07 * impulso})`);\n      gr.addColorStop(1, \"rgba(0,0,0,0)\");\n      ctx.fillStyle = gr;\n      ctx.fillRect(0, 0, w, h);\n\n      ctx.globalCompositeOperation = \"lighter\";\n      for (const e of estrellas) {\n        const dz = vel * (0.22 + 0.55 * (1 - e.z));\n        e.z -= dz * dt;\n        const sx = vp.x + (e.x * f) / e.z, sy = vp.y + (e.y * f) / e.z;\n        if (e.z < 0.02 || sx < -60 || sx > w + 60 || sy < -60 || sy > h + 60) { nueva(e, false); continue; }\n        const zt = e.z + dz * tau;\n        const px = vp.x + (e.x * f) / zt, py = vp.y + (e.y * f) / zt;\n        const cerca = 1 - e.z;\n        const alfa = Math.min(1, cerca * 3.2) * (0.5 + 0.5 * cerca);\n        const nivel = Math.max(0, Math.min(NIVELES - 1, Math.round(alfa * NIVELES) - 1));\n        const tinte = tintes[e.t][nivel];\n        const lw = 0.7 + 2.6 * cerca * cerca;\n        // Las más cercanas se ven desenfocadas y con aberración cromática: profundidad de campo.\n        if (e.z < 0.2) {\n          const dx = sx - vp.x, dy = sy - vp.y, d = Math.hypot(dx, dy) || 1;\n          const ab = (0.2 - e.z) * 9;\n          ctx.lineWidth = lw * 3.2;\n          ctx.strokeStyle = `rgba(255,255,255,${(0.05 * (0.2 - e.z) * 5).toFixed(3)})`;\n          ctx.beginPath(); ctx.moveTo(px, py); ctx.lineTo(sx, sy); ctx.stroke();\n          ctx.lineWidth = lw * 0.8;\n          ctx.strokeStyle = `rgba(255,90,60,${(0.22 * alfa).toFixed(3)})`;\n          ctx.beginPath(); ctx.moveTo(px + (dx / d) * ab, py + (dy / d) * ab); ctx.lineTo(sx + (dx / d) * ab, sy + (dy / d) * ab); ctx.stroke();\n          ctx.strokeStyle = `rgba(70,130,255,${(0.28 * alfa).toFixed(3)})`;\n          ctx.beginPath(); ctx.moveTo(px - (dx / d) * ab, py - (dy / d) * ab); ctx.lineTo(sx - (dx / d) * ab, sy - (dy / d) * ab); ctx.stroke();\n        }\n        // Estela: un trazo tenue entero y otro vivo en la cabeza, para que se afine hacia atrás.\n        ctx.lineWidth = lw;\n        ctx.strokeStyle = tintes[e.t][Math.max(0, nivel - 3)];\n        ctx.beginPath(); ctx.moveTo(px, py); ctx.lineTo(sx, sy); ctx.stroke();\n        ctx.strokeStyle = tinte;\n        ctx.beginPath(); ctx.moveTo(px + (sx - px) * 0.55, py + (sy - py) * 0.55); ctx.lineTo(sx, sy); ctx.stroke();\n      }\n      ctx.globalCompositeOperation = \"source-over\";\n    };\n\n    const paso = (ts: number) => {\n      raf = 0;\n      if (muerto || !visible) return;\n      const dt = Math.min(0.05, ult ? (ts - ult) / 1000 : 0.016);\n      ult = ts;\n      tiempo += dt;\n      pintar(dt);\n      raf = requestAnimationFrame(paso);\n    };\n    const wake = () => { if (!raf && visible && !muerto) { ult = 0; raf = requestAnimationFrame(paso); } };\n    wakeRef.current = () => { armarTintes(); ajustarCantidad(); if (reducido) pintar(0.016); else wake(); };\n\n    const medir = () => {\n      dpr = Math.min(window.devicePixelRatio || 1, 2);\n      const r = root.getBoundingClientRect();\n      w = Math.max(1, r.width); h = Math.max(1, r.height);\n      canvas.width = Math.round(w * dpr); canvas.height = Math.round(h * dpr);\n      if (!vp.x) { vp.x = w / 2; vp.y = h / 2; }\n      ajustarCantidad();\n      if (reducido) { for (const e of estrellas) nueva(e, true); pintar(0.016); } else wake();\n    };\n    const ro = new ResizeObserver(medir);\n    ro.observe(root);\n    const io = new IntersectionObserver(([e]) => { visible = e.isIntersecting; if (visible) wake(); }, { rootMargin: \"80px\" });\n    io.observe(root);\n\n    const onMove = (e: PointerEvent) => {\n      const r = root.getBoundingClientRect();\n      vp.tx = e.clientX - r.left; vp.ty = e.clientY - r.top; vp.hay = true;\n    };\n    const onLeave = () => { vp.hay = false; impulsoObj = 0; };\n    const onDown = (e: PointerEvent) => { onMove(e); impulsoObj = 1; };\n    const onUp = () => { impulsoObj = 0; };\n    if (!reducido) {\n      root.addEventListener(\"pointermove\", onMove, { passive: true });\n      root.addEventListener(\"pointerdown\", onDown, { passive: true });\n      root.addEventListener(\"pointerup\", onUp);\n      root.addEventListener(\"pointercancel\", onUp);\n      root.addEventListener(\"pointerleave\", onLeave);\n    }\n    medir();\n\n    return () => {\n      muerto = true;\n      wakeRef.current = null;\n      cancelAnimationFrame(raf);\n      ro.disconnect(); io.disconnect();\n      root.removeEventListener(\"pointermove\", onMove);\n      root.removeEventListener(\"pointerdown\", onDown);\n      root.removeEventListener(\"pointerup\", onUp);\n      root.removeEventListener(\"pointercancel\", onUp);\n      root.removeEventListener(\"pointerleave\", onLeave);\n      canvas.remove();\n    };\n  }, []);\n\n  useEffect(() => { wakeRef.current?.(); }, [speed, density, trail, steer, color]);\n\n  const vars = { \"--furin-warp-stars-bg\": background } as CSSProperties;\n  return (\n    <div ref={rootRef} className={`furin-warp-stars ${className}`} style={{ ...vars, ...style }}>\n      <span className=\"furin-warp-stars__vineta\" aria-hidden=\"true\" />\n      {children != null ? <div className=\"furin-warp-stars__contenido\">{children}</div> : null}\n    </div>\n  );\n}\n\nexport default WarpStars;\n",
      "type": "registry:component",
      "target": "components/furin/warp-stars/WarpStars.tsx"
    },
    {
      "path": "registry/warp-stars/WarpStars.css",
      "content": ".furin-warp-stars {\n  position: absolute;\n  inset: 0;\n  display: grid;\n  place-items: center;\n  overflow: hidden;\n  isolation: isolate;\n  background: var(--furin-warp-stars-bg, #05060a);\n  touch-action: pan-y;\n}\n.furin-warp-stars__lienzo { position: absolute; inset: 0; width: 100%; height: 100%; display: block; z-index: 0; }\n/* Viñeta de lente: oscurece las esquinas y deja respirar el centro, donde suele ir el titular. */\n.furin-warp-stars__vineta {\n  position: absolute;\n  inset: 0;\n  z-index: 1;\n  pointer-events: none;\n  background: radial-gradient(ellipse 85% 80% at 50% 50%, transparent 38%, rgba(0, 0, 0, 0.62) 100%);\n}\n.furin-warp-stars__contenido { position: relative; z-index: 2; max-width: 100%; pointer-events: none; }\n.furin-warp-stars__contenido > * { pointer-events: auto; }\n",
      "type": "registry:file",
      "target": "components/furin/warp-stars/WarpStars.css"
    }
  ]
}
