{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "plasma",
  "type": "registry:component",
  "title": "Plasma",
  "description": "Classic plasma: a small buffer of interfering sine waves mapped through a color gradient and upscaled smooth. Canvas, no dependencies.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "plasma",
    "canvas",
    "background",
    "gradient",
    "retro",
    "demoscene"
  ],
  "docs": "https://hyperblur.dev/effects/plasma",
  "files": [
    {
      "path": "registry/hyperblur/plasma.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/plasma.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface PlasmaProps extends React.HTMLAttributes<HTMLDivElement> {\n  colors?: string[];\n  speed?: number;\n  scale?: number;\n  opacity?: number;\n  seed?: number;\n}\n\nconst css = `\n.hb-pl { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }\n.hb-pl-c { display: block; width: 100%; height: 100%; }\n`;\n\nfunction rnd(seed: number, i: number) {\n  let x = (Math.imul(i + 1, 0x9e3779b1) + Math.imul(seed | 0, 0x85ebca6b)) >>> 0;\n  x = Math.imul((x >>> 16) ^ x, 0x45d9f3b) >>> 0;\n  x = Math.imul((x >>> 16) ^ x, 0x45d9f3b) >>> 0;\n  x = ((x >>> 16) ^ x) >>> 0;\n  return x / 4294967296;\n}\n\nexport function Plasma({\n  colors = [\"#0b0e14\", \"#2f7bff\", \"#19c3f5\"],\n  speed = 1,\n  scale = 1,\n  opacity = 0.8,\n  seed = 1,\n  className = \"\",\n  style,\n  ...rest\n}: PlasmaProps) {\n  const wrapRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n  const key = colors.join(\"|\");\n\n  React.useEffect(() => {\n    const wrap = wrapRef.current;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    const buf = document.createElement(\"canvas\");\n    const bctx = buf.getContext(\"2d\");\n    const probe = document.createElement(\"canvas\");\n    probe.width = 1;\n    probe.height = 1;\n    const pctx = probe.getContext(\"2d\", { willReadFrequently: true });\n    if (!wrap || !canvas || !ctx || !bctx || !pctx) return;\n\n    const stops = key.split(\"|\").map((c) => {\n      pctx.clearRect(0, 0, 1, 1);\n      pctx.fillStyle = \"#000\";\n      pctx.fillStyle = c;\n      pctx.fillRect(0, 0, 1, 1);\n      const d = pctx.getImageData(0, 0, 1, 1).data;\n      return [d[0], d[1], d[2]];\n    });\n    const last = stops.length - 1;\n    const lut = new Uint8ClampedArray(256 * 3);\n    for (let i = 0; i < 256; i++) {\n      const p = (i / 255) * last;\n      const a = Math.min(last, Math.floor(p));\n      const b = Math.min(last, a + 1);\n      const f = p - a;\n      for (let k = 0; k < 3; k++) lut[i * 3 + k] = stops[a][k] + (stops[b][k] - stops[a][k]) * f;\n    }\n\n    const TAU = Math.PI * 2;\n    const r = (i: number) => rnd(seed, i);\n    const u = 1 / (70 * Math.max(scale, 0.05));\n    const ph0 = r(1) * TAU;\n    const ph1 = r(2) * TAU;\n    const ph2 = r(3) * TAU;\n    const ph3 = r(4) * TAU;\n    const kx = u * (0.9 + r(5) * 0.3);\n    const ky = u * (1.1 + r(6) * 0.3);\n    const kc = u * (0.7 + r(7) * 0.3);\n    const kr = u * (1 + r(8) * 0.3);\n    const s0 = 0.7 + r(9) * 0.4;\n    const s1 = 0.9 + r(10) * 0.4;\n    const s2 = 0.5 + r(11) * 0.4;\n    const s3 = 0.8 + r(12) * 0.4;\n    const fx = 0.3 + r(13) * 0.4;\n    const fy = 0.3 + r(14) * 0.4;\n    const rate = 0.8 * speed;\n\n    let bw = 0;\n    let bh = 0;\n    let cellX = 1;\n    let cellY = 1;\n    let img: ImageData | null = null;\n    let colA = new Float32Array(0);\n    let colS = new Float32Array(0);\n    let colC = new Float32Array(0);\n    let rowB = new Float32Array(0);\n    let rowS = new Float32Array(0);\n    let rowC = new Float32Array(0);\n    let dist = new Float32Array(0);\n    let time = 20 + r(15) * 40;\n\n    function draw(t: number) {\n      if (!img || !ctx || !bctx || !canvas) return;\n      const tt = t * rate;\n      for (let x = 0; x < bw; x++) {\n        const X = x * cellX;\n        colA[x] = Math.sin(X * kx + ph0 + tt * s0);\n        const p = X * kc + tt * s2 + ph2;\n        colS[x] = Math.sin(p);\n        colC[x] = Math.cos(p);\n      }\n      for (let y = 0; y < bh; y++) {\n        const Y = y * cellY;\n        rowB[y] = Math.sin(Y * ky + ph1 + tt * s1);\n        const q = Y * kc;\n        rowS[y] = Math.sin(q);\n        rowC[y] = Math.cos(q);\n      }\n      const data = img.data;\n      const rt = ph3 - tt * s3;\n      let i = 0;\n      for (let y = 0; y < bh; y++) {\n        const b = rowB[y];\n        const sq = rowS[y];\n        const cq = rowC[y];\n        for (let x = 0; x < bw; x++, i++) {\n          const v = colA[x] + b + (colS[x] * cq + colC[x] * sq) + Math.sin(dist[i] * kr + rt);\n          let j = ((v * 0.2 + 0.5) * 255) | 0;\n          if (j < 0) j = 0;\n          else if (j > 255) j = 255;\n          const o = i * 4;\n          const l = j * 3;\n          data[o] = lut[l];\n          data[o + 1] = lut[l + 1];\n          data[o + 2] = lut[l + 2];\n          data[o + 3] = 255;\n        }\n      }\n      bctx.putImageData(img, 0, 0);\n      ctx.clearRect(0, 0, canvas.width, canvas.height);\n      ctx.globalAlpha = Math.min(1, Math.max(0, opacity));\n      ctx.imageSmoothingEnabled = true;\n      ctx.imageSmoothingQuality = \"high\";\n      ctx.drawImage(buf, 0, 0, bw, bh, 0, 0, canvas.width, canvas.height);\n    }\n\n    function resize() {\n      if (!wrap || !canvas || !bctx) return;\n      const w = wrap.clientWidth;\n      const h = wrap.clientHeight;\n      if (!w || !h) return;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      canvas.width = Math.max(1, Math.round(w * dpr));\n      canvas.height = Math.max(1, Math.round(h * dpr));\n      let cell = 4;\n      if ((w * h) / (cell * cell) > 90000) cell = Math.sqrt((w * h) / 90000);\n      bw = Math.max(2, Math.round(w / cell));\n      bh = Math.max(2, Math.round(h / cell));\n      buf.width = bw;\n      buf.height = bh;\n      img = bctx.createImageData(bw, bh);\n      cellX = w / bw;\n      cellY = h / bh;\n      colA = new Float32Array(bw);\n      colS = new Float32Array(bw);\n      colC = new Float32Array(bw);\n      rowB = new Float32Array(bh);\n      rowS = new Float32Array(bh);\n      rowC = new Float32Array(bh);\n      dist = new Float32Array(bw * bh);\n      const cx = fx * w;\n      const cy = fy * h;\n      let i = 0;\n      for (let y = 0; y < bh; y++) {\n        const dy = y * cellY - cy;\n        for (let x = 0; x < bw; x++, i++) {\n          const dx = x * cellX - cx;\n          dist[i] = Math.sqrt(dx * dx + dy * dy);\n        }\n      }\n      draw(time);\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 frame(now: number) {\n      const dt = Math.min(0.05, (now - prev) / 1000);\n      prev = now;\n      time += dt;\n      draw(time);\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    }\n\n    function onMotion() {\n      sync();\n      draw(time);\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(wrap);\n    io.observe(wrap);\n    document.addEventListener(\"visibilitychange\", sync);\n    motion.addEventListener(\"change\", onMotion);\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\", onMotion);\n    };\n  }, [key, speed, scale, opacity, seed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-pl ${className}`} style={style} aria-hidden=\"true\" {...rest}>\n      <style href=\"hb-plasma\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-pl-c\" />\n    </div>\n  );\n}\n"
    }
  ]
}