{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"plasma","type":"registry:ui","title":"Plasma","description":"A flowing plasma color field that the pointer warps like a lens.","dependencies":["motion"],"registryDependencies":["https://ui.mekaigames.com/r/theme.json"],"files":[{"path":"components/ui/plasma.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useReducedMotion } from \"motion/react\";\nimport { useEffect, useRef } from \"react\";\n\ntype PlasmaProps = {\n  /** Three hex colors the field cycles through. */\n  colors?: [string, string, string];\n  /** Flow speed multiplier. */\n  speed?: number;\n  /** Size of the plasma cells. Larger values make broader shapes. */\n  scale?: number;\n  /** Overall opacity, from 0 to 1. */\n  opacity?: number;\n  /** Strength of the lens that follows the pointer, from 0 to 1. */\n  lens?: number;\n  /** Render resolution, from 0.25 to 1. Lower is cheaper. */\n  quality?: number;\n  className?: string;\n};\n\nconst VERTEX = `\nattribute vec2 aPos;\nvoid main() { gl_Position = vec4(aPos, 0.0, 1.0); }\n`;\n\nconst FRAGMENT = `\n#ifdef GL_FRAGMENT_PRECISION_HIGH\nprecision highp float;\n#else\nprecision mediump float;\n#endif\nuniform vec2 uRes;\nuniform float uTime;\nuniform vec2 uPointer;\nuniform float uHover;\nuniform vec3 uC0;\nuniform vec3 uC1;\nuniform vec3 uC2;\nuniform float uScale;\nuniform float uLens;\n\n// Loops through the three colors, so the field never shows a seam.\nvec3 palette(float x) {\n  x = fract(x) * 3.0;\n  if (x < 1.0) return mix(uC0, uC1, smoothstep(0.0, 1.0, x));\n  if (x < 2.0) return mix(uC1, uC2, smoothstep(0.0, 1.0, x - 1.0));\n  return mix(uC2, uC0, smoothstep(0.0, 1.0, x - 2.0));\n}\n\nvoid main() {\n  float unit = min(uRes.x, uRes.y);\n  vec2 q = (gl_FragCoord.xy - 0.5 * uRes) / unit;\n  vec2 m = (uPointer * uRes - 0.5 * uRes) / unit;\n\n  // Lens: sample closer to the pointer, which magnifies the field under it.\n  vec2 d = q - m;\n  float r2 = dot(d, d);\n  float lens = uLens * uHover * exp(-r2 / 0.07);\n  q = m + d * (1.0 - 0.55 * lens);\n\n  vec2 p = q * (6.0 / uScale);\n  float t = uTime;\n  float v = sin(p.x + t);\n  v += sin((p.y + t) * 0.8);\n  v += sin((p.x + p.y + t * 0.7) * 0.7);\n  vec2 c = p + vec2(sin(t * 0.33) * 2.2, cos(t * 0.5) * 2.2);\n  v += sin(sqrt(dot(c, c) + 1.0) * 1.3 + t);\n  v *= 0.25;\n\n  vec3 col = palette(v * 0.9 + t * 0.03);\n  // Darker troughs give the field depth instead of a flat gradient.\n  col *= 0.2 + 0.8 * smoothstep(-0.7, 0.9, sin(v * 6.0 + t * 0.6));\n  // A faint bright rim around the lens.\n  float ring = (sqrt(r2) - 0.26) * 22.0;\n  col += uLens * uHover * 0.18 * exp(-ring * ring) * mix(uC1, vec3(1.0), 0.5);\n\n  gl_FragColor = vec4(col, 1.0);\n}\n`;\n\nfunction hexToVec3(hex: string): [number, number, number] {\n  let h = hex.replace(\"#\", \"\");\n  if (h.length === 3) h = h.replace(/./g, (c) => c + c);\n  const n = parseInt(h, 16) || 0;\n  return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255];\n}\n\nfunction createProgram(gl: WebGLRenderingContext, fragment: string) {\n  const program = gl.createProgram();\n  const shaders: WebGLShader[] = [];\n  for (const [type, source] of [\n    [gl.VERTEX_SHADER, VERTEX],\n    [gl.FRAGMENT_SHADER, fragment],\n  ] as const) {\n    const shader = gl.createShader(type);\n    if (!shader || !program) return null;\n    gl.shaderSource(shader, source);\n    gl.compileShader(shader);\n    if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {\n      console.warn(gl.getShaderInfoLog(shader));\n      return null;\n    }\n    gl.attachShader(program, shader);\n    shaders.push(shader);\n  }\n  if (!program) return null;\n  gl.linkProgram(program);\n  for (const shader of shaders) gl.deleteShader(shader);\n  return gl.getProgramParameter(program, gl.LINK_STATUS) ? program : null;\n}\n\n/** Releases the GPU context once the canvas has really left the page (not on a Strict Mode re-run). */\nfunction releaseLater(gl: WebGLRenderingContext, canvas: HTMLCanvasElement) {\n  window.setTimeout(() => {\n    if (!canvas.isConnected) gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n  }, 0);\n}\n\n/** A flowing plasma color field. The pointer warps it like a lens. */\nexport function Plasma({\n  colors = [\"#ff784f\", \"#ffc24a\", \"#e2552b\"],\n  speed = 1,\n  scale = 1,\n  opacity = 1,\n  lens = 0.7,\n  quality = 1,\n  className = \"\",\n}: PlasmaProps) {\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const reduce = useReducedMotion();\n  const [c0 = \"#ff784f\", c1 = \"#ffc24a\", c2 = \"#e2552b\"] = colors;\n\n  useEffect(() => {\n    const canvas = canvasRef.current;\n    if (!canvas) return;\n    const fallback = `linear-gradient(120deg, ${c0}, ${c1} 50%, ${c2})`;\n    const gl = canvas.getContext(\"webgl\", { alpha: false, antialias: false, depth: false, powerPreference: \"low-power\" });\n    const program = gl ? createProgram(gl, FRAGMENT) : null;\n    if (!gl || !program) {\n      canvas.style.background = fallback;\n      return;\n    }\n    canvas.style.background = \"\";\n\n    const buffer = gl.createBuffer();\n    gl.bindBuffer(gl.ARRAY_BUFFER, buffer);\n    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), 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    gl.useProgram(program);\n\n    const uniform = (name: string) => gl.getUniformLocation(program, name);\n    const uRes = uniform(\"uRes\");\n    const uTime = uniform(\"uTime\");\n    const uPointer = uniform(\"uPointer\");\n    const uHover = uniform(\"uHover\");\n    gl.uniform3f(uniform(\"uC0\"), ...hexToVec3(c0));\n    gl.uniform3f(uniform(\"uC1\"), ...hexToVec3(c1));\n    gl.uniform3f(uniform(\"uC2\"), ...hexToVec3(c2));\n    gl.uniform1f(uniform(\"uScale\"), Math.max(0.1, scale));\n    gl.uniform1f(uniform(\"uLens\"), Math.min(1, Math.max(0, lens)));\n\n    const pointer = { x: 0.5, y: 0.5, tx: 0.5, ty: 0.5, hover: 0, target: 0 };\n    let time = 10;\n    let last = 0;\n    let frame = 0;\n    let visible = true;\n    let lost = false;\n\n    const render = (t: number) => {\n      if (lost) return;\n      const dt = last ? Math.min(50, t - last) : 16;\n      last = t;\n      if (!reduce) {\n        time += (dt / 1000) * speed;\n        const k = Math.min(1, dt * 0.008);\n        pointer.x += (pointer.tx - pointer.x) * k;\n        pointer.y += (pointer.ty - pointer.y) * k;\n        pointer.hover += (pointer.target - pointer.hover) * Math.min(1, dt * 0.004);\n      }\n      gl.uniform1f(uTime, time % 3600);\n      gl.uniform2f(uPointer, pointer.x, pointer.y);\n      gl.uniform1f(uHover, pointer.hover);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n      if (!reduce && visible) frame = requestAnimationFrame(render);\n      else frame = 0;\n    };\n\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2) * Math.min(1, Math.max(0.25, quality));\n      canvas.width = Math.max(1, Math.round(canvas.clientWidth * dpr));\n      canvas.height = Math.max(1, Math.round(canvas.clientHeight * dpr));\n      gl.viewport(0, 0, canvas.width, canvas.height);\n      gl.uniform2f(uRes, canvas.width, canvas.height);\n      if (reduce) render(0);\n    };\n\n    const onMove = (e: PointerEvent) => {\n      const rect = canvas.getBoundingClientRect();\n      const x = (e.clientX - rect.left) / rect.width;\n      const y = 1 - (e.clientY - rect.top) / rect.height;\n      const inside = x >= 0 && x <= 1 && y >= 0 && y <= 1;\n      if (inside) {\n        pointer.tx = x;\n        pointer.ty = y;\n        // Jump the lens in place when entering, instead of sliding in from the last spot.\n        if (pointer.hover < 0.05) {\n          pointer.x = x;\n          pointer.y = y;\n        }\n      }\n      pointer.target = inside ? 1 : 0;\n    };\n    const onLost = () => {\n      lost = true;\n      cancelAnimationFrame(frame);\n      canvas.style.background = fallback;\n    };\n\n    resize();\n    const observer = new ResizeObserver(resize);\n    observer.observe(canvas);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible && !reduce && !frame && !lost) {\n        last = 0;\n        frame = requestAnimationFrame(render);\n      }\n    });\n    io.observe(canvas);\n    canvas.addEventListener(\"webglcontextlost\", onLost);\n    if (!reduce) {\n      window.addEventListener(\"pointermove\", onMove, { passive: true });\n      frame = requestAnimationFrame(render);\n    }\n\n    return () => {\n      cancelAnimationFrame(frame);\n      observer.disconnect();\n      io.disconnect();\n      window.removeEventListener(\"pointermove\", onMove);\n      canvas.removeEventListener(\"webglcontextlost\", onLost);\n      gl.deleteBuffer(buffer);\n      gl.deleteProgram(program);\n      releaseLater(gl, canvas);\n    };\n  }, [c0, c1, c2, speed, scale, lens, quality, reduce]);\n\n  return (\n    <canvas\n      ref={canvasRef}\n      className={`pointer-events-none absolute inset-0 block size-full ${className}`}\n      style={{ opacity: Math.min(1, Math.max(0, opacity)) }}\n      aria-hidden=\"true\"\n    />\n  );\n}\n"}]}