{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-lightning",
  "type": "registry:component",
  "title": "Lightning Storm",
  "description": "A night storm with branching bolts that strike at random intervals, light up the clouds with a restrike and fade, over a layer of slanted rain.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "lightning",
    "storm",
    "rain",
    "canvas",
    "weather"
  ],
  "docs": "https://hyperblur.dev/effects/bg-lightning",
  "files": [
    {
      "path": "registry/hyperblur/bg-lightning.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-lightning.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgLightningProps extends React.HTMLAttributes<HTMLDivElement> {\n  accent?: string;\n  background?: string;\n  interval?: number;\n  branches?: number;\n  rain?: boolean;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-ve10 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-ve10-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-ve10-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nfunction rgbOf(value: string, fallback: [number, number, number]): [number, number, number] {\n  const raw = value.trim().replace(\"#\", \"\");\n  const hex = raw.length === 3 ? raw.split(\"\").map((c) => c + c).join(\"\") : raw;\n  const n = parseInt(hex.slice(0, 6), 16);\n  if (hex.length < 6 || Number.isNaN(n)) return fallback;\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\ninterface Bolt {\n  segs: number[];\n  at: number;\n}\n\nexport function BgLightning({\n  accent = \"#a9c8ff\",\n  background = \"#05070d\",\n  interval = 3.2,\n  branches = 1,\n  rain = true,\n  speed = 1,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: BgLightningProps) {\n  const wrapRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n\n  React.useEffect(() => {\n    const wrap = wrapRef.current;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!wrap || !canvas || !ctx) return;\n    const g: CanvasRenderingContext2D = ctx;\n    const el: HTMLDivElement = wrap;\n    const cv: HTMLCanvasElement = canvas;\n    const sky = document.createElement(\"canvas\");\n    const sctx = sky.getContext(\"2d\");\n    if (!sctx) return;\n    const S: CanvasRenderingContext2D = sctx;\n\n    const ac = rgbOf(accent, [169, 200, 255]);\n    const bg = rgbOf(background, [5, 7, 13]);\n\n    let seed = 0x7a11;\n    function rand() {\n      seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0;\n      return seed / 4294967296;\n    }\n\n    let w = 0;\n    let h = 0;\n    let t = 0;\n    let next = 0.6;\n    let bolts: Bolt[] = [];\n    let drops = new Float32Array(0);\n\n    function paintSky(dpr: number) {\n      sky.width = Math.max(1, Math.round(w * dpr));\n      sky.height = Math.max(1, Math.round(h * dpr));\n      S.setTransform(dpr, 0, 0, dpr, 0, 0);\n      S.clearRect(0, 0, w, h);\n      seed = 0x5c1d;\n      const count = Math.round(26 + (w * h) / 9000);\n      for (let i = 0; i < count; i++) {\n        const x = rand() * w * 1.2 - w * 0.1;\n        const y = Math.pow(rand(), 1.6) * h * 0.62 - h * 0.05;\n        const r = (0.12 + rand() * 0.22) * Math.max(w, h * 1.4);\n        const rg = S.createRadialGradient(x, y, 0, x, y, r);\n        const lum = 0.55 + rand() * 0.45;\n        rg.addColorStop(0, `rgba(${Math.round(ac[0] * lum)},${Math.round(ac[1] * lum)},${Math.round(ac[2] * lum)},0.16)`);\n        rg.addColorStop(1, \"rgba(0,0,0,0)\");\n        S.fillStyle = rg;\n        S.beginPath();\n        S.ellipse(x, y, r, r * 0.45, 0, 0, 6.2832);\n        S.fill();\n      }\n      seed = 0x7a11;\n    }\n\n    function setupRain() {\n      const n = rain ? Math.round(Math.min(420, (w * h) / 1400)) : 0;\n      drops = new Float32Array(n * 3);\n      for (let i = 0; i < n; i++) {\n        drops[i * 3] = rand() * w;\n        drops[i * 3 + 1] = rand() * h;\n        drops[i * 3 + 2] = 0.5 + rand() * 0.5;\n      }\n    }\n\n    function crack(segs: number[], x1: number, y1: number, x2: number, y2: number, off: number, depth: number, width: number) {\n      if (depth === 0) {\n        segs.push(x1, y1, x2, y2, width);\n        return;\n      }\n      const mx = (x1 + x2) / 2 + (rand() - 0.5) * off;\n      const my = (y1 + y2) / 2 + (rand() - 0.5) * off * 0.25;\n      crack(segs, x1, y1, mx, my, off * 0.52, depth - 1, width);\n      crack(segs, mx, my, x2, y2, off * 0.52, depth - 1, width);\n    }\n\n    function strike() {\n      const segs: number[] = [];\n      const x0 = w * (0.12 + rand() * 0.76);\n      const x1 = x0 + (rand() - 0.5) * w * 0.35;\n      const y1 = h * (0.62 + rand() * 0.38);\n      crack(segs, x0, -8, x1, y1, Math.max(w, h) * 0.32, 7, 1);\n      const main = segs.length / 5;\n      const nb = Math.round((2 + rand() * 3) * Math.max(0, branches));\n      for (let b = 0; b < nb; b++) {\n        const k = Math.floor(rand() * main * 0.8) * 5;\n        const sx = segs[k];\n        const sy = segs[k + 1];\n        const dir = rand() > 0.5 ? 1 : -1;\n        const len = h * (0.12 + rand() * 0.22);\n        crack(segs, sx, sy, sx + dir * len * (0.5 + rand() * 0.6), sy + len, len * 0.5, 5, 0.55);\n      }\n      bolts.push({ segs, at: t });\n      if (bolts.length > 3) bolts.shift();\n    }\n\n    function bright(age: number) {\n      const a = Math.exp(-age / 0.06);\n      const b = age > 0.16 ? Math.exp(-(age - 0.16) / 0.08) * 0.6 : 0;\n      return Math.min(1, Math.max(a, b) + Math.exp(-age / 0.45) * 0.18);\n    }\n\n    function render(dt: number) {\n      if (!w || !h) return;\n      if (t >= next) {\n        strike();\n        next = t + Math.max(0.6, interval) * (0.55 + rand() * 0.9);\n      }\n      let flash = 0;\n      for (const bo of bolts) flash = Math.max(flash, bright(t - bo.at));\n      g.fillStyle = background;\n      g.fillRect(0, 0, w, h);\n      const sway = Math.sin(t * 0.05) * 18;\n      g.globalAlpha = Math.min(1, 0.45 + flash * 1.4);\n      g.drawImage(sky, sway, 0, w, h);\n      g.globalAlpha = 1;\n      if (flash > 0.01) {\n        const lg = g.createLinearGradient(0, 0, 0, h);\n        lg.addColorStop(0, `rgba(${ac[0]},${ac[1]},${ac[2]},${(flash * 0.22).toFixed(3)})`);\n        lg.addColorStop(1, `rgba(${ac[0]},${ac[1]},${ac[2]},${(flash * 0.04).toFixed(3)})`);\n        g.fillStyle = lg;\n        g.fillRect(0, 0, w, h);\n      }\n      g.globalCompositeOperation = \"lighter\";\n      g.lineCap = \"round\";\n      g.lineJoin = \"round\";\n      for (const bo of bolts) {\n        const age = t - bo.at;\n        if (age > 1.2) continue;\n        const b = bright(age);\n        const passes: [number, string][] = [\n          [9, `rgba(${ac[0]},${ac[1]},${ac[2]},${(b * 0.07).toFixed(3)})`],\n          [3.2, `rgba(${ac[0]},${ac[1]},${ac[2]},${(b * 0.32).toFixed(3)})`],\n          [1.1, `rgba(255,255,255,${b.toFixed(3)})`],\n        ];\n        for (const [lw, col] of passes) {\n          g.strokeStyle = col;\n          const segs = bo.segs;\n          for (const thin of [1, 0.55]) {\n            g.lineWidth = lw * thin;\n            g.beginPath();\n            for (let i = 0; i < segs.length; i += 5) {\n              if (segs[i + 4] !== thin) continue;\n              g.moveTo(segs[i], segs[i + 1]);\n              g.lineTo(segs[i + 2], segs[i + 3]);\n            }\n            g.stroke();\n          }\n        }\n      }\n      g.globalCompositeOperation = \"source-over\";\n      if (drops.length) {\n        g.strokeStyle = `rgba(${Math.min(255, bg[0] + 150)},${Math.min(255, bg[1] + 160)},${Math.min(255, bg[2] + 180)},${(0.1 + flash * 0.3).toFixed(3)})`;\n        g.lineWidth = 0.8;\n        g.beginPath();\n        const n = drops.length / 3;\n        for (let i = 0; i < n; i++) {\n          const z = drops[i * 3 + 2];\n          let x = drops[i * 3] - 210 * z * dt * 0.28;\n          let y = drops[i * 3 + 1] + 760 * z * dt;\n          if (y > h + 10) {\n            y = -12;\n            x = rand() * (w + 60);\n          }\n          if (x < -20) x += w + 40;\n          drops[i * 3] = x;\n          drops[i * 3 + 1] = y;\n          g.moveTo(x, y);\n          g.lineTo(x - 3 * z, y + 13 * z);\n        }\n        g.stroke();\n      }\n    }\n\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    let visible = true;\n    let raf = 0;\n    let prev = 0;\n\n    function still() {\n      bolts = [];\n      t = 10;\n      next = 10;\n      render(0);\n      t = 10.2;\n      render(0);\n    }\n\n    function resize() {\n      const nw = el.clientWidth;\n      const nh = el.clientHeight;\n      if (!nw || !nh) return;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      w = nw;\n      h = nh;\n      cv.width = Math.max(1, Math.round(w * dpr));\n      cv.height = Math.max(1, Math.round(h * dpr));\n      g.setTransform(dpr, 0, 0, dpr, 0, 0);\n      paintSky(dpr);\n      setupRain();\n      if (motion.matches) still();\n      else render(0);\n    }\n\n    function frame(now: number) {\n      const dt = Math.min(0.05, (now - prev) / 1000) * speed;\n      prev = now;\n      t += dt;\n      render(dt);\n      raf = requestAnimationFrame(frame);\n    }\n\n    function sync() {\n      const run = visible && !document.hidden && !motion.matches;\n      if (run && !raf) {\n        prev = performance.now();\n        raf = requestAnimationFrame(frame);\n      } else if (!run && raf) {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n      if (motion.matches) still();\n    }\n\n    const ro = new ResizeObserver(resize);\n    const io = new IntersectionObserver((entries) => {\n      visible = entries[entries.length - 1].isIntersecting;\n      sync();\n    });\n    ro.observe(el);\n    io.observe(el);\n    document.addEventListener(\"visibilitychange\", sync);\n    motion.addEventListener(\"change\", sync);\n    resize();\n    sync();\n\n    return () => {\n      if (raf) cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", sync);\n      motion.removeEventListener(\"change\", sync);\n    };\n  }, [accent, background, interval, branches, rain, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-ve10 ${className}`} style={{ background, ...style }} {...rest}>\n      <style href=\"hb-bg-lightning\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-ve10-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-ve10-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}