{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"pointer-warp-dot-field","type":"registry:component","title":"Pointer Warp Dot Field","description":"A canvas dot grid that bulges and brightens away from the cursor.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/pointer-warp-dot-field.tsx","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport function PointerWarpDotField({\n  spacing = 18,\n  radius = 80,\n  strength = 26,\n  connect = false,\n}: {\n  spacing?: number;\n  radius?: number;\n  strength?: number;\n  /** Draw hairlines between vertically adjacent dots, so the warp reads as a mesh. */\n  connect?: boolean;\n}) {\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const pointer = useRef({ x: -9999, y: -9999 });\n  const frame = useRef<number | null>(null);\n\n  // Options live on a ref so changing a prop adjusts the next frame instead\n  // of tearing down and restarting the loop. Synced in an effect, NOT\n  // assigned during render — writing ref.current mid-render is a render-phase\n  // side effect and is unsafe under concurrent rendering.\n  const opts = useRef({ spacing, radius, strength, connect });\n  useEffect(() => {\n    opts.current = { spacing, radius, strength, connect };\n  }, [spacing, radius, strength, connect]);\n\n  useEffect(() => {\n    const el = canvasRef.current;\n    const context = el?.getContext(\"2d\");\n    if (!el || !context) return;\n    // Re-bound so resize() and draw() below, hoisted functions, see them as\n    // non-null under strict TypeScript.\n    const canvas: HTMLCanvasElement = el;\n    const ctx: CanvasRenderingContext2D = context;\n\n    // Cap DPR at 2 — beyond that the pixel count grows faster than the\n    // visible gain, and this is a decorative layer.\n    const dpr = Math.min(window.devicePixelRatio || 1, 2);\n\n    function resize() {\n      const rect = canvas.getBoundingClientRect();\n      canvas.width = Math.round(rect.width * dpr);\n      canvas.height = Math.round(rect.height * dpr);\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n    }\n    resize();\n\n    const observer = new ResizeObserver(resize);\n    observer.observe(canvas);\n\n    function draw() {\n      const rect = canvas.getBoundingClientRect();\n      const { spacing: sp, radius: rad, strength: str, connect: link } = opts.current;\n      ctx.clearRect(0, 0, rect.width, rect.height);\n\n      const cols = Math.ceil(rect.width / sp) + 1;\n      const rows = Math.ceil(rect.height / sp) + 1;\n      const px = pointer.current.x;\n      const py = pointer.current.y;\n\n      let prevRow: { x: number; y: number }[] = [];\n\n      for (let r = 0; r < rows; r++) {\n        const row: { x: number; y: number }[] = [];\n        for (let c = 0; c < cols; c++) {\n          const ox = c * sp;\n          const oy = r * sp;\n          const dx = ox - px;\n          const dy = oy - py;\n          const dist = Math.hypot(dx, dy);\n\n          let x = ox;\n          let y = oy;\n          let scale = 1;\n\n          if (dist < rad && dist > 0.0001) {\n            // Squared falloff gives the bulge a soft shoulder rather than a\n            // hard circular edge.\n            const f = Math.pow(1 - dist / rad, 2);\n            x += (dx / dist) * f * str;\n            y += (dy / dist) * f * str;\n            scale = 1 + f * 1.6;\n          }\n\n          row.push({ x, y });\n\n          ctx.beginPath();\n          ctx.arc(x, y, scale, 0, Math.PI * 2);\n          ctx.fillStyle =\n            scale > 1.05 ? \"rgba(61,123,255,0.9)\" : \"rgba(232,234,238,0.28)\";\n          ctx.fill();\n        }\n\n        if (link && prevRow.length) {\n          ctx.strokeStyle = \"rgba(232,234,238,0.08)\";\n          ctx.lineWidth = 1;\n          for (let c = 0; c < row.length; c++) {\n            ctx.beginPath();\n            ctx.moveTo(prevRow[c].x, prevRow[c].y);\n            ctx.lineTo(row[c].x, row[c].y);\n            ctx.stroke();\n          }\n        }\n        prevRow = row;\n      }\n\n      frame.current = requestAnimationFrame(draw);\n    }\n\n    frame.current = requestAnimationFrame(draw);\n\n    return () => {\n      if (frame.current !== null) cancelAnimationFrame(frame.current);\n      observer.disconnect();\n    };\n  }, []);\n\n  return (\n    <div\n      onMouseMove={(e) => {\n        const rect = canvasRef.current?.getBoundingClientRect();\n        if (!rect) return;\n        // Ref, not state — the loop reads it each frame, so pointer movement\n        // never triggers a React render.\n        pointer.current = { x: e.clientX - rect.left, y: e.clientY - rect.top };\n      }}\n      onMouseLeave={() => {\n        pointer.current = { x: -9999, y: -9999 };\n      }}\n      className=\"relative h-full w-full overflow-hidden bg-[#07080b]\"\n    >\n      <canvas ref={canvasRef} className=\"absolute inset-0 h-full w-full\" />\n    </div>\n  );\n}\n","type":"registry:component","target":"components/viberdy/pointer-warp-dot-field.tsx"}],"meta":{"tier":"free","tags":["background","canvas","cursor","grid"],"href":"https://viberdy.dev/library/pointer-warp-dot-field","aiPrompt":"Create a React + Tailwind component named PointerWarpDotField — a canvas dot grid that bulges away from the cursor.\n\nBehavior:\n- Fill the container with a regular grid of small dots at `spacing` px.\n- Dots within `radius` px of the pointer are pushed radially outward and scaled up; the rest sit at rest.\n- Displacement uses a SQUARED falloff — f = (1 - dist / radius) ** 2 — so the bulge has a soft shoulder instead of a visible circular edge. Offset each dot by (dx / dist) * f * strength.\n- Affected dots take the accent color; resting dots are faint neutral.\n\nImplementation requirements (these are the point of the component):\n- Use ONE <canvas>, not DOM nodes. A grid this size is hundreds of nodes, and transforming hundreds of absolutely-positioned elements per frame is layout/paint work the compositor cannot absorb. Canvas draws them all in one pass.\n- Pointer position must live in a ref, NOT React state — the rAF loop reads it each frame, so pointer movement never triggers a React render.\n- Store props in a ref too, so changing one does not tear down and restart the loop.\n- Handle devicePixelRatio: scale the backing store by DPR (capped at 2) and call ctx.setTransform(dpr, 0, 0, dpr, 0, 0). Re-run on resize via ResizeObserver.\n- Cancel the animation frame and disconnect the observer on unmount.\n\nProps: spacing (number), radius (number), strength (number).\n\nOutput a single self-contained \"use client\" component.","license":"https://viberdy.dev/license"}}