{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "electric-border",
  "type": "registry:component",
  "title": "Electric Border",
  "description": "Arcs of electricity that snake around the border, with local discharges, sparks and a flickering glow.",
  "author": "Marcelo Treviño Velázquez <https://furin.dev>",
  "dependencies": [],
  "categories": [
    "Cards"
  ],
  "docs": "Installed in components/furin/electric-border/. Docs, live controls and props table: https://furin.dev/components/electric-border/",
  "files": [
    {
      "path": "registry/electric-border/ElectricBorder.tsx",
      "content": "\"use client\";\nimport { useEffect, useRef } from \"react\";\nimport type { CSSProperties, ReactNode } from \"react\";\nimport \"./ElectricBorder.css\";\n\nexport interface ElectricBorderProps {\n  /** Contenido de la tarjeta; si es texto se muestra como título. */\n  children?: ReactNode;\n  /** Texto de apoyo bajo el título (solo cuando children es texto). */\n  caption?: string;\n  /** Color de la electricidad. */\n  color?: string;\n  /** Cuánto se desvía el arco del borde (1 = normal). */\n  chaos?: number;\n  /** Rapidez del chisporroteo (1 = normal). */\n  speed?: number;\n  /** Cuántas chispas saltan por segundo. */\n  sparks?: number;\n  /** Intensidad del resplandor, de 0 a 1.5. */\n  glow?: number;\n  /** Grosor del arco, en px. */\n  borderWidth?: number;\n  /** Radio de las esquinas, en px. */\n  radius?: number;\n  /** Ancho máximo de la tarjeta, en px. */\n  width?: number;\n  /** Alto mínimo de la tarjeta, en px. */\n  minHeight?: number;\n  /** Fondo de la tarjeta. */\n  background?: string;\n  /** Clase extra para el contenedor. */\n  className?: string;\n  /** Estilos extra para el contenedor. */\n  style?: CSSProperties;\n}\n\nconst PAD = 40;\nconst TAU = Math.PI * 2;\n\nfunction parseColor(c: string): [number, number, number] {\n  const cv = document.createElement(\"canvas\");\n  cv.width = cv.height = 1;\n  const x = cv.getContext(\"2d\", { willReadFrequently: true });\n  if (!x) return [120, 200, 255];\n  x.fillStyle = \"#fff\";\n  x.fillStyle = c;\n  x.fillRect(0, 0, 1, 1);\n  const d = x.getImageData(0, 0, 1, 1).data;\n  return [d[0], d[1], d[2]];\n}\n\n/** Rectángulo redondeado en puntos equiespaciados, con su normal hacia afuera. */\nfunction buildPath(w: number, h: number, r: number, ds: number) {\n  const sw = Math.max(0, w - 2 * r), sh = Math.max(0, h - 2 * r), arc = (Math.PI / 2) * r;\n  const P = 2 * sw + 2 * sh + 4 * arc;\n  const n = Math.max(24, Math.round(P / ds));\n  const xs = new Float32Array(n), ys = new Float32Array(n), nx = new Float32Array(n), ny = new Float32Array(n);\n  for (let i = 0; i < n; i++) {\n    let d = (i / n) * P, x = 0, y = 0, a = 0, ax = 0, ay = 0;\n    if (d < sw) { x = r + d; y = 0; ay = -1; }\n    else if ((d -= sw) < arc) { a = -Math.PI / 2 + d / r; ax = Math.cos(a); ay = Math.sin(a); x = w - r + r * ax; y = r + r * ay; }\n    else if ((d -= arc) < sh) { x = w; y = r + d; ax = 1; }\n    else if ((d -= sh) < arc) { a = d / r; ax = Math.cos(a); ay = Math.sin(a); x = w - r + r * ax; y = h - r + r * ay; }\n    else if ((d -= arc) < sw) { x = w - r - d; y = h; ay = 1; }\n    else if ((d -= sw) < arc) { a = Math.PI / 2 + d / r; ax = Math.cos(a); ay = Math.sin(a); x = r + r * ax; y = h - r + r * ay; }\n    else if ((d -= arc) < sh) { x = 0; y = h - r - d; ax = -1; }\n    else { d -= sh; a = Math.PI + d / r; ax = Math.cos(a); ay = Math.sin(a); x = r + r * ax; y = r + r * ay; }\n    xs[i] = x; ys[i] = y; nx[i] = ax; ny[i] = ay;\n  }\n  return { xs, ys, nx, ny, n, P };\n}\n\ninterface Wave { k: number; a: number; ph: number; w: number }\ninterface Spark { x: number; y: number; vx: number; vy: number; life: number; max: number }\n\n/** Mulberry32: determinista, para que el servidor y el cliente pinten lo mismo. */\nfunction rng(seed: number) {\n  let a = seed >>> 0;\n  return () => { a = (a + 0x6d2b79f5) >>> 0; let t = a; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };\n}\n\n/** Borde de electricidad: arcos que serpentean alrededor del contenido, con descargas locales, chispas y parpadeo. */\nexport function ElectricBorder({\n  children = \"Electric Border\",\n  caption = \"A live arc that crackles around the edge.\",\n  color = \"#5cc8ff\",\n  chaos = 1,\n  speed = 1,\n  sparks = 26,\n  glow = 1,\n  borderWidth = 1.8,\n  radius = 22,\n  width = 360,\n  minHeight = 0,\n  background = \"#0c0e13\",\n  className = \"\",\n  style,\n}: ElectricBorderProps) {\n  const rootRef = useRef<HTMLDivElement>(null);\n  const cvRef = useRef<HTMLCanvasElement>(null);\n  const cfg = useRef({ color, chaos, speed, sparks, glow, borderWidth, radius });\n  cfg.current = { color, chaos, speed, sparks, glow, borderWidth, radius };\n  const refresh = useRef<() => void>(() => {});\n\n  useEffect(() => {\n    const root = rootRef.current, cv = cvRef.current;\n    if (!root || !cv) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    let W = 0, H = 0, dpr = 1, raf = 0, visible = true, last = 0, time = 0;\n    let path = buildPath(10, 10, 4, 3);\n    let rgb: number[] = parseColor(cfg.current.color);\n    const rnd = rng(7);\n    // Tres arcos independientes: cada uno es una suma de ondas periódicas a lo largo del perímetro.\n    const layers: Wave[][] = [0, 1, 2].map(() => [5, 9, 16, 27, 44, 71, 112].map((k) => ({\n      k: k + Math.round(rnd() * 2), a: Math.pow(k, -0.62) * (0.8 + rnd() * 0.5), ph: rnd() * TAU, w: (rnd() < 0.5 ? -1 : 1) * (1.5 + k * 0.16 + rnd() * 2),\n    })));\n    let surgeAt = 0, surgeW = 60, surgeNext = 0, surgeAmp = 0;\n    const sp: Spark[] = [];\n    let sparkAcc = 0;\n\n    const layout = () => {\n      const c = cfg.current;\n      const w = root.offsetWidth, h = root.offsetHeight;\n      dpr = Math.min(window.devicePixelRatio || 1, 2);\n      W = w + PAD * 2; H = h + PAD * 2;\n      cv.width = Math.round(W * dpr); cv.height = Math.round(H * dpr);\n      const r = Math.min(c.radius, w / 2, h / 2);\n      path = buildPath(w - c.borderWidth, h - c.borderWidth, Math.max(0, r - c.borderWidth / 2), 3);\n      rgb = parseColor(c.color);\n    };\n\n    const disp = new Float32Array(2048);\n    const draw = (dt: number, animate: boolean) => {\n      const c = cfg.current;\n      const { xs, ys, nx, ny, n, P } = path;\n      if (disp.length < n) return;\n      const off = PAD + c.borderWidth / 2;\n      const ds = P / n;\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      ctx.clearRect(0, 0, W, H);\n      ctx.globalCompositeOperation = \"lighter\";\n      ctx.lineJoin = \"round\";\n      ctx.lineCap = \"round\";\n\n      // Parpadeo global: mezcla de senos irregulares con picos ocasionales.\n      const fl = 0.82 + 0.1 * Math.sin(time * 23) * Math.sin(time * 7.3 + 1) + 0.08 * Math.sin(time * 41 + 2) + (Math.sin(time * 3.1) > 0.93 ? 0.22 : 0);\n      root.style.setProperty(\"--furin-electric-border-f\", fl.toFixed(3));\n\n      // Descarga local: de vez en cuando una zona del borde se dispara y salta más lejos.\n      if (animate && time > surgeNext) {\n        surgeAt = Math.floor(Math.random() * n);\n        surgeW = (30 + Math.random() * 60) / ds;\n        surgeAmp = 1.4 + Math.random() * 1.6;\n        surgeNext = time + 0.12 + Math.random() * 0.5;\n        // Cada descarga suelta un racimo de chispas.\n        const burst = Math.round(c.sparks * 0.18);\n        for (let q = 0; q < burst && sp.length < 90; q++) {\n          const j = (surgeAt + Math.round((Math.random() - 0.5) * surgeW)) % n;\n          const jj = (j + n) % n;\n          const a = Math.atan2(ny[jj], nx[jj]) + (Math.random() - 0.5) * 1.6;\n          const v = 50 + Math.random() * 190;\n          sp.push({ x: off + xs[jj], y: off + ys[jj], vx: Math.cos(a) * v, vy: Math.sin(a) * v, life: 0, max: 0.2 + Math.random() * 0.45 });\n        }\n      }\n      const decay = Math.max(0, 1 - (time - (surgeNext - 0.4)) * 2.2);\n\n      const amp = 7 * c.chaos;\n      const widths = [c.borderWidth * 1.15, c.borderWidth * 0.8, c.borderWidth * 0.6];\n      for (let l = 0; l < 3; l++) {\n        const waves = layers[l];\n        const lamp = amp * (l === 0 ? 0.8 : l === 1 ? 1.1 : 1.6);\n        for (let i = 0; i < n; i++) {\n          const u = (i / n) * TAU;\n          let d = 0;\n          for (let q = 0; q < waves.length; q++) {\n            const wv = waves[q];\n            d += wv.a * Math.sin(u * wv.k + wv.ph + time * c.speed * wv.w);\n          }\n          // Más picuda que una senoide: da el aspecto de relámpago.\n          d = Math.sign(d) * Math.pow(Math.abs(d), 1.5);\n          let m = 1;\n          if (surgeAmp > 0) {\n            let dd = Math.abs(i - surgeAt); if (dd > n / 2) dd = n - dd;\n            m += surgeAmp * decay * Math.exp(-(dd * dd) / (surgeW * surgeW));\n          }\n          disp[i] = d * lamp * m;\n        }\n        const build = () => {\n          ctx.beginPath();\n          for (let i = 0; i <= n; i++) {\n            const j = i % n;\n            const x = off + xs[j] + nx[j] * disp[j], y = off + ys[j] + ny[j] * disp[j];\n            if (i === 0) ctx.moveTo(x, y); else ctx.lineTo(x, y);\n          }\n          ctx.closePath();\n        };\n        // Resplandor\n        ctx.shadowColor = `rgba(${rgb},${0.9 * fl})`;\n        ctx.shadowBlur = 12 * c.glow;\n        ctx.strokeStyle = `rgba(${rgb},${0.55 * fl * (l === 0 ? 1 : 0.7)})`;\n        ctx.lineWidth = widths[l] * 1.9;\n        build(); ctx.stroke();\n        ctx.shadowBlur = 0;\n        // Arco\n        ctx.strokeStyle = `rgba(${rgb[0] * 0.55 + 115 | 0},${rgb[1] * 0.55 + 115 | 0},${rgb[2] * 0.55 + 115 | 0},${0.95 * fl})`;\n        ctx.lineWidth = widths[l];\n        build(); ctx.stroke();\n        // Núcleo blanco\n        if (l < 2) {\n          ctx.strokeStyle = `rgba(255,255,255,${0.85 * fl})`;\n          ctx.lineWidth = Math.max(0.6, widths[l] * 0.38);\n          build(); ctx.stroke();\n        }\n      }\n\n      // Chispas: nacen del borde y salen despedidas con un poco de gravedad.\n      if (animate) {\n        sparkAcc += dt * c.sparks;\n        while (sparkAcc >= 1 && sp.length < 90) {\n          sparkAcc -= 1;\n          const j = Math.floor(Math.random() * n);\n          const a = Math.atan2(ny[j], nx[j]) + (Math.random() - 0.5) * 1.2;\n          const v = 30 + Math.random() * 120;\n          sp.push({ x: off + xs[j], y: off + ys[j], vx: Math.cos(a) * v, vy: Math.sin(a) * v, life: 0, max: 0.15 + Math.random() * 0.4 });\n        }\n        sparkAcc = Math.min(sparkAcc, 2);\n        for (let i = sp.length - 1; i >= 0; i--) {\n          const s = sp[i];\n          s.life += dt;\n          if (s.life >= s.max) { sp.splice(i, 1); continue; }\n          s.vy += 160 * dt;\n          s.vx *= 1 - 2.2 * dt; s.vy *= 1 - 2.2 * dt;\n          const px = s.x, py = s.y;\n          s.x += s.vx * dt; s.y += s.vy * dt;\n          const f = 1 - s.life / s.max;\n          ctx.strokeStyle = `rgba(${rgb[0] * 0.4 + 150 | 0},${rgb[1] * 0.4 + 150 | 0},${rgb[2] * 0.4 + 150 | 0},${f})`;\n          ctx.lineWidth = 0.6 + f * 1.1;\n          ctx.shadowColor = `rgba(${rgb},${f})`;\n          ctx.shadowBlur = 6;\n          ctx.beginPath();\n          ctx.moveTo(px, py);\n          ctx.lineTo(s.x - s.vx * 0.012, s.y - s.vy * 0.012);\n          ctx.stroke();\n        }\n        ctx.shadowBlur = 0;\n      }\n    };\n\n    const frame = (now: number) => {\n      raf = 0;\n      const dt = Math.min(0.05, (now - last) / 1000 || 0.016);\n      last = now;\n      time += dt;\n      draw(dt, true);\n      if (visible) raf = requestAnimationFrame(frame);\n    };\n    const start = () => { if (!raf && !reduced) { last = performance.now(); raf = requestAnimationFrame(frame); } };\n\n    layout();\n    time = 1.3;\n    if (reduced) draw(0, false); else start();\n    refresh.current = () => { layout(); if (reduced) draw(0, false); };\n\n    const ro = new ResizeObserver(() => { layout(); if (reduced) draw(0, false); });\n    ro.observe(root);\n    const io = new IntersectionObserver((e) => {\n      visible = e[0]?.isIntersecting ?? true;\n      if (visible) start(); else { cancelAnimationFrame(raf); raf = 0; }\n    });\n    io.observe(root);\n    return () => { cancelAnimationFrame(raf); ro.disconnect(); io.disconnect(); };\n  }, []);\n\n  useEffect(() => { refresh.current(); }, [color, borderWidth, radius]);\n\n  const vars = {\n    \"--furin-electric-border-w\": `${width}px`,\n    \"--furin-electric-border-r\": `${radius}px`,\n    \"--furin-electric-border-bg\": background,\n    \"--furin-electric-border-min\": `${minHeight}px`,\n    \"--furin-electric-border-color\": color,\n    \"--furin-electric-border-glow\": glow,\n    \"--furin-electric-border-pad\": `${PAD}px`,\n  } as CSSProperties;\n\n  return (\n    <div ref={rootRef} className={`furin-electric-border ${className}`} style={{ ...vars, ...style }}>\n      <div className=\"furin-electric-border__content\">\n        {typeof children === \"string\" ? (\n          <>\n            <h3 className=\"furin-electric-border__title\">{children}</h3>\n            {caption ? <p className=\"furin-electric-border__caption\">{caption}</p> : null}\n          </>\n        ) : children}\n      </div>\n      <canvas ref={cvRef} className=\"furin-electric-border__canvas\" aria-hidden=\"true\" />\n    </div>\n  );\n}\n\nexport default ElectricBorder;\n",
      "type": "registry:component",
      "target": "components/furin/electric-border/ElectricBorder.tsx"
    },
    {
      "path": "registry/electric-border/ElectricBorder.css",
      "content": ".furin-electric-border {\n  --furin-electric-border-f: 0.85;\n  position: relative;\n  box-sizing: border-box;\n  width: min(var(--furin-electric-border-w), calc(100% - 64px));\n  min-height: var(--furin-electric-border-min);\n  display: grid;\n  border-radius: var(--furin-electric-border-r);\n  background: var(--furin-electric-border-bg);\n  color: #f2f4f8;\n  font-family: Geist, -apple-system, BlinkMacSystemFont, \"SF Pro Text\", system-ui, sans-serif;\n  /* El arco ilumina el aire y el interior; el parpadeo (--f) lo escribe el bucle en cada cuadro. */\n  box-shadow:\n    inset 0 0 calc(34px * var(--furin-electric-border-glow)) color-mix(in srgb, var(--furin-electric-border-color) calc(var(--furin-electric-border-f) * 26%), transparent),\n    0 0 calc(70px * var(--furin-electric-border-glow)) color-mix(in srgb, var(--furin-electric-border-color) calc(var(--furin-electric-border-f) * 14%), transparent);\n}\n.furin-electric-border__content {\n  position: relative;\n  display: flex;\n  flex-direction: column;\n  justify-content: center;\n  padding: 32px;\n  border-radius: inherit;\n}\n.furin-electric-border__title { margin: 0; font: 650 26px/1.15 Geist, system-ui, sans-serif; letter-spacing: -0.025em; }\n.furin-electric-border__caption { margin: 10px 0 0; font: 400 15px/1.5 Geist, system-ui, sans-serif; color: rgb(255 255 255 / 0.55); max-width: 30ch; }\n.furin-electric-border__canvas {\n  position: absolute;\n  left: calc(var(--furin-electric-border-pad) * -1);\n  top: calc(var(--furin-electric-border-pad) * -1);\n  width: calc(100% + var(--furin-electric-border-pad) * 2);\n  height: calc(100% + var(--furin-electric-border-pad) * 2);\n  pointer-events: none;\n}\n",
      "type": "registry:file",
      "target": "components/furin/electric-border/ElectricBorder.css"
    }
  ]
}
