{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "lava-lamp",
  "type": "registry:component",
  "title": "Lava Lamp",
  "description": "A lava lamp with glass and warm light: the wax rises, stretches, merges with other blobs and splits apart. Your cursor warms it.",
  "author": "Marcelo Treviño Velázquez <https://furin.dev>",
  "dependencies": [],
  "categories": [
    "Backgrounds"
  ],
  "docs": "Installed in components/furin/lava-lamp/. Docs, live controls and props table: https://furin.dev/components/lava-lamp/",
  "files": [
    {
      "path": "registry/lava-lamp/LavaLamp.tsx",
      "content": "\"use client\";\nimport { useEffect, useRef } from \"react\";\nimport type { CSSProperties, ReactNode } from \"react\";\nimport \"./LavaLamp.css\";\n\nexport interface LavaLampProps {\n  /** Color de la cera. */\n  waxColor?: string;\n  /** Color del líquido dentro del vidrio. */\n  liquidColor?: string;\n  /** Velocidad de las burbujas. */\n  speed?: number;\n  /** Intensidad de la luz cálida alrededor de la lámpara. */\n  glow?: number;\n  /** El cursor calienta la cera y atrae una burbuja. */\n  interactive?: boolean;\n  /** Contenido opcional encima del fondo. */\n  children?: ReactNode;\n  /** Clase extra para el contenedor. */\n  className?: string;\n  /** Estilos extra para el contenedor. */\n  style?: CSSProperties;\n}\n\nlet colorCtx: CanvasRenderingContext2D | null = null;\nfunction rgb(color: string): [number, number, number] {\n  colorCtx ??= document.createElement(\"canvas\").getContext(\"2d\", { willReadFrequently: true })!;\n  colorCtx.clearRect(0, 0, 1, 1);\n  colorCtx.fillStyle = \"#000\";\n  colorCtx.fillStyle = color;\n  colorCtx.fillRect(0, 0, 1, 1);\n  const d = colorCtx.getImageData(0, 0, 1, 1).data;\n  return [d[0] / 255, d[1] / 255, d[2] / 255];\n}\n\nconst VS = `attribute vec2 a; void main(){ gl_Position = vec4(a, 0.0, 1.0); }`;\n\nconst FS = `precision highp float;\nuniform vec2 uRes;\nuniform float uTime, uGlow;\nuniform vec3 uWax, uLiquid;\nuniform vec3 uMouse;\n\nfloat hash(float n){ return fract(sin(n * 12.9898 + 1.7) * 43758.5453); }\nfloat hwV(float y){\n  float t = clamp((y + 0.30) / 0.64, 0.0, 1.0);\n  return mix(0.205, 0.07, pow(t, 0.9)) + 0.014 * sin(t * 3.14159);\n}\n\n// campo de metaballs (Wyvill): x = campo, yz = gradiente\nvec3 ball(vec2 q, vec2 c, float r){\n  vec2 d = q - c;\n  d.y *= 0.84;\n  float R = r * 2.1;\n  float k = dot(d, d) / (R * R);\n  if (k >= 1.0) return vec3(0.0);\n  float m = 1.0 - k;\n  return vec3(m * m * m, -6.0 * m * m * vec2(d.x, d.y * 0.84) / (R * R));\n}\n\nvec3 field(vec2 q, float t){\n  vec3 f = vec3(0.0);\n  // charco de cera en el fondo, calentado por el foco\n  for (int i = 0; i < 4; i++) {\n    float fi = float(i);\n    vec2 c = vec2(-0.115 + fi * 0.077, -0.268 + 0.010 * sin(t * 0.5 + fi * 1.9));\n    f += ball(q, c, 0.062 + 0.007 * sin(t * 0.37 + fi));\n  }\n  // burbujas que suben, se enfrían y bajan\n  for (int i = 0; i < 8; i++) {\n    float fi = float(i);\n    float h1 = hash(fi), h2 = hash(fi + 11.0), h3 = hash(fi + 23.0), h4 = hash(fi + 37.0);\n    float ph = fract(t * (0.055 + 0.04 * h1) + h2);\n    float w = 0.5 - 0.5 * cos(6.2832 * ph);\n    w = smoothstep(0.0, 1.0, w);\n    float y = mix(-0.255, 0.305, w);\n    float x = hwV(y) * 0.6 * sin(6.2832 * (t * (0.02 + 0.02 * h3) + h4));\n    float r = (0.036 + 0.026 * h3) * (1.12 - 0.42 * w);\n    f += ball(q, vec2(x, y), r);\n  }\n  if (uMouse.z > 0.001) f += ball(q, uMouse.xy, 0.05 * uMouse.z);\n  return f;\n}\n\nvec3 metal(float u, float v, vec3 tint){\n  float c = sqrt(max(1.0 - u * u, 0.0));\n  vec3 n = vec3(u, 0.0, c);\n  vec3 L = normalize(vec3(-0.55, 0.35, 0.8));\n  float l = 0.22 + 0.78 * max(dot(n, L), 0.0);\n  float spec = pow(max(dot(n, normalize(L + vec3(0.0, 0.0, 1.0))), 0.0), 26.0);\n  vec3 col = tint * l;\n  col += tint * vec3(1.0, 0.5, 0.2) * 0.55 * smoothstep(0.7, 0.0, v) * c;\n  col += vec3(1.0, 0.84, 0.6) * spec * 0.85;\n  col *= 0.8 + 0.2 * smoothstep(0.0, 0.25, 1.0 - abs(u));\n  return col;\n}\n\nvoid main(){\n  vec2 p = (gl_FragCoord.xy - 0.5 * uRes) / uRes.y;\n  float px = 1.0 / uRes.y;\n  vec3 warm = mix(uLiquid, uWax, 0.45);\n\n  // ---------- fondo: penumbra con la luz de la lámpara ----------\n  vec3 col = vec3(0.030, 0.022, 0.026);\n  float halo = exp(-length(vec2(p.x * 1.15, (p.y + 0.02) * 0.62)) * 4.4);\n  col += warm * halo * 0.20 * uGlow;\n  col += uWax * exp(-length(vec2(p.x * 0.9, (p.y + 0.12) * 0.55)) * 3.2) * 0.05 * uGlow;\n  // piso: un brillo suave bajo la base\n  float floorY = -0.462;\n  float onFloor = smoothstep(floorY + px, floorY - px, p.y);\n  col = mix(col, vec3(0.018, 0.013, 0.016) + warm * 0.10 * uGlow * exp(-abs(p.x) * 4.5) * exp(-(floorY - p.y) * 9.0), onFloor);\n  col += uWax * 0.12 * uGlow * exp(-pow(p.x / 0.22, 2.0)) * exp(-pow((p.y - floorY + 0.012) / 0.02, 2.0)) * onFloor;\n\n  // ---------- base y tapa de metal ----------\n  float tb = clamp((p.y + 0.462) / 0.162, 0.0, 1.0);\n  float hwB = mix(0.262, 0.214, smoothstep(0.0, 1.0, tb));\n  float mB = (1.0 - smoothstep(-px, px, abs(p.x) - hwB)) * smoothstep(-0.462 - px, -0.462 + px, p.y) * (1.0 - smoothstep(-0.30 - px, -0.30 + px, p.y));\n  vec3 bronze = vec3(0.58, 0.36, 0.19);\n  vec3 baseCol = metal(clamp(p.x / hwB, -1.0, 1.0), tb, bronze * 0.62);\n  baseCol += vec3(1.0, 0.8, 0.5) * 0.5 * exp(-pow((p.y + 0.318) / 0.006, 2.0));\n  baseCol *= 1.0 - 0.45 * smoothstep(0.06, 0.0, tb);\n  col = mix(col, baseCol, mB);\n\n  float tc = clamp((p.y - 0.335) / 0.115, 0.0, 1.0);\n  float hwC = 0.098 * sqrt(max(1.0 - tc * tc, 0.0)) + 0.0001;\n  float mC = (1.0 - smoothstep(-px, px, abs(p.x) - hwC)) * smoothstep(0.335 - px, 0.335 + px, p.y) * (1.0 - smoothstep(0.45, 0.452, p.y));\n  vec3 capCol = metal(clamp(p.x / max(hwC, 0.001), -1.0, 1.0), 1.0 - tc, bronze * 0.55);\n  col = mix(col, capCol, mC);\n\n  // ---------- vidrio con líquido y cera ----------\n  float hv = hwV(p.y);\n  float mV = (1.0 - smoothstep(-px, px, abs(p.x) - hv)) * smoothstep(-0.30 - px, -0.30 + px, p.y) * (1.0 - smoothstep(0.34 - px, 0.34 + px, p.y));\n  if (mV > 0.001) {\n    float u = p.x / hv;\n    float t = clamp((p.y + 0.30) / 0.64, 0.0, 1.0);\n    // luz del foco: sube desde abajo y se pierde arriba\n    float bulb = exp(-t * 3.1) * (1.0 - 0.55 * u * u);\n    vec3 liquid = mix(uLiquid * uLiquid * 0.55, uLiquid * 1.25, bulb);\n    liquid += uWax * uWax * 0.45 * exp(-t * 7.5);\n\n    vec3 f = field(p, uTime);\n    float T = 0.5;\n    float g = length(f.yz) + 1e-4;\n    float edge = clamp((f.x - T) / (g * px * 1.3), -1.0, 1.0) * 0.5 + 0.5;\n    float depth = clamp((f.x - T) / 0.55, 0.0, 1.0);\n    // brillo de cera que se filtra al líquido\n    liquid += uWax * 0.22 * smoothstep(0.0, T, f.x) * (0.4 + bulb);\n\n    float nz = pow(depth, 0.55);\n    vec2 nxy = normalize(-f.yz) * sqrt(max(1.0 - nz * nz, 0.0));\n    vec3 n = vec3(nxy, nz);\n    vec3 bulbPos = vec3(0.0, -0.37, 0.28);\n    vec3 Ld = normalize(bulbPos - vec3(p, 0.0));\n    float diff = max(dot(n, Ld), 0.0);\n    float glowY = exp(-t * 2.4);\n    vec3 hot = mix(uWax, vec3(1.0, 0.72, 0.30), 0.42);\n    vec3 wax = mix(uWax * uWax * 0.62, hot, pow(depth, 1.0)) * (0.40 + 0.70 * diff + 0.75 * glowY) * mix(1.1, 0.72, t);\n    wax += hot * 0.30 * pow(depth, 2.0) * (0.35 + glowY);\n    vec3 V = vec3(0.0, 0.0, 1.0);\n    float s1 = pow(max(dot(n, normalize(Ld + V)), 0.0), 36.0);\n    float s2 = pow(max(dot(n, normalize(normalize(vec3(-0.45, 0.65, 0.75)) + V)), 0.0), 70.0);\n    wax += vec3(1.0, 0.92, 0.75) * (s1 * 0.40 + s2 * 0.70);\n    wax += hot * pow(1.0 - nz, 3.0) * 0.35;\n\n    vec3 inner = mix(liquid, wax, edge);\n\n    // vidrio: borde grueso y oscuro, reflejos de ventana\n    float au = abs(u);\n    inner *= 1.0 - 0.58 * pow(au, 3.4);\n    float fadeY = smoothstep(0.04, 0.2, t) * (1.0 - smoothstep(0.72, 0.97, t));\n    float st1 = exp(-pow((u + 0.70) / 0.075, 2.0));\n    float st2 = exp(-pow((u + 0.52) / 0.03, 2.0));\n    float st3 = exp(-pow((u - 0.80) / 0.04, 2.0));\n    inner += vec3(1.0, 0.95, 0.9) * (st1 * 0.20 + st2 * 0.11 + st3 * 0.12) * fadeY;\n    inner += vec3(1.0, 0.8, 0.6) * smoothstep(0.90, 0.985, au) * (1.0 - smoothstep(0.985, 1.0, au)) * 0.22;\n    // reflejo curvo arriba\n    inner += vec3(1.0, 0.95, 0.9) * 0.07 * smoothstep(0.9, 0.99, t) * (1.0 - au);\n    col = mix(col, inner, mV);\n  }\n  // pequeño destello donde la base abraza el vidrio\n  col += vec3(1.0, 0.7, 0.4) * 0.10 * exp(-pow((p.y + 0.30) / 0.012, 2.0)) * (1.0 - smoothstep(0.0, 1.0, abs(p.x) / 0.22)) * mV;\n\n  col = col / (1.0 + max(col - 0.8, 0.0));\n  col = pow(col, vec3(0.95));\n  // viñeta suave\n  col *= 1.0 - 0.35 * dot(p * vec2(0.9, 1.1), p * vec2(0.9, 1.1));\n  col += (fract(sin(dot(gl_FragCoord.xy, vec2(12.9898, 78.233))) * 43758.5453) - 0.5) / 160.0;\n  gl_FragColor = vec4(col, 1.0);\n}`;\n\n/** Lámpara de lava con vidrio, base de bronce y cera que se funde: metaballs en un solo shader. */\nexport function LavaLamp({\n  waxColor = \"#FF6A1F\",\n  liquidColor = \"#8A1240\",\n  speed = 1,\n  glow = 1,\n  interactive = true,\n  children,\n  className = \"\",\n  style,\n}: LavaLampProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const live = useRef({ waxColor, liquidColor, speed, glow, interactive });\n  live.current = { waxColor, liquidColor, speed, glow, interactive };\n  const wakeRef = useRef<() => void>(() => {});\n\n  useEffect(() => {\n    const el = host.current;\n    if (!el) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const canvas = document.createElement(\"canvas\");\n    canvas.className = \"furin-lava-lamp__canvas\";\n    canvas.setAttribute(\"aria-hidden\", \"true\");\n    el.insertBefore(canvas, el.firstChild);\n    const gl = canvas.getContext(\"webgl\", { antialias: false, alpha: false });\n    if (!gl) {\n      canvas.remove();\n      return;\n    }\n    const compile = (type: number, src: string) => {\n      const s = gl.createShader(type)!;\n      gl.shaderSource(s, src);\n      gl.compileShader(s);\n      if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) console.error(\"[lava-lamp] shader:\", gl.getShaderInfoLog(s));\n      return s;\n    };\n    const prog = gl.createProgram()!;\n    gl.attachShader(prog, compile(gl.VERTEX_SHADER, VS));\n    gl.attachShader(prog, compile(gl.FRAGMENT_SHADER, FS));\n    gl.linkProgram(prog);\n    if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) console.error(\"[lava-lamp] link:\", gl.getProgramInfoLog(prog));\n    gl.useProgram(prog);\n    const quad = gl.createBuffer()!;\n    gl.bindBuffer(gl.ARRAY_BUFFER, quad);\n    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW);\n    const loc = gl.getAttribLocation(prog, \"a\");\n    gl.enableVertexAttribArray(loc);\n    gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0);\n    const U = {\n      res: gl.getUniformLocation(prog, \"uRes\"),\n      time: gl.getUniformLocation(prog, \"uTime\"),\n      glow: gl.getUniformLocation(prog, \"uGlow\"),\n      wax: gl.getUniformLocation(prog, \"uWax\"),\n      liquid: gl.getUniformLocation(prog, \"uLiquid\"),\n      mouse: gl.getUniformLocation(prog, \"uMouse\"),\n    };\n\n    let W = 1, H = 1, time = reduce ? 14 : 3 + Math.random() * 4, last = 0, raf = 0, visible = true;\n    let waxKey = \"\", liqKey = \"\";\n    let wax: [number, number, number] = [1, 0.5, 0.2];\n    let liq: [number, number, number] = [0.5, 0.07, 0.25];\n    const m = { tx: 0, ty: 0, x: 0, y: 0, s: 0, ts: 0 };\n\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      W = el.clientWidth || 300;\n      H = el.clientHeight || 300;\n      canvas.width = Math.max(2, Math.round(W * dpr));\n      canvas.height = Math.max(2, Math.round(H * dpr));\n      gl.viewport(0, 0, canvas.width, canvas.height);\n      draw();\n    };\n\n    const draw = () => {\n      if (waxKey !== live.current.waxColor) {\n        waxKey = live.current.waxColor;\n        wax = rgb(waxKey);\n      }\n      if (liqKey !== live.current.liquidColor) {\n        liqKey = live.current.liquidColor;\n        liq = rgb(liqKey);\n      }\n      gl.uniform2f(U.res, canvas.width, canvas.height);\n      gl.uniform1f(U.time, time);\n      gl.uniform1f(U.glow, live.current.glow);\n      gl.uniform3f(U.wax, wax[0], wax[1], wax[2]);\n      gl.uniform3f(U.liquid, liq[0], liq[1], liq[2]);\n      gl.uniform3f(U.mouse, m.x, m.y, m.s);\n      gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);\n    };\n\n    const frame = (now: number) => {\n      raf = 0;\n      const dt = Math.min(0.05, Math.max(0.001, (now - last) / 1000));\n      last = now;\n      time += dt * live.current.speed;\n      // la burbuja del cursor se desliza con suavidad hasta donde está la mano\n      const k = 1 - Math.exp(-dt * 7);\n      m.x += (m.tx - m.x) * k;\n      m.y += (m.ty - m.y) * k;\n      m.s += (m.ts - m.s) * (1 - Math.exp(-dt * 4));\n      draw();\n      if (visible) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => {\n      if (raf || !visible || reduce) return;\n      last = performance.now();\n      raf = requestAnimationFrame(frame);\n    };\n    wakeRef.current = () => {\n      draw();\n      wake();\n    };\n\n    const hw = (y: number) => {\n      const t = Math.min(1, Math.max(0, (y + 0.3) / 0.64));\n      return 0.205 + (0.07 - 0.205) * Math.pow(t, 0.9) + 0.014 * Math.sin(t * Math.PI);\n    };\n    const onMove = (e: PointerEvent) => {\n      if (!live.current.interactive) return;\n      const r = el.getBoundingClientRect();\n      const px = (e.clientX - r.left - r.width / 2) / r.height;\n      const py = (r.height / 2 - (e.clientY - r.top)) / r.height;\n      // la cera busca la mano, pero dentro del vidrio\n      const y = Math.min(0.29, Math.max(-0.24, py));\n      const lim = hw(y) - 0.05;\n      m.tx = Math.min(lim, Math.max(-lim, px));\n      m.ty = y;\n      if (m.ts === 0) {\n        m.x = m.tx;\n        m.y = m.ty;\n      }\n      m.ts = 1;\n    };\n    const onLeave = () => {\n      m.ts = 0;\n    };\n    el.addEventListener(\"pointermove\", onMove);\n    el.addEventListener(\"pointerdown\", onMove);\n    el.addEventListener(\"pointerleave\", onLeave);\n    el.addEventListener(\"pointercancel\", onLeave);\n\n    const io = new IntersectionObserver(([en]) => {\n      visible = en.isIntersecting;\n      if (visible) wake();\n      else if (raf) {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n    });\n    io.observe(el);\n    const ro = new ResizeObserver(resize);\n    ro.observe(el);\n    resize();\n    wake();\n\n    return () => {\n      cancelAnimationFrame(raf);\n      io.disconnect();\n      ro.disconnect();\n      el.removeEventListener(\"pointermove\", onMove);\n      el.removeEventListener(\"pointerdown\", onMove);\n      el.removeEventListener(\"pointerleave\", onLeave);\n      el.removeEventListener(\"pointercancel\", onLeave);\n      wakeRef.current = () => {};\n      gl.deleteBuffer(quad);\n      gl.deleteProgram(prog);\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n      canvas.remove();\n    };\n  }, []);\n\n  useEffect(() => {\n    wakeRef.current();\n  }, [waxColor, liquidColor, speed, glow]);\n\n  return (\n    <div ref={host} className={`furin-lava-lamp ${className}`} style={style}>\n      {children ? <div className=\"furin-lava-lamp__contenido\">{children}</div> : null}\n    </div>\n  );\n}\n\nexport default LavaLamp;\n",
      "type": "registry:component",
      "target": "components/furin/lava-lamp/LavaLamp.tsx"
    },
    {
      "path": "registry/lava-lamp/LavaLamp.css",
      "content": ".furin-lava-lamp {\n  position: relative;\n  width: 100%;\n  height: 100%;\n  min-height: 240px;\n  overflow: hidden;\n  background: #0a0809;\n  touch-action: pan-y;\n}\n.furin-lava-lamp__canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }\n.furin-lava-lamp__contenido { position: relative; z-index: 1; width: 100%; height: 100%; display: grid; place-items: center; pointer-events: none; }\n.furin-lava-lamp__contenido > * { pointer-events: auto; }\n",
      "type": "registry:file",
      "target": "components/furin/lava-lamp/LavaLamp.css"
    }
  ]
}
