{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "flickering-grid",
  "title": "Flickering grid",
  "description": "A canvas grid of small squares that flicker in opacity, follows the theme color, is sharp on retina and pauses off-screen.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "registry/components/backgrounds/flickering-grid.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { useReducedMotion } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\n\n/** Resolves any CSS color (oklch, color-mix, var()) to RGBA by painting it once on a 1×1 canvas. */\nfunction resolveColor(host: HTMLElement, color: string): [number, number, number, number] {\n  const probe = document.createElement(\"span\")\n  probe.style.color = color\n  probe.style.display = \"none\"\n  host.appendChild(probe)\n  const computed = getComputedStyle(probe).color\n  probe.remove()\n  const c = document.createElement(\"canvas\")\n  c.width = c.height = 1\n  const ctx = c.getContext(\"2d\", { willReadFrequently: true })\n  if (!ctx) return [128, 128, 128, 1]\n  ctx.fillStyle = \"#808080\"\n  ctx.fillStyle = computed\n  ctx.fillRect(0, 0, 1, 1)\n  const [r = 128, g = 128, b = 128, a = 255] = ctx.getImageData(0, 0, 1, 1).data\n  return [r, g, b, a / 255]\n}\n\nexport type FlickeringGridProps = React.HTMLAttributes<HTMLDivElement> & {\n  /** Side of each square, in px. */\n  squareSize?: number\n  /** Space between squares, in px. */\n  gap?: number\n  /** Share of squares that change per second (0–1). */\n  flickerChance?: number\n  /** Square color; any CSS color or variable. Re-read when the theme changes. */\n  color?: string\n  /** Highest square opacity (0–1). */\n  maxOpacity?: number\n  /** Frames per second; flicker reads fine at 24–30 and costs half of 60. */\n  fps?: number\n  /** Stop flickering and keep the current frame. */\n  paused?: boolean\n}\n\n/**\n * A canvas grid of small squares that flicker in opacity. DPR-aware, resizes with its container, follows the\n * theme, pauses off-screen and in hidden tabs, and draws one still frame for reduced motion.\n */\nexport function FlickeringGrid({\n  squareSize = 4,\n  gap = 6,\n  flickerChance = 0.3,\n  color = \"var(--foreground)\",\n  maxOpacity = 0.25,\n  fps = 30,\n  paused = false,\n  className,\n  ...props\n}: FlickeringGridProps) {\n  const reduce = useReducedMotion() ?? false\n  const rootRef = React.useRef<HTMLDivElement>(null)\n  const canvasRef = React.useRef<HTMLCanvasElement>(null)\n\n  React.useEffect(() => {\n    const root = rootRef.current\n    const canvas = canvasRef.current\n    const ctx = canvas?.getContext(\"2d\")\n    if (!root || !canvas || !ctx) return\n\n    let w = 0, h = 0, dpr = 1, cols = 0, rows = 0\n    let cells = new Float32Array(0)\n    let rgb = \"rgb(128 128 128)\"\n    let alpha = 1\n    let raf = 0\n    let last = 0\n    let inView = false\n    const pitch = squareSize + gap\n\n    const readColor = () => {\n      const [r, g, b, a] = resolveColor(root, color)\n      rgb = `rgb(${r} ${g} ${b})`\n      alpha = a\n    }\n\n    const draw = () => {\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0)\n      ctx.clearRect(0, 0, w, h)\n      ctx.fillStyle = rgb\n      for (let x = 0; x < cols; x++) {\n        for (let y = 0; y < rows; y++) {\n          const o = cells[x * rows + y]!\n          if (o <= 0.002) continue\n          ctx.globalAlpha = o * alpha\n          ctx.fillRect(x * pitch, y * pitch, squareSize, squareSize)\n        }\n      }\n      ctx.globalAlpha = 1\n    }\n\n    const resize = () => {\n      const r = root.getBoundingClientRect()\n      w = r.width\n      h = r.height\n      dpr = Math.min(window.devicePixelRatio || 1, 2)\n      canvas.width = Math.max(1, Math.round(w * dpr))\n      canvas.height = Math.max(1, Math.round(h * dpr))\n      cols = Math.ceil(w / pitch)\n      rows = Math.ceil(h / pitch)\n      cells = new Float32Array(cols * rows)\n      for (let i = 0; i < cells.length; i++) cells[i] = Math.random() * maxOpacity\n      draw()\n    }\n\n    const frame = (t: number) => {\n      raf = requestAnimationFrame(frame)\n      if (t - last < 1000 / fps) return\n      const dt = Math.min((t - last) / 1000, 0.1)\n      last = t\n      const chance = flickerChance * dt\n      for (let i = 0; i < cells.length; i++) if (Math.random() < chance) cells[i] = Math.random() * maxOpacity\n      draw()\n    }\n\n    const update = () => {\n      const run = inView && !document.hidden && !reduce && !paused\n      if (run && !raf) {\n        last = performance.now()\n        raf = requestAnimationFrame(frame)\n      } else if (!run && raf) {\n        cancelAnimationFrame(raf)\n        raf = 0\n      }\n    }\n\n    readColor()\n    resize()\n\n    const ro = new ResizeObserver(resize)\n    ro.observe(root)\n    const io = new IntersectionObserver(([e]) => {\n      if (!e) return\n      inView = e.isIntersecting\n      update()\n    })\n    io.observe(root)\n    const mo = new MutationObserver(() => {\n      readColor()\n      draw()\n    })\n    mo.observe(document.documentElement, { attributes: true, attributeFilter: [\"class\", \"style\", \"data-theme\"] })\n    const mq = window.matchMedia(\"(prefers-color-scheme: dark)\")\n    const onScheme = () => {\n      readColor()\n      draw()\n    }\n    mq.addEventListener(\"change\", onScheme)\n    document.addEventListener(\"visibilitychange\", update)\n\n    return () => {\n      cancelAnimationFrame(raf)\n      ro.disconnect()\n      io.disconnect()\n      mo.disconnect()\n      mq.removeEventListener(\"change\", onScheme)\n      document.removeEventListener(\"visibilitychange\", update)\n    }\n  }, [squareSize, gap, flickerChance, color, maxOpacity, fps, paused, reduce])\n\n  return (\n    <div ref={rootRef} aria-hidden=\"true\" className={cn(\"pointer-events-none absolute inset-0 overflow-hidden\", className)} {...props}>\n      <canvas ref={canvasRef} className=\"block size-full\" />\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/flickering-grid.tsx"
    }
  ],
  "categories": [
    "components",
    "backgrounds"
  ],
  "type": "registry:component"
}