{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "spark-trail",
  "type": "registry:component",
  "title": "Spark Trail",
  "description": "The cursor sheds sparks with real physics: they inherit its momentum, fall under gravity, bounce on the floor and cool from white to ember.",
  "author": "Marcelo Treviño Velázquez <https://furin.dev>",
  "dependencies": [],
  "categories": [
    "Cursors"
  ],
  "docs": "Installed in components/furin/spark-trail/. Docs, live controls and props table: https://furin.dev/components/spark-trail/",
  "files": [
    {
      "path": "registry/spark-trail/SparkTrail.tsx",
      "content": "\"use client\";\nimport { useEffect, useRef, useState } from \"react\";\nimport type { CSSProperties, ReactNode } from \"react\";\nimport \"./SparkTrail.css\";\n\nexport interface SparkTrailProps {\n  /** Color de las chispas: nacen casi blancas y se apagan en brasa. */\n  color?: string;\n  /** Cuántas chispas suelta cada trazo (1 es discreto, 3 es lluvia). */\n  density?: number;\n  /** Grosor de las chispas, en px. */\n  thickness?: number;\n  /** Gravedad que las hace caer, en px/s². */\n  gravity?: number;\n  /** Segundos que dura cada chispa. */\n  life?: number;\n  /** Sigue al cursor en toda la ventana en vez de solo en el contenedor. */\n  global?: boolean;\n  /** Se pasea solo cuando nadie mueve el cursor. */\n  autoplay?: boolean;\n  /** Texto de pista de la escena de muestra (solo se ve si omites children). */\n  hint?: string;\n  /** Contenido que queda debajo de las chispas. */\n  children?: ReactNode;\n  /** Clase extra para el contenedor. */\n  className?: string;\n  /** Estilos extra para el contenedor. */\n  style?: CSSProperties;\n}\n\nlet colorCtx: CanvasRenderingContext2D | null = null;\n/** Convierte cualquier color CSS a [r, g, b] pintándolo en un lienzo de 1 px. */\nfunction toRgb(color: string): [number, number, number] {\n  if (typeof document === \"undefined\") return [255, 179, 71];\n  colorCtx ??= document.createElement(\"canvas\").getContext(\"2d\", { willReadFrequently: true })!;\n  colorCtx.clearRect(0, 0, 1, 1);\n  colorCtx.fillStyle = \"#000\";\n  colorCtx.fillStyle = color;\n  colorCtx.fillRect(0, 0, 1, 1);\n  const d = colorCtx.getImageData(0, 0, 1, 1).data;\n  return [d[0], d[1], d[2]];\n}\n\ninterface Spark {\n  x: number; y: number; vx: number; vy: number;\n  age: number; life: number; w: number;\n  /** 0 = chispa con estela, 1 = brasa que titila. */\n  kind: 0 | 1;\n  seed: number;\n}\n\nconst MAX = 420;\n\n/** Estela de chispas: cada trazo del cursor suelta chispas con física (inercia, gravedad, rebote) y color de brasa. */\nexport function SparkTrail({\n  color = \"#FFA94D\",\n  density = 2,\n  thickness = 2.1,\n  gravity = 760,\n  life = 0.9,\n  global = false,\n  autoplay = false,\n  hint = \"Move your cursor\",\n  children,\n  className = \"\",\n  style,\n}: SparkTrailProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const [activo, setActivo] = useState(true);\n  const live = useRef({ color, density, thickness, gravity, life, autoplay });\n  live.current = { color, density, thickness, gravity, life, autoplay };\n\n  useEffect(() => {\n    const el = host.current;\n    const cv = canvas.current;\n    if (!el || !cv) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    if (reduce) { setActivo(false); return; }\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n\n    let W = 1, H = 1, dpr = 1, raf = 0, last = 0, visible = true;\n    let lx = 0, ly = 0, lt = 0, has = false, inside = false, lastReal = -1e9;\n    const sparks: Spark[] = [];\n    let glow = 0;\n    let col = toRgb(live.current.color);\n    let colKey = live.current.color;\n\n    const resize = () => {\n      dpr = Math.min(window.devicePixelRatio || 1, 2);\n      const r = global ? { width: window.innerWidth, height: window.innerHeight } : el.getBoundingClientRect();\n      W = Math.max(1, r.width);\n      H = Math.max(1, r.height);\n      cv.width = Math.round(W * dpr);\n      cv.height = Math.round(H * dpr);\n    };\n\n    /** Mueve el \"lápiz\" y suelta chispas a lo largo del segmento recorrido. */\n    const move = (x: number, y: number, tMs: number) => {\n      if (!has) { lx = x; ly = y; lt = tMs; has = true; return; }\n      const dx = x - lx, dy = y - ly;\n      const dist = Math.hypot(dx, dy);\n      if (dist < 1.5) return;\n      const dt = Math.max(0.008, (tMs - lt) / 1000);\n      const sp = Math.min(dist / dt, 3200);\n      const n = Math.min(14, Math.max(1, Math.round((dist / 9) * live.current.density)));\n      for (let i = 0; i < n; i++) {\n        if (sparks.length >= MAX) sparks.shift();\n        const f = (i + Math.random()) / n;\n        const ang = Math.random() * Math.PI * 2;\n        const burst = 30 + Math.random() * (60 + sp * 0.05);\n        // La chispa hereda un poco del movimiento del cursor y sale en cualquier dirección.\n        const vx = (dx / dist) * sp * 0.1 * (0.3 + Math.random()) + Math.cos(ang) * burst;\n        const vy = (dy / dist) * sp * 0.1 * (0.3 + Math.random()) + Math.sin(ang) * burst - 45;\n        const ember = Math.random() < 0.28;\n        sparks.push({\n          x: lx + dx * f, y: ly + dy * f, vx, vy, age: 0,\n          life: live.current.life * (ember ? 1.15 : 0.55 + Math.random() * 0.6),\n          w: live.current.thickness * (ember ? 0.75 : 0.6 + Math.random() * 0.7),\n          kind: ember ? 1 : 0,\n          seed: Math.random() * 6.28,\n        });\n      }\n      lx = x; ly = y; lt = tMs;\n      glow = Math.min(1, glow + n * 0.08);\n    };\n\n    const draw = () => {\n      if (colKey !== live.current.color) { colKey = live.current.color; col = toRgb(colKey); }\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      ctx.clearRect(0, 0, W, H);\n      ctx.globalCompositeOperation = \"lighter\";\n      ctx.lineCap = \"round\";\n      const hot: [number, number, number] = [col[0] + (255 - col[0]) * 0.78, col[1] + (255 - col[1]) * 0.78, col[2] + (255 - col[2]) * 0.78];\n      const dim: [number, number, number] = [col[0] * 0.55, col[1] * 0.22, col[2] * 0.1];\n      if (glow > 0.02 && has) {\n        const g = ctx.createRadialGradient(lx, ly, 0, lx, ly, 54);\n        g.addColorStop(0, `rgba(${hot[0] | 0},${hot[1] | 0},${hot[2] | 0},${glow * 0.42})`);\n        g.addColorStop(0.35, `rgba(${col[0]},${col[1]},${col[2]},${glow * 0.14})`);\n        g.addColorStop(1, `rgba(${col[0]},${col[1]},${col[2]},0)`);\n        ctx.fillStyle = g;\n        ctx.fillRect(lx - 54, ly - 54, 108, 108);\n      }\n      for (const s of sparks) {\n        const k = s.age / s.life;\n        const a = Math.pow(1 - k, 0.9);\n        let c: [number, number, number];\n        if (k < 0.22) { const t = k / 0.22; c = [hot[0] + (col[0] - hot[0]) * t, hot[1] + (col[1] - hot[1]) * t, hot[2] + (col[2] - hot[2]) * t]; }\n        else { const t = (k - 0.22) / 0.78; c = [col[0] + (dim[0] - col[0]) * t, col[1] + (dim[1] - col[1]) * t, col[2] + (dim[2] - col[2]) * t]; }\n        const rgb = `${c[0] | 0},${c[1] | 0},${c[2] | 0}`;\n        if (s.kind === 0) {\n          const sp = Math.hypot(s.vx, s.vy) || 1;\n          const len = Math.min(sp * 0.03, 15);\n          const ux = s.vx / sp, uy = s.vy / sp;\n          const w = s.w * (1 - k * 0.55);\n          const gr = w * 4.5;\n          const gg = ctx.createRadialGradient(s.x, s.y, 0, s.x, s.y, gr);\n          gg.addColorStop(0, `rgba(${rgb},${a * 0.38})`);\n          gg.addColorStop(1, `rgba(${rgb},0)`);\n          ctx.fillStyle = gg;\n          ctx.fillRect(s.x - gr, s.y - gr, gr * 2, gr * 2);\n          ctx.beginPath();\n          ctx.moveTo(s.x - ux * len, s.y - uy * len);\n          ctx.lineTo(s.x, s.y);\n          ctx.strokeStyle = `rgba(${rgb},${a})`;\n          ctx.lineWidth = w;\n          ctx.stroke();\n        } else {\n          const tw = 0.6 + 0.4 * Math.sin(s.age * 26 + s.seed);\n          const r = s.w * 0.85 * (1 - k * 0.4);\n          const g = ctx.createRadialGradient(s.x, s.y, 0, s.x, s.y, r * 5);\n          g.addColorStop(0, `rgba(${rgb},${a * tw * 0.55})`);\n          g.addColorStop(1, `rgba(${rgb},0)`);\n          ctx.fillStyle = g;\n          ctx.fillRect(s.x - r * 5, s.y - r * 5, r * 10, r * 10);\n          ctx.fillStyle = `rgba(${rgb},${a * tw})`;\n          ctx.beginPath();\n          ctx.arc(s.x, s.y, r, 0, 6.2832);\n          ctx.fill();\n        }\n      }\n      ctx.globalCompositeOperation = \"source-over\";\n    };\n\n    const step = (dt: number) => {\n      glow *= Math.exp(-5 * dt);\n      const g = live.current.gravity;\n      const drag = Math.exp(-1.7 * dt);\n      for (let i = sparks.length - 1; i >= 0; i--) {\n        const s = sparks[i];\n        s.age += dt;\n        if (s.age >= s.life) { sparks[i] = sparks[sparks.length - 1]; sparks.pop(); continue; }\n        s.vx *= drag;\n        s.vy = s.vy * drag + g * (s.kind ? 0.25 : 1) * dt;\n        s.x += s.vx * dt;\n        s.y += s.vy * dt;\n        // Rebote en el piso del contenedor: pierde energía y casi no rueda.\n        if (!global && s.y > H - 1 && s.vy > 0) { s.y = H - 1; s.vy *= -0.32; s.vx *= 0.72; }\n      }\n    };\n\n    const frame = (nowMs: number) => {\n      raf = 0;\n      const dt = Math.min(0.05, Math.max(0.001, (nowMs - last) / 1000));\n      last = nowMs;\n      const auto = live.current.autoplay && (!inside || nowMs - lastReal > 2500);\n      if (auto) {\n        // Paseo de Lissajous para que la demo se entienda sola.\n        const t = nowMs / 1000;\n        const sc = global ? { w: W, h: H } : { w: W, h: H };\n        move(sc.w * (0.5 + 0.34 * Math.sin(t * 1.05)), sc.h * (0.46 + 0.26 * Math.sin(t * 1.7 + 0.6)), nowMs);\n      }\n      step(dt);\n      draw();\n      if ((sparks.length || auto || glow > 0.02) && visible) raf = requestAnimationFrame(frame);\n      else if (!sparks.length) { ctx.setTransform(dpr, 0, 0, dpr, 0, 0); ctx.clearRect(0, 0, W, H); }\n    };\n    const wake = () => {\n      if (raf || !visible) return;\n      last = performance.now();\n      raf = requestAnimationFrame(frame);\n    };\n\n    const target: EventTarget = global ? window : el;\n    const onMove = (e: Event) => {\n      const pe = e as PointerEvent;\n      if (pe.pointerType === \"touch\") return;\n      const r = global ? { left: 0, top: 0 } : el.getBoundingClientRect();\n      inside = true;\n      lastReal = performance.now();\n      move(pe.clientX - r.left, pe.clientY - r.top, performance.now());\n      wake();\n    };\n    const onLeave = () => { inside = false; has = false; wake(); };\n    target.addEventListener(\"pointermove\", onMove);\n    el.addEventListener(\"pointerleave\", onLeave);\n\n    const io = new IntersectionObserver(([en]) => {\n      visible = en.isIntersecting;\n      if (visible) wake();\n      else if (raf) { cancelAnimationFrame(raf); raf = 0; }\n    });\n    io.observe(el);\n    const ro = new ResizeObserver(resize);\n    ro.observe(el);\n    if (global) window.addEventListener(\"resize\", resize);\n    resize();\n    wake();\n    return () => {\n      cancelAnimationFrame(raf);\n      target.removeEventListener(\"pointermove\", onMove);\n      el.removeEventListener(\"pointerleave\", onLeave);\n      if (global) window.removeEventListener(\"resize\", resize);\n      io.disconnect();\n      ro.disconnect();\n    };\n  }, [global]);\n\n  return (\n    <div ref={host} className={`furin-spark-trail ${className}`} style={style}>\n      <div className=\"furin-spark-trail__contenido\">\n        {children ?? <span className=\"furin-spark-trail__pista\">{hint}</span>}\n      </div>\n      {activo ? <canvas ref={canvas} aria-hidden=\"true\" className={`furin-spark-trail__canvas${global ? \" furin-spark-trail__canvas--global\" : \"\"}`} /> : null}\n    </div>\n  );\n}\n\nexport default SparkTrail;\n",
      "type": "registry:component",
      "target": "components/furin/spark-trail/SparkTrail.tsx"
    },
    {
      "path": "registry/spark-trail/SparkTrail.css",
      "content": ".furin-spark-trail {\n  position: relative;\n  width: 100%;\n  height: 100%;\n  min-height: 200px;\n  overflow: hidden;\n  touch-action: pan-y;\n  font-family: Geist, system-ui, -apple-system, sans-serif;\n  color: rgba(255, 255, 255, 0.5);\n}\n.furin-spark-trail__contenido { position: relative; width: 100%; height: 100%; display: grid; place-items: center; }\n.furin-spark-trail__pista {\n  font: 500 12px/1 \"JetBrains Mono\", ui-monospace, \"SF Mono\", Menlo, monospace;\n  letter-spacing: 0.14em;\n  text-transform: uppercase;\n  color: rgba(255, 255, 255, 0.34);\n  user-select: none;\n}\n.furin-spark-trail__canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; pointer-events: none; z-index: 2; }\n.furin-spark-trail__canvas--global { position: fixed; z-index: 2147483000; }\n",
      "type": "registry:file",
      "target": "components/furin/spark-trail/SparkTrail.css"
    }
  ]
}
