{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "noise-background",
  "title": "Noise Background",
  "description": "A canvas background with animated film-grain noise, soft color blob gradients, 7 built-in themes (or fully custom colors), drifting blob animation, and a vignette.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/noise-background/noise-background.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\ninterface NoiseBlob {\n  x: number;\n  y: number;\n  rx: number;\n  ry: number;\n  color: string;\n  opacity: number;\n}\n\ninterface NoiseTheme {\n  backgroundColor: string;\n  blobs: NoiseBlob[];\n}\n\nconst THEMES: Record<string, NoiseTheme> = {\n  cognac: {\n    backgroundColor: \"#040302\",\n    blobs: [\n      { x: 0.84, y: 0.25, rx: 0.5, ry: 0.7, color: \"#c83c0a\", opacity: 0.62 },\n      { x: 0.12, y: 0.72, rx: 0.42, ry: 0.6, color: \"#b03008\", opacity: 0.4 },\n    ],\n  },\n  crimson: {\n    backgroundColor: \"#040202\",\n    blobs: [\n      { x: 0.82, y: 0.2, rx: 0.48, ry: 0.66, color: \"#8a0000\", opacity: 0.6 },\n      { x: 0.16, y: 0.7, rx: 0.4, ry: 0.58, color: \"#700000\", opacity: 0.38 },\n    ],\n  },\n  orchid: {\n    backgroundColor: \"#040208\",\n    blobs: [\n      { x: 0.14, y: 0.22, rx: 0.48, ry: 0.66, color: \"#700090\", opacity: 0.58 },\n      { x: 0.84, y: 0.72, rx: 0.4, ry: 0.58, color: \"#580070\", opacity: 0.38 },\n    ],\n  },\n  forest: {\n    backgroundColor: \"#020402\",\n    blobs: [\n      { x: 0.16, y: 0.25, rx: 0.48, ry: 0.66, color: \"#005522\", opacity: 0.58 },\n      { x: 0.84, y: 0.74, rx: 0.4, ry: 0.58, color: \"#004018\", opacity: 0.38 },\n    ],\n  },\n  midnight: {\n    backgroundColor: \"#020306\",\n    blobs: [\n      { x: 0.85, y: 0.22, rx: 0.5, ry: 0.68, color: \"#001278\", opacity: 0.62 },\n      { x: 0.12, y: 0.74, rx: 0.42, ry: 0.6, color: \"#001580\", opacity: 0.38 },\n    ],\n  },\n  slate: {\n    backgroundColor: \"#030508\",\n    blobs: [\n      { x: 0.84, y: 0.2, rx: 0.46, ry: 0.64, color: \"#1a2e5a\", opacity: 0.58 },\n      { x: 0.14, y: 0.72, rx: 0.38, ry: 0.56, color: \"#122650\", opacity: 0.36 },\n    ],\n  },\n  obsidian: { backgroundColor: \"#070707\", blobs: [] },\n};\n\nexport type NoiseBackgroundTheme = keyof typeof THEMES | \"custom\";\n\nexport interface NoiseBackgroundProps extends Omit<React.ComponentPropsWithoutRef<\"div\">, \"children\"> {\n  colorTheme?: NoiseBackgroundTheme;\n  backgroundColor?: string;\n  blobColor?: string;\n  blobX?: number;\n  blobY?: number;\n  blobRadiusX?: number;\n  blobRadiusY?: number;\n  blobOpacity?: number;\n  noiseOpacity?: number;\n  noiseSpeed?: number;\n  grainSize?: number;\n  blendMode?: \"soft-light\" | \"screen\";\n  vignetteEnabled?: boolean;\n  vignetteIntensity?: number;\n  animateBlobs?: boolean;\n  animateSpeed?: number;\n}\n\nfunction hexToRgb(hex: string): [number, number, number] {\n  const c = (hex || \"#000000\").replace(\"#\", \"\");\n  const full = c.length === 3 ? c.split(\"\").map((x) => x + x).join(\"\") : c;\n  return [parseInt(full.slice(0, 2), 16) || 0, parseInt(full.slice(2, 4), 16) || 0, parseInt(full.slice(4, 6), 16) || 0];\n}\n\nexport function NoiseBackground({\n  colorTheme = \"slate\",\n  backgroundColor = \"#060403\",\n  blobColor = \"#c84010\",\n  blobX = 0.2,\n  blobY = 0.5,\n  blobRadiusX = 0.5,\n  blobRadiusY = 0.8,\n  blobOpacity = 0.7,\n  noiseOpacity = 0.1,\n  noiseSpeed = 24,\n  grainSize = 150,\n  blendMode = \"screen\",\n  vignetteEnabled = true,\n  vignetteIntensity = 0.62,\n  animateBlobs = false,\n  animateSpeed = 0.5,\n  className,\n  ...props\n}: NoiseBackgroundProps) {\n  const containerRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n  const liveRef = React.useRef({\n    backgroundColor,\n    blobs: [] as NoiseBlob[],\n    noiseOpacity,\n    noiseSpeed,\n    grainSize,\n    blendMode,\n    vignetteEnabled,\n    vignetteIntensity,\n    animateBlobs,\n    animateSpeed,\n  });\n  const stateRef = React.useRef({\n    time: 0,\n    lastNoiseUpdate: -99999,\n    lastGrainSize: -1,\n    cachedPattern: null as CanvasPattern | null,\n    width: 0,\n    height: 0,\n  });\n\n  const theme = colorTheme !== \"custom\" ? THEMES[colorTheme] : null;\n  const customBlobs: NoiseBlob[] = [{ x: blobX, y: blobY, rx: blobRadiusX, ry: blobRadiusY, color: blobColor, opacity: blobOpacity }];\n\n  React.useEffect(() => {\n    liveRef.current = {\n      backgroundColor: theme?.backgroundColor ?? backgroundColor,\n      blobs: theme?.blobs ?? customBlobs,\n      noiseOpacity,\n      noiseSpeed,\n      grainSize,\n      blendMode,\n      vignetteEnabled,\n      vignetteIntensity,\n      animateBlobs,\n      animateSpeed,\n    };\n  });\n\n  React.useEffect(() => {\n    const container = containerRef.current;\n    const canvas = canvasRef.current;\n    if (!container || !canvas) return;\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n\n    const noiseCanvas = document.createElement(\"canvas\");\n    const noiseCtx = noiseCanvas.getContext(\"2d\");\n    if (!noiseCtx) return;\n\n    const buildNoise = (size: number) => {\n      noiseCanvas.width = size;\n      noiseCanvas.height = size;\n      const id = noiseCtx.createImageData(size, size);\n      const d = id.data;\n      for (let i = 0; i < d.length; i += 4) {\n        const v = (Math.random() * 255) | 0;\n        d[i] = d[i + 1] = d[i + 2] = v;\n        d[i + 3] = 255;\n      }\n      noiseCtx.putImageData(id, 0, 0);\n      stateRef.current.cachedPattern = ctx.createPattern(noiseCanvas, \"repeat\");\n    };\n\n    const resize = () => {\n      const w = container.clientWidth;\n      const h = container.clientHeight;\n      if (!w || !h) return;\n      const dpr = window.devicePixelRatio || 1;\n      canvas.width = w * dpr;\n      canvas.height = h * dpr;\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      stateRef.current.width = w;\n      stateRef.current.height = h;\n      stateRef.current.cachedPattern = null;\n    };\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(container);\n    resize();\n\n    let lastTs = 0;\n    let rafId = 0;\n\n    const loop = (ts: number) => {\n      const dt = Math.min((ts - lastTs) / 1000, 0.05);\n      lastTs = ts;\n\n      const s = stateRef.current;\n      const live = liveRef.current;\n      const { width: w, height: h } = s;\n\n      if (!w || !h) {\n        rafId = requestAnimationFrame(loop);\n        return;\n      }\n\n      s.time += dt;\n\n      ctx.clearRect(0, 0, w, h);\n      ctx.fillStyle = live.backgroundColor;\n      ctx.fillRect(0, 0, w, h);\n\n      for (let i = 0; i < live.blobs.length; i++) {\n        const blob = live.blobs[i];\n        const driftX = live.animateBlobs ? Math.sin(s.time * 0.08 * live.animateSpeed + i * 2.4) * 0.06 : 0;\n        const driftY = live.animateBlobs ? Math.cos(s.time * 0.06 * live.animateSpeed + i * 1.7) * 0.04 : 0;\n        const bx = (blob.x + driftX) * w;\n        const by = (blob.y + driftY) * h;\n        const rx = blob.rx * w;\n        const ry = blob.ry * w;\n        if (rx <= 0 || ry <= 0) continue;\n\n        const [r, g, b] = hexToRgb(blob.color);\n\n        ctx.save();\n        ctx.translate(bx, by);\n        ctx.scale(rx / ry, 1);\n\n        const o = blob.opacity;\n        const grd = ctx.createRadialGradient(0, 0, 0, 0, 0, ry);\n        grd.addColorStop(0, `rgba(${r},${g},${b},${o})`);\n        grd.addColorStop(0.22, `rgba(${r},${g},${b},${o * 0.72})`);\n        grd.addColorStop(0.45, `rgba(${r},${g},${b},${o * 0.38})`);\n        grd.addColorStop(0.68, `rgba(${r},${g},${b},${o * 0.12})`);\n        grd.addColorStop(0.86, `rgba(${r},${g},${b},${o * 0.03})`);\n        grd.addColorStop(1, `rgba(${r},${g},${b},0)`);\n\n        ctx.beginPath();\n        ctx.arc(0, 0, ry, 0, Math.PI * 2);\n        ctx.fillStyle = grd;\n        ctx.fill();\n        ctx.restore();\n      }\n\n      const intervalMs = live.noiseSpeed > 0 ? 1000 / live.noiseSpeed : Infinity;\n      const grainChanged = s.lastGrainSize !== live.grainSize;\n      const shouldAnimate = live.noiseSpeed > 0 && ts - s.lastNoiseUpdate > intervalMs;\n      if (s.cachedPattern === null || grainChanged || shouldAnimate) {\n        buildNoise(live.grainSize);\n        s.lastNoiseUpdate = ts;\n        s.lastGrainSize = live.grainSize;\n      }\n\n      if (s.cachedPattern && live.noiseOpacity > 0) {\n        ctx.save();\n        ctx.globalAlpha = live.noiseOpacity;\n        ctx.globalCompositeOperation = live.blendMode;\n        ctx.fillStyle = s.cachedPattern;\n        ctx.fillRect(0, 0, w, h);\n        ctx.restore();\n      }\n\n      if (live.vignetteEnabled && live.vignetteIntensity > 0) {\n        const innerR = Math.min(w, h) * 0.15;\n        const outerR = Math.sqrt(w * w + h * h) * 0.65;\n        const vgrd = ctx.createRadialGradient(w / 2, h / 2, innerR, w / 2, h / 2, outerR);\n        vgrd.addColorStop(0, \"rgba(0,0,0,0)\");\n        vgrd.addColorStop(1, `rgba(0,0,0,${live.vignetteIntensity})`);\n        ctx.fillStyle = vgrd;\n        ctx.fillRect(0, 0, w, h);\n      }\n\n      rafId = requestAnimationFrame(loop);\n    };\n\n    rafId = requestAnimationFrame(loop);\n\n    return () => {\n      cancelAnimationFrame(rafId);\n      ro.disconnect();\n    };\n  }, []);\n\n  return (\n    <div ref={containerRef} className={cn(\"relative w-full h-full overflow-hidden\", className)} {...props}>\n      <canvas ref={canvasRef} className=\"absolute inset-0 h-full w-full\" />\n\n          </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}