{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"pointer-repelled-dot-field-particle-background-that-settles-back","type":"registry:block","title":"Pointer-Repelled Dot Field","description":"","dependencies":[],"files":[{"path":"components/we-inc/pointer-repelled-dot-field-particle-background-that-settles-back.tsx","type":"registry:component","content":"import { useEffect, useRef } from \"react\";\n\n// Pointer-Repelled Dot Field: a canvas 2D particle background behind a small hero.\n// Sprites are drawn once to small offscreen canvases, motion is scaled by frame\n// time, the loop stops off screen, and reduced motion gets one settled frame.\n// Values worth changing:\nconst SPACING = 20; // pixels between resting dots\nconst RADIUS = 120; // how far the pointer pushes, in pixels\nconst INK = \"40,39,36\"; // resting dot colour, as r,g,b\nconst HEAT = \"194,72,20\"; // colour of dots while they are pushed\nconst SPRING = 16; // how quickly dots settle home: 8 is lazy, 30 is snappy\nconst WARMUP = 0; // frames simulated before the first paint (0 keeps the field at rest)\n\ntype Ptr = { x: number; y: number; on: boolean };\ntype Scene = { update: (dt: number, t: number) => void; draw: (c: CanvasRenderingContext2D, t: number) => void };\n\nfunction glow(size: number, rgb: string, stops: number[][]): HTMLCanvasElement {\n  const c = document.createElement(\"canvas\");\n  c.width = c.height = size;\n  const g = c.getContext(\"2d\")!;\n  const r = size / 2;\n  const grd = g.createRadialGradient(r, r, 0, r, r, r);\n  for (const [o, a] of stops) grd.addColorStop(o, `rgba(${rgb},${a})`);\n  g.fillStyle = grd;\n  g.fillRect(0, 0, size, size);\n  return c;\n}\n\nfunction mount(canvas: HTMLCanvasElement, root: HTMLElement): () => void {\n  const ctx = canvas.getContext(\"2d\");\n  if (!ctx) return () => {};\n  const still = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n  const ptr: Ptr = { x: -1e4, y: -1e4, on: false };\n  let w = 0, h = 0, t = 0, last = 0, raf = 0, seen = true;\n  let scene: Scene | null = null;\n  const fit = () => {\n    const r = canvas.getBoundingClientRect();\n    if (scene && Math.abs(r.width - w) < 1 && Math.abs(r.height - h) < 1) return;\n    const dpr = Math.min(window.devicePixelRatio || 1, 1.5);\n    w = Math.max(1, r.width);\n    h = Math.max(1, r.height);\n    canvas.width = Math.round(w * dpr);\n    canvas.height = Math.round(h * dpr);\n    ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n    scene = createScene(w, h, ptr);\n    for (let i = 0; i < WARMUP; i++) { t += 1 / 30; scene.update(1 / 30, t); }\n    scene.draw(ctx, t);\n  };\n  const frame = (now: number) => {\n    const dt = Math.min(0.05, Math.max(0, (now - last) / 1000));\n    last = now;\n    t += dt;\n    if (scene) { scene.update(dt, t); scene.draw(ctx, t); }\n    raf = requestAnimationFrame(frame);\n  };\n  const run = () => {\n    cancelAnimationFrame(raf);\n    raf = 0;\n    if (!still && seen && !document.hidden) { last = performance.now(); raf = requestAnimationFrame(frame); }\n  };\n  const move = (e: PointerEvent) => {\n    const r = canvas.getBoundingClientRect();\n    ptr.x = e.clientX - r.left;\n    ptr.y = e.clientY - r.top;\n    ptr.on = true;\n  };\n  const leave = () => { ptr.on = false; };\n  const ro = new ResizeObserver(fit);\n  ro.observe(canvas);\n  const io = new IntersectionObserver((es) => { seen = es[es.length - 1].isIntersecting; run(); });\n  io.observe(canvas);\n  document.addEventListener(\"visibilitychange\", run);\n  root.addEventListener(\"pointermove\", move);\n  root.addEventListener(\"pointerleave\", leave);\n  fit();\n  run();\n  return () => {\n    cancelAnimationFrame(raf);\n    ro.disconnect();\n    io.disconnect();\n    document.removeEventListener(\"visibilitychange\", run);\n    root.removeEventListener(\"pointermove\", move);\n    root.removeEventListener(\"pointerleave\", leave);\n  };\n}\n\nfunction createScene(w: number, h: number, ptr: Ptr): Scene {\n  const ink = glow(16, INK, [[0, 1], [0.4, 1], [0.55, 0]]);\n  const heat = glow(16, HEAT, [[0, 1], [0.4, 1], [0.55, 0]]);\n  const ps: { hx: number; hy: number; x: number; y: number; vx: number; vy: number; z: number }[] = [];\n  for (let y = SPACING / 2; y < h; y += SPACING) {\n    for (let x = SPACING / 2; x < w; x += SPACING) {\n      const hx = x + (Math.random() - 0.5) * SPACING * 0.18, hy = y + (Math.random() - 0.5) * SPACING * 0.18;\n      ps.push({ hx, hy, x: hx, y: hy, vx: 0, vy: 0, z: Math.random() });\n    }\n  }\n  const src = { x: 0, y: 0 };\n  return {\n    update(dt, t) {\n      // Without a real pointer a slow invisible one wanders, so the field is never still.\n      if (ptr.on) { src.x = ptr.x; src.y = ptr.y; }\n      else { src.x = w * (0.62 + 0.3 * Math.sin(t * 0.29)); src.y = h * (0.5 + 0.36 * Math.sin(t * 0.47 + 1)); }\n      const damp = Math.exp(-7 * dt), R2 = RADIUS * RADIUS;\n      for (const p of ps) {\n        const dx = p.x - src.x, dy = p.y - src.y, d2 = dx * dx + dy * dy;\n        if (d2 < R2 && d2 > 0.01) {\n          const d = Math.sqrt(d2), f = (1 - d / RADIUS) ** 2 * 2600 * (0.5 + p.z);\n          p.vx += (dx / d) * f * dt;\n          p.vy += (dy / d) * f * dt;\n        }\n        p.vx = (p.vx + (p.hx - p.x) * SPRING * dt) * damp;\n        p.vy = (p.vy + (p.hy - p.y) * SPRING * dt) * damp;\n        p.x += p.vx * dt;\n        p.y += p.vy * dt;\n      }\n    },\n    draw(c) {\n      c.clearRect(0, 0, w, h);\n      for (const p of ps) {\n        const off = Math.min(1, (Math.abs(p.x - p.hx) + Math.abs(p.y - p.hy)) / 16);\n        const s = 3.2 + p.z * 2.8;\n        c.globalAlpha = 0.3 + p.z * 0.35;\n        c.drawImage(ink, p.x - s / 2, p.y - s / 2, s, s);\n        if (off > 0.05) {\n          c.globalAlpha = off;\n          c.drawImage(heat, p.x - s / 2, p.y - s / 2, s, s);\n        }\n      }\n      c.globalAlpha = 1;\n    },\n  };\n}\n\nexport default function PointerRepelledDotField() {\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const rootRef = useRef<HTMLElement>(null);\n\n  useEffect(() => {\n    if (!canvasRef.current || !rootRef.current) return;\n    return mount(canvasRef.current, rootRef.current);\n  }, []);\n\n  return (\n    <section ref={rootRef} className=\"prs-bg relative isolate flex min-h-[560px] items-center overflow-hidden px-6 py-20 sm:px-12\">\n      <style>{`.prs-bg{background:linear-gradient(180deg, #efeeea 0%, #e7e5df 100%)}.prs-scrim{background:radial-gradient(48% 70% at 20% 50%, rgba(239,238,234,0.95) 0%, rgba(239,238,234,0) 78%)}`}</style>\n      <canvas ref={canvasRef} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 -z-10 h-full w-full\" />\n      <div aria-hidden=\"true\" className=\"prs-scrim pointer-events-none absolute inset-0 -z-10\" />\n      <div className=\"mx-auto w-full max-w-6xl\">\n        <div className=\"max-w-xl\">\n          <p className=\"text-sm font-semibold uppercase tracking-[0.14em] text-[#9a3a10]\">Plinth Architecture Studio</p>\n          <h2 className=\"mt-4 text-4xl font-semibold leading-[1.08] tracking-tight text-[#1c1b19] sm:text-5xl\">Rooms that move with the people inside them.</h2>\n          <p className=\"mt-5 text-lg leading-relaxed text-[#47453f]\">Additions, renovations and new homes planned around how your household really lives, from first sketch to handover.</p>\n          <a\n            href=\"#\"\n            className=\"mt-8 inline-flex h-12 items-center rounded-md bg-[#1c1b19] px-6 text-sm font-semibold text-[#f5f4f0] transition hover:opacity-90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#1c1b19] focus-visible:ring-offset-2 focus-visible:ring-offset-[#efeeea]\"\n          >\n            Start a project\n          </a>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
