{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"dot-grid-fabric-background-that-presses-and-springs-under-the-pointer","type":"registry:block","title":"Dot Grid Fabric","description":"","dependencies":[],"files":[{"path":"components/we-inc/dot-grid-fabric-background-that-presses-and-springs-under-the-pointer.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\n// The values worth changing.\nconst SETTINGS = {\n  gap: 22, // distance between dots in pixels\n  ink: \"rgba(36, 44, 58, 0.62)\", // dots at rest\n  thread: \"rgba(190, 82, 46, 0.95)\", // dots pulled furthest by the pointer\n  reach: 180, // radius of the pointer's push, in pixels\n  push: 46, // how far the cloth is pressed at the centre\n  spring: 18, // how quickly the cloth springs back; lower is looser\n};\n\nconst SCENE: any = {\n  start: 0,\n  still: 2.2,\n  init(ctx: any, w: any, h: any) {\n    const g = SETTINGS.gap;\n    const cols = Math.ceil(w / g) + 1;\n    const rows = Math.ceil(h / g) + 1;\n    const ox = (w - (cols - 1) * g) / 2;\n    const oy = (h - (rows - 1) * g) / 2;\n    const n = cols * rows;\n    const st: any = { cols, rows, ox, oy, dx: new Float32Array(n), dy: new Float32Array(n), vx: new Float32Array(n), vy: new Float32Array(n), px: w * 0.7, py: h * 0.5 };\n    // Settle the cloth around the idle pointer before the first frame.\n    for (let i = 0; i < 40; i++) SCENE.step(st, w, h, 1 / 60, i / 60, { on: false });\n    return st;\n  },\n  step(st: any, w: any, h: any, dt: any, t: any, ptr: any) {\n    // Without a real pointer, an unseen hand drifts across the cloth in a slow figure of eight.\n    const tx = ptr.on ? ptr.x : w * (0.62 + 0.28 * Math.sin(t * 0.37));\n    const ty = ptr.on ? ptr.y : h * (0.5 + 0.32 * Math.sin(t * 0.74));\n    st.px += (tx - st.px) * Math.min(1, dt * 6);\n    st.py += (ty - st.py) * Math.min(1, dt * 6);\n    const g = SETTINGS.gap;\n    const R = SETTINGS.reach;\n    const k = SETTINGS.spring;\n    const damp = Math.exp(-dt * 5.5);\n    for (let r = 0; r < st.rows; r++) {\n      for (let c = 0; c < st.cols; c++) {\n        const i = r * st.cols + c;\n        const bx = st.ox + c * g;\n        const by = st.oy + r * g;\n        const ex = bx - st.px;\n        const ey = by - st.py;\n        const d2 = ex * ex + ey * ey;\n        let fx = -st.dx[i] * k * k * 0.05;\n        let fy = -st.dy[i] * k * k * 0.05;\n        if (d2 < R * R * 4) {\n          const d = Math.sqrt(d2) + 0.001;\n          const fall = Math.exp(-d2 / (R * R * 0.5));\n          fx += (ex / d) * fall * SETTINGS.push * 9;\n          fy += (ey / d) * fall * SETTINGS.push * 9;\n        }\n        st.vx[i] = (st.vx[i] + fx * dt) * damp;\n        st.vy[i] = (st.vy[i] + fy * dt) * damp;\n        st.dx[i] += st.vx[i] * dt;\n        st.dy[i] += st.vy[i] * dt;\n      }\n    }\n  },\n  draw(ctx: any, w: any, h: any, t: any, st: any, ptr: any, dt: any) {\n    if (dt > 0) SCENE.step(st, w, h, dt, t, ptr);\n    ctx.clearRect(0, 0, w, h);\n    const g = SETTINGS.gap;\n    const calm = new Path2D();\n    const mid = new Path2D();\n    const hot = new Path2D();\n    for (let r = 0; r < st.rows; r++) {\n      for (let c = 0; c < st.cols; c++) {\n        const i = r * st.cols + c;\n        const x = st.ox + c * g + st.dx[i];\n        const y = st.oy + r * g + st.dy[i];\n        const m = Math.sqrt(st.dx[i] * st.dx[i] + st.dy[i] * st.dy[i]) / SETTINGS.push;\n        // Stretched cloth shows bigger, warmer dots, as if the weave opens under the press.\n        const rad = 1.35 + Math.min(1.8, m * 1.8);\n        const p = m < 0.15 ? calm : m < 0.4 ? mid : hot;\n        p.moveTo(x + rad, y);\n        p.arc(x, y, rad, 0, 6.2832);\n      }\n    }\n    // A soft shadow where the cloth is pressed in.\n    const dent = ctx.createRadialGradient(st.px, st.py, 0, st.px, st.py, SETTINGS.reach * 1.1);\n    dent.addColorStop(0, \"rgba(60, 50, 40, 0.10)\");\n    dent.addColorStop(0.6, \"rgba(60, 50, 40, 0.04)\");\n    dent.addColorStop(1, \"rgba(60, 50, 40, 0)\");\n    ctx.fillStyle = dent;\n    ctx.fillRect(st.px - SETTINGS.reach * 1.2, st.py - SETTINGS.reach * 1.2, SETTINGS.reach * 2.4, SETTINGS.reach * 2.4);\n    ctx.fillStyle = SETTINGS.ink;\n    ctx.fill(calm);\n    ctx.fillStyle = \"rgba(120, 70, 56, 0.75)\";\n    ctx.fill(mid);\n    ctx.fillStyle = SETTINGS.thread;\n    ctx.fill(hot);\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 DotGridFabricBackground() {\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-[600px] w-full overflow-hidden bg-[#f3f1ec] text-[#1e2733]\">\n      <canvas ref={canvasRef} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 block h-full w-full\" />\n      <div aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0\" style={{ background: \"radial-gradient(ellipse 46% 44% at 50% 50%, rgba(243,241,236,0.96) 0%, rgba(243,241,236,0.8) 60%, rgba(243,241,236,0) 100%)\" }} />\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\">\n          <p className=\"text-xs font-semibold uppercase tracking-[0.2em] text-[#a8452a]\">Weft and Warp Studio</p>\n          <h2 className=\"mt-4 text-4xl font-semibold leading-[1.05] tracking-tight sm:text-5xl lg:text-6xl\">Woven textiles for quiet rooms.</h2>\n          <p className=\"mt-5 text-base leading-relaxed text-[#48505c] sm:text-lg\">Upholstery and curtain cloth designed in our studio and woven on old shuttle looms.</p>\n          <a href=\"#\" className=\"mt-8 inline-flex h-12 items-center rounded-full bg-[#a8452a] px-6 text-sm font-semibold text-[#ffffff] transition-opacity hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#a8452a] focus-visible:ring-offset-2 focus-visible:ring-offset-[#f3f1ec]\">Browse the swatch library</a>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
