{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "smoke-field",
  "type": "registry:component",
  "title": "Smoke Field",
  "description": "Volumetric smoke with studio lighting that rises endlessly; your cursor parts and swirls it, and the smoke fills the gap back in.",
  "author": "Marcelo Treviño Velázquez <https://furin.dev>",
  "dependencies": [],
  "categories": [
    "Backgrounds"
  ],
  "docs": "Installed in components/furin/smoke-field/. Docs, live controls and props table: https://furin.dev/components/smoke-field/",
  "files": [
    {
      "path": "registry/smoke-field/SmokeField.tsx",
      "content": "\"use client\";\nimport { useEffect, useRef } from \"react\";\nimport type { CSSProperties, ReactNode } from \"react\";\nimport \"./SmokeField.css\";\n\nexport interface SmokeFieldProps {\n  /** Color del humo iluminado. */\n  color?: string;\n  /** Color de la luz de contraluz desde abajo. */\n  glowColor?: string;\n  /** Qué tan espeso es el humo. */\n  density?: number;\n  /** Velocidad con la que sube. */\n  speed?: number;\n  /** El cursor aparta y arremolina el humo. */\n  interactive?: boolean;\n  /** Contenido opcional encima del humo. */\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, uDensity;\nuniform sampler2D uDisp;\nuniform vec3 uColor, uGlow;\n\nfloat hash(vec2 p){ p = fract(p * vec2(123.34, 456.21)); p += dot(p, p + 45.32); return fract(p.x * p.y); }\nfloat noise(vec2 p){\n  vec2 i = floor(p), f = fract(p);\n  f = f * f * (3.0 - 2.0 * f);\n  float a = hash(i), b = hash(i + vec2(1.0, 0.0)), c = hash(i + vec2(0.0, 1.0)), d = hash(i + vec2(1.0, 1.0));\n  return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);\n}\nfloat fbm(vec2 p){\n  float s = 0.0, a = 0.5;\n  mat2 m = mat2(1.6, 1.2, -1.2, 1.6);\n  for (int i = 0; i < 5; i++) { s += a * noise(p); p = m * p; a *= 0.5; }\n  return s;\n}\n\nvoid main(){\n  vec2 uv = gl_FragCoord.xy / uRes;\n  float asp = uRes.x / uRes.y;\n  vec4 d = texture2D(uDisp, uv);\n  vec2 disp = (d.rg - 0.5) * 2.0 * 0.24;\n  float hole = d.b;\n  vec2 p = (uv - disp) * vec2(asp, 1.0) * 2.1;\n  float t = uTime;\n  p.y -= t * 0.14;\n  p.x += 0.12 * sin(t * 0.11 + uv.y * 2.0);\n  vec2 q = vec2(fbm(p + vec2(0.0, t * 0.04)), fbm(p + vec2(5.2, 1.3) - t * 0.03));\n  vec2 r = vec2(fbm(p + 3.0 * q + vec2(1.7, 9.2) + 0.11 * t), fbm(p + 3.0 * q + vec2(8.3, 2.8) + 0.09 * t));\n  vec2 pp = p + 3.0 * r;\n  float f = fbm(pp);\n  float e = 0.05;\n  float fx = fbm(pp + vec2(e, 0.0));\n  float fy = fbm(pp + vec2(0.0, e));\n  vec2 g = vec2(fx - f, fy - f) / e;\n\n  float dens = pow(smoothstep(0.24, 0.80, f), 1.05) * uDensity;\n  // más espeso abajo, más ralo arriba y hacia los lados\n  dens *= 0.62 + 0.8 * (1.0 - uv.y);\n  dens *= smoothstep(1.35, 0.25, length((uv - vec2(0.5, 0.45)) * vec2(1.0, 1.15)));\n  dens *= 1.0 - hole * 0.8;\n  dens = clamp(dens, 0.0, 1.0);\n\n  vec3 n = normalize(vec3(-g * 0.17, 1.0));\n  vec3 L = normalize(vec3(-0.45, 0.75, 0.55));\n  float lit = clamp(dot(n, L) * 0.5 + 0.5, 0.0, 1.0);\n  vec3 shade = vec3(0.075, 0.09, 0.14);\n  vec3 smoke = mix(shade, uColor, smoothstep(0.25, 0.95, lit));\n  // contraluz cálido desde abajo, solo en los bordes que miran hacia abajo\n  float under = clamp(-n.y * 1.2 + 0.25, 0.0, 1.0) * (1.0 - uv.y);\n  smoke += uGlow * under * 0.55 * dens;\n  smoke += uColor * pow(clamp(length(g) * 0.12, 0.0, 1.0), 2.0) * 0.30;\n\n  vec3 bg = vec3(0.022, 0.026, 0.038) + uGlow * 0.05 * exp(-uv.y * 3.2) * (1.0 - abs(uv.x - 0.5));\n  vec3 col = bg + smoke * dens * 1.12;\n  col *= 1.0 - 0.30 * dot(uv - 0.5, uv - 0.5) * 2.0;\n  col = col / (1.0 + max(col - 0.70, 0.0) * 1.2);\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\nconst GW = 64;\n\n/** Humo volumétrico: fbm con dominio deformado y luz de estudio; el cursor lo aparta y lo arremolina. */\nexport function SmokeField({\n  color = \"#BFD0FF\",\n  glowColor = \"#FF8A4A\",\n  density = 1,\n  speed = 1,\n  interactive = true,\n  children,\n  className = \"\",\n  style,\n}: SmokeFieldProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const live = useRef({ color, glowColor, density, speed, interactive });\n  live.current = { color, glowColor, density, speed, 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-smoke-field__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(\"[smoke-field] 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(\"[smoke-field] 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      density: gl.getUniformLocation(prog, \"uDensity\"),\n      color: gl.getUniformLocation(prog, \"uColor\"),\n      glow: gl.getUniformLocation(prog, \"uGlow\"),\n      disp: gl.getUniformLocation(prog, \"uDisp\"),\n    };\n    const tex = gl.createTexture()!;\n    gl.bindTexture(gl.TEXTURE_2D, tex);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n    gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);\n    gl.uniform1i(U.disp, 0);\n\n    let W = 1, H = 1, GH = 40;\n    let dx = new Float32Array(GW * GH);\n    let dy = new Float32Array(GW * GH);\n    let hl = new Float32Array(GW * GH);\n    let tmp = new Float32Array(GW * GH);\n    let bytes = new Uint8Array(GW * GH * 4);\n    let time = reduce ? 31 : 22 + Math.random() * 10;\n    let last = 0, raf = 0, visible = true;\n    let colKey = \"\", glowKey = \"\";\n    let col: [number, number, number] = [0.75, 0.82, 1];\n    let glo: [number, number, number] = [1, 0.54, 0.29];\n    const ptr = { x: 0, y: 0, has: false };\n\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      W = el.clientWidth || 300;\n      H = el.clientHeight || 300;\n      const scale = 0.78;\n      canvas.width = Math.max(2, Math.round(W * dpr * scale));\n      canvas.height = Math.max(2, Math.round(H * dpr * scale));\n      gl.viewport(0, 0, canvas.width, canvas.height);\n      GH = Math.max(16, Math.round((GW * H) / W));\n      dx = new Float32Array(GW * GH);\n      dy = new Float32Array(GW * GH);\n      hl = new Float32Array(GW * GH);\n      tmp = new Float32Array(GW * GH);\n      bytes = new Uint8Array(GW * GH * 4);\n      draw();\n    };\n\n    const upload = () => {\n      const MAX = 0.24;\n      for (let i = 0; i < GW * GH; i++) {\n        bytes[i * 4] = Math.round((Math.max(-1, Math.min(1, dx[i] / MAX)) * 0.5 + 0.5) * 255);\n        bytes[i * 4 + 1] = Math.round((Math.max(-1, Math.min(1, dy[i] / MAX)) * 0.5 + 0.5) * 255);\n        bytes[i * 4 + 2] = Math.round(Math.min(1, hl[i]) * 255);\n        bytes[i * 4 + 3] = 255;\n      }\n      gl.activeTexture(gl.TEXTURE0);\n      gl.bindTexture(gl.TEXTURE_2D, tex);\n      gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, GW, GH, 0, gl.RGBA, gl.UNSIGNED_BYTE, bytes);\n    };\n\n    const draw = () => {\n      if (colKey !== live.current.color) {\n        colKey = live.current.color;\n        col = rgb(colKey);\n      }\n      if (glowKey !== live.current.glowColor) {\n        glowKey = live.current.glowColor;\n        glo = rgb(glowKey);\n      }\n      upload();\n      gl.uniform2f(U.res, canvas.width, canvas.height);\n      gl.uniform1f(U.time, time);\n      gl.uniform1f(U.density, live.current.density);\n      gl.uniform3f(U.color, col[0], col[1], col[2]);\n      gl.uniform3f(U.glow, glo[0], glo[1], glo[2]);\n      gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);\n    };\n\n    // El humo desplazado vuelve a su sitio poco a poco, y se difunde para no verse a cuadros.\n    const relax = (dt: number) => {\n      const k = Math.exp(-dt / 1.9);\n      for (const arr of [dx, dy, hl]) {\n        for (let j = 0; j < GH; j++) {\n          for (let i = 0; i < GW; i++) {\n            const o = j * GW + i;\n            const l = arr[j * GW + Math.max(i - 1, 0)];\n            const r = arr[j * GW + Math.min(i + 1, GW - 1)];\n            const u = arr[Math.max(j - 1, 0) * GW + i];\n            const d = arr[Math.min(j + 1, GH - 1) * GW + i];\n            tmp[o] = (arr[o] * 0.6 + (l + r + u + d) * 0.1) * k;\n          }\n        }\n        arr.set(tmp);\n      }\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      relax(dt);\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 push = (cx: number, cy: number, mx: number, my: number) => {\n      // cx, cy en [0,1] (y hacia arriba); mx, my = movimiento en unidades de uv\n      const speed = Math.hypot(mx, my);\n      if (speed < 1e-5) return;\n      const sigma = 0.12; // en alturas de la caja\n      const sx = sigma * (H / W) * GW;\n      const sy = sigma * GH;\n      const reachX = Math.ceil(sx * 2.4);\n      const reachY = Math.ceil(sy * 2.4);\n      const ci = Math.floor(cx * GW);\n      const cj = Math.floor(cy * GH);\n      for (let j = Math.max(0, cj - reachY); j <= Math.min(GH - 1, cj + reachY); j++) {\n        for (let i = Math.max(0, ci - reachX); i <= Math.min(GW - 1, ci + reachX); i++) {\n          const rx = (i + 0.5) / GW - cx;\n          const ry = (j + 0.5) / GH - cy;\n          const nx = ((i + 0.5 - cx * GW) / sx) ** 2;\n          const ny = ((j + 0.5 - cy * GH) / sy) ** 2;\n          const w = Math.exp(-(nx + ny) * 0.5);\n          // el humo acompaña al cursor y se abre a ambos lados de su camino\n          const side = Math.sign(mx * ry - my * rx) || 0;\n          const o = j * GW + i;\n          dx[o] += w * (mx * 1.5 + -my * side * 1.1);\n          dy[o] += w * (my * 1.5 + mx * side * 1.1);\n          hl[o] += w * Math.min(speed * 18, 0.35);\n        }\n      }\n      for (let i = 0; i < dx.length; i++) {\n        if (dx[i] > 0.24) dx[i] = 0.24;\n        else if (dx[i] < -0.24) dx[i] = -0.24;\n        if (dy[i] > 0.24) dy[i] = 0.24;\n        else if (dy[i] < -0.24) dy[i] = -0.24;\n        if (hl[i] > 1) hl[i] = 1;\n      }\n    };\n\n    const onMove = (e: PointerEvent) => {\n      if (!live.current.interactive) return;\n      const r = el.getBoundingClientRect();\n      const x = (e.clientX - r.left) / r.width;\n      const y = 1 - (e.clientY - r.top) / r.height;\n      if (ptr.has) {\n        // reparte el movimiento en pasos cortos para que el trazo sea continuo\n        const mx = x - ptr.x;\n        const my = y - ptr.y;\n        const steps = Math.min(6, Math.max(1, Math.ceil(Math.hypot(mx * W, my * H) / 24)));\n        for (let s = 1; s <= steps; s++) push(ptr.x + (mx * s) / steps, ptr.y + (my * s) / steps, mx / steps, my / steps);\n      }\n      ptr.x = x;\n      ptr.y = y;\n      ptr.has = true;\n      wake();\n    };\n    const onLeave = () => {\n      ptr.has = false;\n    };\n    el.addEventListener(\"pointermove\", 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(\"pointerleave\", onLeave);\n      el.removeEventListener(\"pointercancel\", onLeave);\n      wakeRef.current = () => {};\n      gl.deleteTexture(tex);\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  }, [color, glowColor, density, speed]);\n\n  return (\n    <div ref={host} className={`furin-smoke-field ${className}`} style={style}>\n      {children ? <div className=\"furin-smoke-field__contenido\">{children}</div> : null}\n    </div>\n  );\n}\n\nexport default SmokeField;\n",
      "type": "registry:component",
      "target": "components/furin/smoke-field/SmokeField.tsx"
    },
    {
      "path": "registry/smoke-field/SmokeField.css",
      "content": ".furin-smoke-field {\n  position: relative;\n  width: 100%;\n  height: 100%;\n  min-height: 240px;\n  overflow: hidden;\n  background: #07080b;\n  touch-action: pan-y;\n}\n.furin-smoke-field__canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }\n.furin-smoke-field__contenido { position: relative; z-index: 1; width: 100%; height: 100%; display: grid; place-items: center; pointer-events: none; }\n.furin-smoke-field__contenido > * { pointer-events: auto; }\n",
      "type": "registry:file",
      "target": "components/furin/smoke-field/SmokeField.css"
    }
  ]
}
