{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ink-bloom",
  "type": "registry:component",
  "title": "Ink Bloom",
  "description": "Drops of ink that bloom in dark water with swirls and tendrils, one after another in a cycle. Tap to drop one; move the cursor to stir the water.",
  "author": "Marcelo Treviño Velázquez <https://furin.dev>",
  "dependencies": [],
  "categories": [
    "Backgrounds"
  ],
  "docs": "Installed in components/furin/ink-bloom/. Docs, live controls and props table: https://furin.dev/components/ink-bloom/",
  "files": [
    {
      "path": "registry/ink-bloom/InkBloom.tsx",
      "content": "\"use client\";\nimport { useEffect, useRef } from \"react\";\nimport type { CSSProperties, ReactNode } from \"react\";\nimport \"./InkBloom.css\";\n\nexport interface InkBloomProps {\n  /** Colores de las gotas (se usan hasta tres); se reparten en ciclo. */\n  colors?: string[];\n  /** Velocidad con la que florece la tinta. */\n  speed?: number;\n  /** Brillo y cantidad de tinta. */\n  intensity?: number;\n  /** El cursor agita el agua y un clic suelta una gota. */\n  interactive?: boolean;\n  /** Contenido opcional encima del agua. */\n  children?: ReactNode;\n  /** Clase extra para el contenedor. */\n  className?: string;\n  /** Estilos extra para el contenedor. */\n  style?: CSSProperties;\n}\n\nconst DEFAULT_COLORS = [\"#2E5BFF\", \"#18E0C2\", \"#FF7A3C\"];\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, uInt;\nuniform vec3 uC0, uC1, uC2, uClickCol;\nuniform vec4 uClick;\nuniform sampler2D uDisp;\n\nfloat hash(float n){ return fract(sin(n * 127.1 + 3.7) * 43758.5453); }\nfloat h2(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  return mix(mix(h2(i), h2(i + vec2(1.0, 0.0)), f.x), mix(h2(i + vec2(0.0, 1.0)), h2(i + vec2(1.0, 1.0)), 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 < 4; i++) { s += a * noise(p); p = m * p; a *= 0.5; }\n  return s;\n}\n\n// x = densidad de tinta, y = nervadura (hebras finas)\nvec2 drop(vec2 p, vec2 c, float age, float seed, float life){\n  vec2 d = p - c;\n  float a = age;\n  float grow = 1.0 - exp(-a * 0.42);\n  float R = 0.05 + 0.31 * grow;\n  float rr = length(d);\n  if (rr > R * 2.7) return vec2(0.0);\n  // remolino: la tinta se enrolla más con el tiempo y el radio\n  float ang = (0.7 + 0.9 * hash(seed)) * sin(rr * 8.0 - a * 0.7 + seed * 6.0) * (1.0 - exp(-a * 0.6)) * exp(-rr * 1.5);\n  float cs = cos(ang), sn = sin(ang);\n  vec2 q = mat2(cs, -sn, sn, cs) * d;\n  float wa = 0.10 + 0.60 * grow;\n  vec2 w = vec2(fbm(q * 3.0 + seed * 13.0 + a * 0.10), fbm(q * 3.0 + seed * 7.0 + 4.7 - a * 0.08)) - 0.5;\n  q += w * wa * R * 2.4;\n  float r = length(q) / R;\n  float rag = (fbm(q * 5.5 + seed * 3.0) - 0.5) * (0.25 + 0.55 * grow);\n  float body = 1.0 - smoothstep(0.50, 1.05, r + rag);\n  float ridge = 1.0 - abs(2.0 * fbm(q * 10.0 + seed * 5.0 + a * 0.05) - 1.0);\n  ridge = pow(ridge, 2.2);\n  // al principio la gota es un núcleo denso; luego se abre en velos\n  float core = (1.0 - smoothstep(0.0, 0.9, r)) * (1.0 - smoothstep(0.0, 2.2, a));\n  float dens = body * (0.22 + 0.85 * ridge) * (1.0 - 0.45 * grow) + core * 0.50;\n  float fade = smoothstep(0.0, 0.25, a) * (1.0 - smoothstep(0.52 * life, life, a));\n  return vec2(dens * fade, ridge * body * fade);\n}\n\nvoid main(){\n  vec2 uv = gl_FragCoord.xy / uRes;\n  float asp = uRes.x / uRes.y;\n  vec4 dp = texture2D(uDisp, uv);\n  vec2 disp = (dp.rg - 0.5) * 2.0 * 0.24;\n  vec2 p = (uv - disp - 0.5) * vec2(asp, 1.0);\n\n  // agua: fondo profundo con un poco de luz desde arriba\n  vec3 col = vec3(0.012, 0.020, 0.034) + vec3(0.015, 0.035, 0.065) * (1.0 - length((uv - vec2(0.5, 0.62)) * vec2(1.0, 1.1)) * 1.25);\n  float caustic = fbm(p * 2.2 + vec2(uTime * 0.03, -uTime * 0.02));\n  col += vec3(0.01, 0.025, 0.04) * smoothstep(0.45, 0.8, caustic);\n\n  float P = 3.3;\n  float LIFE = P * 3.0;\n  vec3 ink = vec3(0.0);\n  float shine = 0.0;\n  for (int k = 0; k < 3; k++) {\n    float fk = float(k);\n    float tt = uTime - fk * P;\n    float cyc = floor(tt / LIFE);\n    float age = tt - cyc * LIFE;\n    float id = cyc * 3.0 + fk;\n    float seed = hash(id + 1.0);\n    vec2 c = vec2((hash(id * 3.1 + 5.0) - 0.5) * 0.70 * asp, (hash(id * 7.7 + 2.0) - 0.5) * 0.62);\n    float ci = mod(floor(id), 3.0);\n    vec3 cc = ci < 0.5 ? uC0 : (ci < 1.5 ? uC1 : uC2);\n    vec2 r = drop(p, c, age, seed, LIFE);\n    ink += cc * r.x;\n    shine += r.y * r.x;\n  }\n  if (uClick.w > 0.5) {\n    float age = uTime - uClick.z;\n    if (age > 0.0 && age < LIFE) {\n      vec2 r = drop(p, uClick.xy * vec2(asp, 1.0), age, hash(uClick.z * 9.0), LIFE);\n      ink += uClickCol * r.x;\n      shine += r.y * r.x;\n    }\n  }\n  col += ink * 1.0 * uInt;\n  col += vec3(0.75, 0.9, 1.0) * pow(clamp(shine, 0.0, 1.0), 2.0) * 0.16 * uInt;\n  col = col / (1.0 + max(col - 0.75, 0.0) * 1.1);\n  col *= 1.0 - 0.45 * dot(uv - 0.5, uv - 0.5) * 2.0;\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/** Tinta en agua: gotas que se abren en núcleos, velos y remolinos; el cursor agita el agua y un clic suelta otra gota. */\nexport function InkBloom({\n  colors = DEFAULT_COLORS,\n  speed = 1,\n  intensity = 1,\n  interactive = true,\n  children,\n  className = \"\",\n  style,\n}: InkBloomProps) {\n  const host = useRef<HTMLDivElement>(null);\n  const colorsKey = colors.join(\"|\");\n  const live = useRef({ colors, speed, intensity, interactive });\n  live.current = { colors, speed, intensity, 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-ink-bloom__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(\"[ink-bloom] 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(\"[ink-bloom] 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: string) => gl.getUniformLocation(prog, n);\n    const U = { res: u(\"uRes\"), time: u(\"uTime\"), int: u(\"uInt\"), c0: u(\"uC0\"), c1: u(\"uC1\"), c2: u(\"uC2\"), clickCol: u(\"uClickCol\"), click: u(\"uClick\"), disp: u(\"uDisp\") };\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    const palette = (): [number, number, number][] => {\n      const list = live.current.colors.length ? live.current.colors : DEFAULT_COLORS;\n      return [0, 1, 2].map((i) => rgb(list[i % list.length]));\n    };\n    let pal = palette();\n    let palKey = colorsKey;\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 tmp = new Float32Array(GW * GH);\n    let bytes = new Uint8Array(GW * GH * 4);\n    let time = reduce ? 9 : 5 + Math.random() * 3;\n    let last = 0, raf = 0, visible = true;\n    let clickCount = 0;\n    const click = { x: 0.5, y: 0.5, t: 0, on: 0, col: [1, 1, 1] as [number, number, number] };\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.8;\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      tmp = new Float32Array(GW * GH);\n      bytes = new Uint8Array(GW * GH * 4);\n      draw();\n    };\n\n    const draw = () => {\n      if (palKey !== live.current.colors.join(\"|\")) {\n        palKey = live.current.colors.join(\"|\");\n        pal = palette();\n      }\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] = 0;\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      gl.uniform2f(U.res, canvas.width, canvas.height);\n      gl.uniform1f(U.time, time);\n      gl.uniform1f(U.int, live.current.intensity);\n      gl.uniform3f(U.c0, pal[0][0], pal[0][1], pal[0][2]);\n      gl.uniform3f(U.c1, pal[1][0], pal[1][1], pal[1][2]);\n      gl.uniform3f(U.c2, pal[2][0], pal[2][1], pal[2][2]);\n      gl.uniform3f(U.clickCol, click.col[0], click.col[1], click.col[2]);\n      gl.uniform4f(U.click, click.x - 0.5, click.y - 0.5, click.t, click.on);\n      gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);\n    };\n\n    const relax = (dt: number) => {\n      const k = Math.exp(-dt / 2.2);\n      for (const arr of [dx, dy]) {\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 up = arr[Math.max(j - 1, 0) * GW + i];\n            const dn = arr[Math.min(j + 1, GH - 1) * GW + i];\n            tmp[o] = (arr[o] * 0.6 + (l + r + up + dn) * 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      const sigma = 0.13;\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 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          const rx = (i + 0.5) / GW - cx;\n          const ry = (j + 0.5) / GH - cy;\n          // remolino: el agua gira alrededor del trazo, con sentidos opuestos a cada lado\n          const side = Math.sign(mx * ry - my * rx) || 0;\n          const o = j * GW + i;\n          dx[o] += w * (mx * 1.2 + -my * side * 1.15);\n          dy[o] += w * (my * 1.2 + mx * side * 1.15);\n        }\n      }\n      for (let i = 0; i < dx.length; i++) {\n        dx[i] = Math.max(-0.24, Math.min(0.24, dx[i]));\n        dy[i] = Math.max(-0.24, Math.min(0.24, dy[i]));\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        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 onDown = (e: PointerEvent) => {\n      if (!live.current.interactive) return;\n      const r = el.getBoundingClientRect();\n      click.x = (e.clientX - r.left) / r.width;\n      click.y = 1 - (e.clientY - r.top) / r.height;\n      click.t = time;\n      click.on = 1;\n      click.col = pal[clickCount++ % 3];\n      wake();\n    };\n    const onLeave = () => {\n      ptr.has = false;\n    };\n    el.addEventListener(\"pointermove\", onMove);\n    el.addEventListener(\"pointerdown\", onDown);\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\", onDown);\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  }, [colorsKey, speed, intensity]);\n\n  return (\n    <div ref={host} className={`furin-ink-bloom ${className}`} style={style}>\n      {children ? <div className=\"furin-ink-bloom__contenido\">{children}</div> : null}\n    </div>\n  );\n}\n\nexport default InkBloom;\n",
      "type": "registry:component",
      "target": "components/furin/ink-bloom/InkBloom.tsx"
    },
    {
      "path": "registry/ink-bloom/InkBloom.css",
      "content": ".furin-ink-bloom {\n  position: relative;\n  width: 100%;\n  height: 100%;\n  min-height: 240px;\n  overflow: hidden;\n  background: #05070b;\n  touch-action: pan-y;\n}\n.furin-ink-bloom__canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }\n.furin-ink-bloom__contenido { position: relative; z-index: 1; width: 100%; height: 100%; display: grid; place-items: center; pointer-events: none; }\n.furin-ink-bloom__contenido > * { pointer-events: auto; }\n",
      "type": "registry:file",
      "target": "components/furin/ink-bloom/InkBloom.css"
    }
  ]
}
