{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"two-ink-liquid-marble-background-swirling-slowly-on-bone-paper","type":"registry:block","title":"Two Ink Liquid Marble","description":"","dependencies":[],"files":[{"path":"components/we-inc/two-ink-liquid-marble-background-swirling-slowly-on-bone-paper.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\n// The values worth changing. Colours are [red, green, blue] from 0 to 255.\nconst SETTINGS = {\n  paper: [239, 232, 219], // the bare sheet between the inks\n  inkA: [28, 41, 70], // the deep ink\n  inkB: [172, 96, 56], // the warm ink\n  speed: 1, // how fast the inks swirl; 0.5 is barely moving\n  cell: 4, // CSS pixels per computed sample; higher is cheaper and softer\n};\n\nfunction inkMix(a: any, b: any, k: any) {\n  return [a[0] + (b[0] - a[0]) * k, a[1] + (b[1] - a[1]) * k, a[2] + (b[2] - a[2]) * k];\n}\n\nfunction smooth(e0: any, e1: any, x: any) {\n  const k = Math.min(1, Math.max(0, (x - e0) / (e1 - e0)));\n  return k * k * (3 - 2 * k);\n}\n\nconst SCENE: any = {\n  start: 40,\n  still: 40,\n  fps: 30,\n  init(ctx: any, w: any, h: any) {\n    const gw = Math.max(8, Math.ceil(w / SETTINGS.cell));\n    const gh = Math.max(8, Math.ceil(h / SETTINGS.cell));\n    const off = document.createElement(\"canvas\");\n    off.width = gw;\n    off.height = gh;\n    const octx = off.getContext(\"2d\");\n    return { gw, gh, off, octx, img: octx.createImageData(gw, gh) };\n  },\n  draw(ctx: any, w: any, h: any, time: any, st: any) {\n    const t = time * SETTINGS.speed;\n    const { gw, gh, img } = st;\n    const data = img.data;\n    const unit = 2.4 / Math.min(gw, gh);\n    let o = 0;\n    for (let j = 0; j < gh; j++) {\n      const v = j * unit;\n      for (let i = 0; i < gw; i++) {\n        const u = i * unit;\n        // Two rounds of warping fold the bands into slow marble swirls.\n        const q1 = Math.sin(u * 1.7 + t * 0.07) + Math.sin(v * 2.3 - t * 0.05);\n        const q2 = Math.cos(v * 1.9 + t * 0.06) + Math.sin(u * 2.1 + t * 0.04);\n        const wx = u + 0.55 * q1;\n        const wy = v + 0.55 * q2;\n        // Thin, winding strata of ink, like a cut through marbled paper: the stripe index picks the colour.\n        const band = wx * 1.5 + wy * 0.7 + 0.6 * Math.sin(wy * 2.2 + wx * 0.7 + t * 0.04);\n        const f = band - Math.floor(band);\n        let c = SETTINGS.paper;\n        // Each stripe: paper, a dark vein, a band of the deep ink, paper, a hairline, a band of the warm ink.\n        const inA = smooth(0.08, 0.12, f) * (1 - smooth(0.3, 0.34, f));\n        const inB = smooth(0.56, 0.6, f) * (1 - smooth(0.74, 0.78, f));\n        c = inkMix(c, SETTINGS.inkA, inA * (0.75 + 0.25 * Math.sin(band * 1.7)));\n        c = inkMix(c, SETTINGS.inkB, inB * (0.7 + 0.3 * Math.cos(band * 1.3)));\n        const vein = Math.max(0, 1 - Math.abs(f - 0.05) * 18) + Math.max(0, 1 - Math.abs(f - 0.47) * 20) * 0.6 + Math.max(0, 1 - Math.abs(f - 0.9) * 20) * 0.4;\n        const dark = 1 - vein * 0.22;\n        data[o] = c[0] * dark;\n        data[o + 1] = c[1] * dark;\n        data[o + 2] = c[2] * dark;\n        data[o + 3] = 255;\n        o += 4;\n      }\n    }\n    st.octx.putImageData(img, 0, 0);\n    ctx.imageSmoothingEnabled = true;\n    ctx.imageSmoothingQuality = \"high\";\n    ctx.drawImage(st.off, 0, 0, w, h);\n  },\n};\n\n// Runs a scene on a canvas: sizes it to the block, pauses off screen and in hidden tabs, cleans up after itself.\nfunction runScene(root: any, canvas: any, scene: any, reduced: boolean) {\n  const ctx = canvas.getContext(\"2d\");\n  if (!ctx) return () => {};\n  let w = 0;\n  let h = 0;\n  let st: any = null;\n  let raf = 0;\n  let last = 0;\n  let acc = 0;\n  let onScreen = true;\n  let time = reduced ? (scene.still ?? scene.start ?? 0) : (scene.start ?? 0);\n  const ptr: any = { x: 0, y: 0, on: false, down: 0 };\n  const step = 1 / (scene.fps || 60);\n  const resize = () => {\n    const r = root.getBoundingClientRect();\n    const nw = Math.max(1, Math.round(r.width));\n    const nh = Math.max(1, Math.round(r.height));\n    if (nw === w && nh === h && st) return;\n    w = nw;\n    h = nh;\n    // Device pixels are capped at 1.5 and scaled by the scene's own quality factor.\n    const d = Math.min(window.devicePixelRatio || 1, 1.5) * (scene.scale || 1);\n    canvas.width = Math.max(1, Math.round(w * d));\n    canvas.height = Math.max(1, Math.round(h * d));\n    ctx.setTransform(canvas.width / w, 0, 0, canvas.height / h, 0, 0);\n    st = scene.init(ctx, w, h, canvas.width / w);\n    scene.draw(ctx, w, h, time, st, ptr, 0);\n  };\n  const tick = (now: number) => {\n    raf = requestAnimationFrame(tick);\n    const dt = last ? Math.min(0.1, (now - last) / 1000) : step;\n    last = now;\n    acc += dt;\n    if (acc < step * 0.95) return;\n    const used = Math.min(acc, 0.1);\n    acc = 0;\n    time += used;\n    scene.draw(ctx, w, h, time, st, ptr, used);\n  };\n  const play = () => {\n    if (raf || reduced || !onScreen || document.hidden) return;\n    last = 0;\n    raf = requestAnimationFrame(tick);\n  };\n  const stop = () => {\n    cancelAnimationFrame(raf);\n    raf = 0;\n  };\n  const ro = new ResizeObserver(resize);\n  ro.observe(root);\n  const io = new IntersectionObserver((entries: any) => {\n    onScreen = entries[0].isIntersecting;\n    if (onScreen) play();\n    else stop();\n  });\n  io.observe(root);\n  const onVis = () => (document.hidden ? stop() : play());\n  const onMove = (e: any) => {\n    const r = root.getBoundingClientRect();\n    ptr.x = e.clientX - r.left;\n    ptr.y = e.clientY - r.top;\n    ptr.on = true;\n  };\n  const onLeave = () => {\n    ptr.on = false;\n  };\n  const onDown = (e: any) => {\n    onMove(e);\n    ptr.down += 1;\n  };\n  document.addEventListener(\"visibilitychange\", onVis);\n  root.addEventListener(\"pointermove\", onMove);\n  root.addEventListener(\"pointerleave\", onLeave);\n  root.addEventListener(\"pointerdown\", onDown);\n  resize();\n  play();\n  return () => {\n    stop();\n    ro.disconnect();\n    io.disconnect();\n    document.removeEventListener(\"visibilitychange\", onVis);\n    root.removeEventListener(\"pointermove\", onMove);\n    root.removeEventListener(\"pointerleave\", onLeave);\n    root.removeEventListener(\"pointerdown\", onDown);\n  };\n}\n\nexport default function TwoInkLiquidMarbleBackground() {\n  const rootRef = useRef<HTMLElement>(null);\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  useEffect(() => {\n    const root = rootRef.current;\n    const canvas = canvasRef.current;\n    if (!root || !canvas) return;\n    // Visitors who prefer reduced motion get one settled frame and no loop.\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    return runScene(root, canvas, SCENE, reduced);\n  }, []);\n\n  return (\n    <section ref={rootRef} className=\"relative h-[620px] w-full overflow-hidden bg-[#efe8dc] text-[#1c2333]\">\n      <canvas ref={canvasRef} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 block h-full w-full\" />\n      <div className=\"relative z-10 mx-auto flex h-full max-w-6xl flex-col justify-center items-center text-center px-6 py-14 sm:px-10\">\n        <div className=\"max-w-xl rounded-2xl border p-7 sm:p-10 bg-[#f5f0e6] border-[rgba(28,41,70,0.18)]\">\n          <p className=\"text-xs font-semibold uppercase tracking-[0.2em] text-[#8a4a28]\">Halden and Rowe Bindery</p>\n          <h2 className=\"font-serif mt-4 text-4xl font-semibold leading-[1.05] tracking-tight sm:text-5xl lg:text-6xl\">Endpapers marbled one sheet at a time.</h2>\n          <p className=\"mt-5 text-base leading-relaxed text-[#3d4352] sm:text-lg\">Hand-floated papers in two inks for journals, wedding albums and small-run books, cut to your trim size.</p>\n          <a href=\"#\" className=\"mt-8 inline-flex h-12 items-center rounded-[2px] bg-[#1c2946] px-6 text-sm font-semibold text-[#f4efe5] transition-opacity hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#1c2946] focus-visible:ring-offset-2 focus-visible:ring-offset-[#efe8dc]\">Order a sample pack</a>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
