{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "electric-storm",
  "title": "Electric Storm",
  "description": "A night storm background shader: rolling clouds and slanting rain, with jagged lightning that strikes on its own every few seconds, flickers, branches and lights up the clouds around it; click or tap to call a strike down where you point.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/electric-storm/electric-storm.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport interface ElectricStormProps extends Omit<React.ComponentPropsWithoutRef<\"div\">, \"children\"> {\n  /** Content shown on top of the background. */\n  children?: React.ReactNode;\n  /** Storm colours: the lightning bolt, its glow in the clouds, the clouds and the rain. */\n  colors?: [string, string, string, string];\n  /** Base colour behind the effect. */\n  background?: string;\n  /** Animation speed multiplier. */\n  speed?: number;\n  /** Overall brightness of the effect. */\n  intensity?: number;\n  /** Click or tap to strike lightning where you point. */\n  interactive?: boolean;\n  /** Render scale: 1 is full resolution, lower values are cheaper on very large screens. */\n  resolution?: number;\n  /** Freeze the animation on its current frame. */\n  paused?: boolean;\n}\n\nconst DEFAULT_COLORS: [string, string, string, string] = [\"#c9dcff\", \"#6a5cff\", \"#1b2140\", \"#9aa8c9\"];\nconst DEFAULT_BACKGROUND = \"#05060d\";\n\nconst VERTEX = `attribute vec2 aPos;\nvoid main() { gl_Position = vec4(aPos, 0.0, 1.0); }`;\n\nconst FRAGMENT = `#ifdef GL_FRAGMENT_PRECISION_HIGH\nprecision highp float;\n#else\nprecision mediump float;\n#endif\nuniform vec2 uRes;\nuniform float uTime;\nuniform vec2 uMouse;\nuniform float uHover;\nuniform float uIntensity;\nuniform vec3 uBg;\nuniform vec3 uC0;\nuniform vec3 uC1;\nuniform vec3 uC2;\nuniform vec3 uC3;\nuniform float uStrike;\nuniform float uStrikeX;\nuniform float uSeed;\nfloat hash(vec2 p) {\n  p = fract(p * vec2(123.34, 456.21));\n  p += dot(p, p + 45.32);\n  return fract(p.x * p.y);\n}\nfloat noise(vec2 p) {\n  vec2 i = floor(p);\n  vec2 f = fract(p);\n  vec2 u = f * f * (3.0 - 2.0 * f);\n  return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), u.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), u.x), u.y);\n}\nfloat fbm(vec2 p) {\n  float v = 0.0;\n  float a = 0.5;\n  for (int i = 0; i < 5; i++) {\n    v += a * noise(p);\n    p = p * 2.03 + vec2(1.7, 9.2);\n    a *= 0.5;\n  }\n  return v;\n}\nfloat boltX(float y, float seed) {\n  return (fbm(vec2(y * 3.2, seed)) - 0.5) * 0.38 + (noise(vec2(y * 26.0, seed * 3.0)) - 0.5) * 0.05;\n}\nvoid main() {\n  vec2 uv = gl_FragCoord.xy / uRes;\n  float ar = uRes.x / uRes.y;\n  vec2 p = vec2(uv.x * ar, uv.y);\n  float t = uTime;\n\n  // Flash envelope: a fast decay with a couple of flickers.\n  float env = exp(-uStrike * 5.0) * (0.65 + 0.35 * step(0.5, fract(uStrike * 14.0)));\n  env = uStrike < 1.2 ? env : 0.0;\n\n  float clouds = fbm(p * 1.6 + vec2(t * 0.04, 0.0)) * 0.7 + fbm(p * 4.0 - vec2(t * 0.07, 0.0)) * 0.3;\n  float sky = smoothstep(0.15, 1.0, uv.y);\n  vec3 col = mix(uBg, uC2 * 2.3, pow(clouds, 1.5) * sky);\n\n  // The bolt: a jagged path from the top down to the ground at the strike point, plus one branch.\n  float sx = uStrikeX * ar;\n  float dx = abs(p.x - sx - boltX(uv.y, uSeed) * (1.0 - uv.y * 0.3));\n  float ground = 0.06;\n  float onBolt = step(ground, uv.y);\n  float core = exp(-dx * 700.0) * 1.6 + exp(-dx * 60.0) * 0.35;\n  float by = 0.55 + hash(vec2(uSeed, 1.0)) * 0.2;\n  float bdx = abs(p.x - sx - boltX(by, uSeed) - (by - uv.y) * (hash(vec2(uSeed, 2.0)) - 0.5) * 1.4 - boltX(uv.y * 1.7, uSeed + 7.0) * 0.4);\n  float branch = step(by - 0.32, uv.y) * step(uv.y, by) * (exp(-bdx * 900.0) + exp(-bdx * 80.0) * 0.2);\n  col += uC0 * (core * onBolt + branch * 0.8) * env;\n\n  // Clouds and sky light up around the strike.\n  float near = exp(-pow((p.x - sx) * 1.4, 2.0));\n  col += uC1 * clouds * sky * env * (0.5 + 1.2 * near);\n  col += uC0 * env * 0.06;\n\n  // Rain: thin slanted streaks.\n  vec2 rp = vec2(p.x * 110.0 + p.y * 22.0, p.y * 9.0 + t * 7.0);\n  vec2 rc = floor(rp);\n  vec2 rf = fract(rp);\n  float rain = step(0.94, hash(rc)) * smoothstep(0.35, 0.0, abs(rf.x - 0.5)) * smoothstep(0.0, 0.25, rf.y) * smoothstep(0.75, 0.45, rf.y) * 0.16;\n  col += uC3 * rain * (0.6 + env);\n  col *= uIntensity;\n  col *= 1.0 - 0.25 * length((uv - 0.5) * vec2(1.0, 0.8));\n  gl_FragColor = vec4(col, 1.0);\n}`;\n\nconst UNIFORMS = [\"uRes\", \"uTime\", \"uMouse\", \"uHover\", \"uIntensity\", \"uBg\", \"uC0\", \"uC1\", \"uC2\", \"uC3\", \"uStrike\", \"uStrikeX\", \"uSeed\"] as const;\n\nfunction rgb(hex: string): [number, number, number] {\n  const h = hex.replace(\"#\", \"\");\n  const full =\n    h.length === 3\n      ? h\n          .split(\"\")\n          .map((c) => c + c)\n          .join(\"\")\n      : h.slice(0, 6);\n  const n = parseInt(full, 16) || 0;\n  return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255];\n}\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    (callback) => {\n      const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n      query.addEventListener(\"change\", callback);\n      return () => query.removeEventListener(\"change\", callback);\n    },\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\nfunction compile(gl: WebGLRenderingContext, type: number, source: string) {\n  const shader = gl.createShader(type);\n  if (!shader) return null;\n  gl.shaderSource(shader, source);\n  gl.compileShader(shader);\n  if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {\n    console.warn(\"ElectricStorm shader:\", gl.getShaderInfoLog(shader));\n    gl.deleteShader(shader);\n    return null;\n  }\n  return shader;\n}\n\nexport function ElectricStorm({\n  children,\n  colors = DEFAULT_COLORS,\n  background = DEFAULT_BACKGROUND,\n  speed = 1,\n  intensity = 1,\n  interactive = true,\n  resolution = 1,\n  paused = false,\n  className,\n  style,\n  onPointerMove,\n  onPointerEnter,\n  onPointerLeave,\n  onPointerDown,\n  onPointerUp,\n  ...props\n}: ElectricStormProps) {\n  const reduce = useReducedMotion();\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n  const [failed, setFailed] = React.useState(false);\n  // The render loop reads the latest props from here, so changing a prop never rebuilds WebGL.\n  const live = React.useRef({ colors, background, speed, intensity, interactive, resolution, still: paused || reduce });\n  const kick = React.useRef<() => void>(() => {});\n  // Pointer state in 0..1 (y up, like gl_FragCoord), eased toward its target every frame.\n  const input = React.useRef({ x: 0.5, y: 0.5, tx: 0.5, ty: 0.5, hover: 0, thover: 0, down: false, clickX: 0.5, clickY: 0.5, clicks: 0 });\n\n  React.useEffect(() => {\n    live.current = { colors, background, speed, intensity, interactive, resolution, still: paused || reduce };\n    kick.current();\n  }, [colors, background, speed, intensity, interactive, resolution, paused, reduce]);\n\n  React.useEffect(() => {\n    const canvas = canvasRef.current;\n    const root = rootRef.current;\n    if (!canvas || !root) return;\n    const gl = canvas.getContext(\"webgl\", { antialias: false, alpha: false, preserveDrawingBuffer: true, powerPreference: \"low-power\" });\n    const fail = () => requestAnimationFrame(() => setFailed(true));\n    if (!gl) {\n      fail();\n      return;\n    }\n    const vs = compile(gl, gl.VERTEX_SHADER, VERTEX);\n    const fs = compile(gl, gl.FRAGMENT_SHADER, FRAGMENT);\n    const program = gl.createProgram();\n    if (!vs || !fs || !program) {\n      fail();\n      return;\n    }\n    gl.attachShader(program, vs);\n    gl.attachShader(program, fs);\n    gl.linkProgram(program);\n    if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {\n      fail();\n      return;\n    }\n    gl.useProgram(program);\n    const buffer = gl.createBuffer();\n    gl.bindBuffer(gl.ARRAY_BUFFER, buffer);\n    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW);\n    const aPos = gl.getAttribLocation(program, \"aPos\");\n    gl.enableVertexAttribArray(aPos);\n    gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0);\n    const u = Object.fromEntries(UNIFORMS.map((name) => [name, gl.getUniformLocation(program, name)])) as Record<(typeof UNIFORMS)[number], WebGLUniformLocation | null>;\n\n    const state = { time: 2, last: 0, strike: 9, strikeX: 0.62, seed: 1, next: 2.47, clicks: 0 };\n    let raf = 0;\n    let visible = true;\n\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 1.5) * Math.max(0.25, Math.min(1, live.current.resolution));\n      const w = Math.max(1, Math.round(root.clientWidth * dpr));\n      const h = Math.max(1, Math.round(root.clientHeight * dpr));\n      if (canvas.width !== w || canvas.height !== h) {\n        canvas.width = w;\n        canvas.height = h;\n        gl.viewport(0, 0, w, h);\n      }\n    };\n\n    const draw = (dt: number) => {\n      const L = live.current;\n      const inp = input.current;\n      const ease = 1 - Math.exp(-dt * 5);\n      inp.x += (inp.tx - inp.x) * ease;\n      inp.y += (inp.ty - inp.y) * ease;\n      inp.hover += ((L.interactive ? inp.thover : 0) - inp.hover) * (1 - Math.exp(-dt * 3));\n      const step = L.still ? 0 : dt * L.speed;\n      state.time += step;\n      // Strikes come on their own every few seconds, and immediately wherever someone clicks.\n      state.strike += step;\n      state.next -= step;\n      const clicked = L.interactive && inp.clicks !== state.clicks;\n      if (clicked || state.next <= 0) {\n        state.clicks = inp.clicks;\n        state.strike = 0;\n        state.strikeX = clicked ? inp.clickX : 0.15 + Math.random() * 0.7;\n        state.seed = Math.random() * 100;\n        state.next = 2.4 + Math.random() * 3.2;\n      }\n      gl.uniform1f(u.uStrike, state.strike);\n      gl.uniform1f(u.uStrikeX, state.strikeX);\n      gl.uniform1f(u.uSeed, state.seed);\n      const [c0, c1, c2, c3] = L.colors;\n      gl.uniform2f(u.uRes, canvas.width, canvas.height);\n      gl.uniform1f(u.uTime, state.time);\n      gl.uniform2f(u.uMouse, inp.x, inp.y);\n      gl.uniform1f(u.uHover, inp.hover);\n      gl.uniform1f(u.uIntensity, L.intensity);\n      gl.uniform3fv(u.uBg, rgb(L.background));\n      gl.uniform3fv(u.uC0, rgb(c0));\n      gl.uniform3fv(u.uC1, rgb(c1));\n      gl.uniform3fv(u.uC2, rgb(c2));\n      gl.uniform3fv(u.uC3, rgb(c3));\n      gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);\n    };\n\n    const frame = (now: number) => {\n      const dt = Math.min(0.05, state.last ? (now - state.last) / 1000 : 1 / 60);\n      state.last = now;\n      resize();\n      draw(dt);\n      // Keep running while visible and moving; a still background draws once and waits for a kick.\n      if (visible && !document.hidden && !live.current.still) raf = requestAnimationFrame(frame);\n      else state.last = 0;\n    };\n    const start = () => {\n      cancelAnimationFrame(raf);\n      raf = requestAnimationFrame(frame);\n    };\n    kick.current = start;\n\n    const ro = new ResizeObserver(start);\n    ro.observe(root);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible) start();\n    });\n    io.observe(root);\n    const onVisibility = () => !document.hidden && start();\n    document.addEventListener(\"visibilitychange\", onVisibility);\n    const onLost = (event: Event) => {\n      event.preventDefault();\n      setFailed(true);\n    };\n    canvas.addEventListener(\"webglcontextlost\", onLost);\n    start();\n\n    return () => {\n      cancelAnimationFrame(raf);\n      kick.current = () => {};\n      ro.disconnect();\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", onVisibility);\n      canvas.removeEventListener(\"webglcontextlost\", onLost);\n      gl.deleteBuffer(buffer);\n      gl.deleteProgram(program);\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n    };\n  }, []);\n\n  const point = (event: React.PointerEvent<HTMLDivElement>) => {\n    const r = event.currentTarget.getBoundingClientRect();\n    const inp = input.current;\n    inp.tx = (event.clientX - r.left) / Math.max(1, r.width);\n    inp.ty = 1 - (event.clientY - r.top) / Math.max(1, r.height);\n  };\n\n  return (\n    <div\n      ref={rootRef}\n      className={cn(\"relative isolate overflow-hidden\", className)}\n      style={{ background, ...style }}\n      onPointerMove={(event) => {\n        point(event);\n        onPointerMove?.(event);\n      }}\n      onPointerEnter={(event) => {\n        point(event);\n        input.current.thover = 1;\n        onPointerEnter?.(event);\n      }}\n      onPointerLeave={(event) => {\n        input.current.thover = 0;\n        input.current.down = false;\n        onPointerLeave?.(event);\n      }}\n      onPointerDown={(event) => {\n        point(event);\n        const inp = input.current;\n        inp.down = true;\n        inp.clickX = inp.tx;\n        inp.clickY = inp.ty;\n        inp.clicks += 1;\n        onPointerDown?.(event);\n      }}\n      onPointerUp={(event) => {\n        input.current.down = false;\n        onPointerUp?.(event);\n      }}\n      {...props}\n    >\n      <canvas ref={canvasRef} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 -z-10 h-full w-full\" />\n      {failed && (\n        <div\n          aria-hidden=\"true\"\n          className=\"absolute inset-0 -z-10\"\n          style={{ background: `radial-gradient(120% 90% at 50% 0%, ${colors[1]}55, transparent 60%), radial-gradient(90% 70% at 50% 100%, ${colors[0]}33, transparent 70%), ${background}` }}\n        />\n      )}\n      {children}\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}