{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"bolt-strike","type":"registry:ui","title":"Bolt Strike","description":"A night storm where procedural, forking lightning bolts strike at random spots and light up the clouds. Canvas 2D.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/bolt-strike.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface BoltStrikeProps {\n  /** Glow color of the bolts and the flash. */\n  color?: string;\n  /** Storm tempo; higher means more frequent strikes. 0 pauses. */\n  speed?: number;\n  /** Bolt thickness multiplier. */\n  scale?: number;\n  /** Strength of the sky flash, 0..1. */\n  flash?: number;\n  /** How much the bolts fork, 0..1. */\n  branching?: number;\n  className?: string;\n}\n\ntype Line = { pts: number[]; w: number };\n\ninterface Strike {\n  lines: Line[];\n  age: number;\n  x: number;\n  ground: number;\n}\n\n/** Jagged polyline from (x, y) heading down-ish, plus recursive forks. */\nfunction grow(x: number, y: number, len: number, angle: number, w: number, fork: number, out: Line[], depth = 0) {\n  const pts = [x, y];\n  const steps = Math.max(4, Math.round(len / 14));\n  const stepLen = len / steps;\n  for (let i = 0; i < steps; i++) {\n    const a = angle + (Math.random() - 0.5) * 1.3;\n    x += Math.sin(a) * stepLen;\n    y += Math.cos(a) * stepLen;\n    pts.push(x, y);\n    if (depth < 3 && Math.random() < fork * 0.09) {\n      const side = Math.random() < 0.5 ? -1 : 1;\n      grow(x, y, len * (0.2 + Math.random() * 0.3), angle + side * (0.4 + Math.random() * 0.6), w * 0.55, fork, out, depth + 1);\n    }\n  }\n  out.push({ pts, w });\n}\n\nfunction strength(age: number) {\n  // A bright stroke, a quick dip, a return stroke, then a fading afterglow.\n  const env = Math.exp(-age * 3.5);\n  if (age < 0.07) return 1;\n  if (age < 0.12) return 0.35 * env;\n  if (age < 0.2) return 0.95 * env;\n  return env * 0.8;\n}\n\nexport function BoltStrike({\n  color = \"#a5b4fc\",\n  speed = 1,\n  scale = 1,\n  flash = 0.6,\n  branching = 0.6,\n  className,\n}: BoltStrikeProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const opts = useRef({ color, speed, scale, flash, branching });\n  const redraw = useRef<(() => void) | null>(null);\n\n  useEffect(() => {\n    opts.current = { color, speed, scale, flash, branching };\n    redraw.current?.();\n  }, [color, speed, scale, flash, branching]);\n\n  useEffect(() => {\n    const el = host.current!;\n    const canvas = el.querySelector(\"canvas\")!;\n    const ctx = canvas.getContext(\"2d\");\n    const clouds = document.createElement(\"canvas\");\n    const cctx = clouds.getContext(\"2d\");\n    if (!ctx || !cctx) return;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n\n    let dpr = 1;\n    let raf = 0;\n    let visible = true;\n    let last = 0;\n    let strikes: Strike[] = [];\n    let wait = 0.6;\n\n    const paintClouds = () => {\n      const W = (clouds.width = Math.max(1, Math.round(canvas.width / 4)));\n      const H = (clouds.height = Math.max(1, Math.round(canvas.height / 4)));\n      cctx.clearRect(0, 0, W, H);\n      // Billowy cloud base: many small puffs clustered along a wavy ceiling.\n      for (let i = 0; i < 140; i++) {\n        const x = Math.random() * W;\n        const y = (0.05 + Math.pow(Math.random(), 2) * 0.35 + Math.sin(x / W * 7) * 0.04) * H;\n        const r = (0.03 + Math.random() * 0.08) * Math.max(W, H);\n        const g = cctx.createRadialGradient(x, y, 0, x, y, r);\n        g.addColorStop(0, \"rgba(190,200,255,0.1)\");\n        g.addColorStop(1, \"rgba(190,200,255,0)\");\n        cctx.fillStyle = g;\n        cctx.fillRect(x - r, y - r, r * 2, r * 2);\n      }\n    };\n\n    const strike = (age = 0) => {\n      const W = canvas.width;\n      const H = canvas.height;\n      const x = W * (0.1 + Math.random() * 0.8);\n      const ground = H * (0.78 + Math.random() * 0.22);\n      const lines: Line[] = [];\n      grow(x, -10, ground, (Math.random() - 0.5) * 0.4, 1, opts.current.branching, lines);\n      strikes.push({ lines, age, x, ground });\n    };\n\n    const draw = () => {\n      const o = opts.current;\n      const W = canvas.width;\n      const H = canvas.height;\n      ctx.clearRect(0, 0, W, H);\n      const lit = strikes.reduce((m, s) => Math.max(m, strength(s.age)), 0);\n\n      // Clouds brighten with each flash.\n      ctx.globalCompositeOperation = \"lighter\";\n      ctx.globalAlpha = Math.min(1, 0.22 + lit * 1.5 * o.flash);\n      ctx.drawImage(clouds, 0, 0, W, H);\n\n      // Sky flash.\n      ctx.globalAlpha = lit * o.flash * 0.22;\n      ctx.fillStyle = o.color;\n      ctx.fillRect(0, 0, W, H);\n\n      ctx.lineJoin = \"round\";\n      ctx.lineCap = \"round\";\n      const k = dpr * Math.max(o.scale, 0.1);\n      for (const s of strikes) {\n        // The leader races down during the first few frames.\n        const reach = Math.min(1, s.age / 0.05);\n        const I = strength(s.age);\n        if (I < 0.01) continue;\n        const halo = ctx.createRadialGradient(s.x, 0, 0, s.x, 0, H * 0.7);\n        halo.addColorStop(0, o.color);\n        halo.addColorStop(1, \"transparent\");\n        ctx.globalAlpha = I * 0.35 * o.flash;\n        ctx.fillStyle = halo;\n        ctx.fillRect(0, 0, W, H);\n        for (const [width, alpha, stroke] of [\n          [18, 0.08, o.color],\n          [6, 0.35, o.color],\n          [2, 1, \"#ffffff\"],\n        ] as const) {\n          ctx.strokeStyle = stroke;\n          for (const l of s.lines) {\n            ctx.globalAlpha = I * alpha * (l.w < 1 ? 0.75 : 1);\n            ctx.lineWidth = width * k * l.w;\n            ctx.beginPath();\n            ctx.moveTo(l.pts[0], l.pts[1]);\n            for (let i = 2; i < l.pts.length; i += 2) {\n              if (l.pts[i + 1] > s.ground * reach) break;\n              ctx.lineTo(l.pts[i], l.pts[i + 1]);\n            }\n            ctx.stroke();\n          }\n        }\n        // Impact glow at the bottom of the main channel.\n        if (reach >= 1) {\n          const main = s.lines[s.lines.length - 1].pts;\n          const gx = main[main.length - 2];\n          const gy = main[main.length - 1];\n          const r = 90 * k;\n          const g = ctx.createRadialGradient(gx, gy, 0, gx, gy, r);\n          g.addColorStop(0, o.color);\n          g.addColorStop(1, \"transparent\");\n          ctx.globalAlpha = I * 0.5;\n          ctx.fillStyle = g;\n          ctx.fillRect(gx - r, gy - r, r * 2, r * 2);\n        }\n      }\n      ctx.globalAlpha = 1;\n      ctx.globalCompositeOperation = \"source-over\";\n    };\n\n    const loop = (now: number) => {\n      const dt = (Math.min(now - last, 50) / 1000) * opts.current.speed;\n      last = now;\n      for (const s of strikes) s.age += dt;\n      strikes = strikes.filter((s) => s.age < 1.4);\n      wait -= dt;\n      if (wait <= 0) {\n        strike();\n        // Occasionally a second bolt follows right away.\n        wait = Math.random() < 0.2 ? 0.15 : 1 + Math.random() * 2.8;\n      }\n      draw();\n      raf = requestAnimationFrame(loop);\n    };\n    const play = () => {\n      if (raf || !visible || reduced) return;\n      last = performance.now();\n      raf = requestAnimationFrame(loop);\n    };\n    const resize = () => {\n      dpr = Math.min(window.devicePixelRatio || 1, 2);\n      canvas.width = Math.max(1, Math.round(el.clientWidth * dpr));\n      canvas.height = Math.max(1, Math.round(el.clientHeight * dpr));\n      paintClouds();\n      strikes = [];\n      if (reduced) strike(0.25);\n      draw();\n    };\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(el);\n    const io = new IntersectionObserver(([e]) => {\n      visible = e.isIntersecting;\n      if (visible) play();\n      else {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n    });\n    io.observe(el);\n    redraw.current = draw;\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      redraw.current = null;\n    };\n  }, []);\n\n  return (\n    <div\n      ref={host}\n      aria-hidden\n      className={className}\n      style={{\n        position: \"absolute\",\n        inset: 0,\n        overflow: \"hidden\",\n        pointerEvents: \"none\",\n        background: \"linear-gradient(180deg, #0b1024 0%, #05060d 70%, #020205 100%)\",\n      }}\n    >\n      <canvas style={{ position: \"absolute\", inset: 0, width: \"100%\", height: \"100%\", display: \"block\" }} />\n    </div>\n  );\n}\n"}]}