{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"dot-grid","type":"registry:ui","title":"Dot Grid","description":"A canvas dot grid where ripples travel out from the pointer, and from random spots when it is idle.","dependencies":["motion"],"registryDependencies":["https://ui.mekaigames.com/r/theme.json"],"files":[{"path":"components/ui/dot-grid.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useReducedMotion } from \"motion/react\";\nimport { useEffect, useRef } from \"react\";\n\ntype DotGridProps = {\n  /** Resting dot color. */\n  color?: string;\n  /** Color dots reach at the crest of a ripple. */\n  activeColor?: string;\n  /** Distance between dots, in pixels. */\n  gap?: number;\n  /** Dot radius at rest, in pixels. */\n  dotSize?: number;\n  /** Radius of the glow that follows the pointer, in pixels. */\n  radius?: number;\n  className?: string;\n};\n\ntype Ripple = { x: number; y: number; born: number; strength: number };\n\nconst RING_SPEED = 0.3; // px per ms\nconst RING_LIFE = 2600; // ms\nconst POINTER_EVERY = 900; // ms between waves while the pointer is over the grid\nconst IDLE_EVERY = 2200; // ms between automatic waves\nconst LEVELS = 8; // brightness steps, so dots can be drawn in a few batched paths\n\nfunction hexToRgb(hex: string) {\n  let h = hex.replace(\"#\", \"\");\n  if (h.length === 3) h = h.replace(/./g, (c) => c + c);\n  const n = parseInt(h, 16) || 0;\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\n/** A canvas dot grid where ripples travel out from the pointer, or from random spots when idle. */\nexport function DotGrid({\n  color = \"#3d3631\",\n  activeColor = \"#ff784f\",\n  gap = 24,\n  dotSize = 1.6,\n  radius = 120,\n  className = \"\",\n}: DotGridProps) {\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const reduce = useReducedMotion();\n\n  useEffect(() => {\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!canvas || !ctx) return;\n\n    const base = hexToRgb(color);\n    const hot = hexToRgb(activeColor);\n    const step = Math.max(6, gap);\n    const size = Math.max(0.5, dotSize);\n    const ringW = Math.max(step * 0.9, 18);\n\n    let width = 0;\n    let height = 0;\n    let cols = 0;\n    let rows = 0;\n    let px = new Float32Array(0);\n    let py = new Float32Array(0);\n    let level = new Int8Array(0);\n    let frame = 0;\n    let visible = true;\n    let pointer: { x: number; y: number } | null = null;\n    let lastEmit = -Infinity;\n    let lastPointer = -Infinity;\n    const ripples: Ripple[] = [];\n\n    const lerp = (k: number) =>\n      `rgb(${Math.round(base[0] + (hot[0] - base[0]) * k)},${Math.round(base[1] + (hot[1] - base[1]) * k)},${Math.round(\n        base[2] + (hot[2] - base[2]) * k,\n      )})`;\n    const palette = Array.from({ length: LEVELS }, (_, i) => lerp((i + 1) / LEVELS));\n\n    const draw = (t: number) => {\n      if (!reduce) {\n        if (pointer && t - lastEmit > POINTER_EVERY) {\n          ripples.push({ x: pointer.x, y: pointer.y, born: t, strength: 1 });\n          lastEmit = t;\n        } else if (!pointer && t - lastPointer > 1500 && t - lastEmit > IDLE_EVERY) {\n          ripples.push({ x: Math.random() * width, y: Math.random() * height, born: t, strength: 0.85 });\n          lastEmit = t;\n        }\n      }\n      for (let i = ripples.length - 1; i >= 0; i--) if (t - ripples[i].born > RING_LIFE) ripples.splice(i, 1);\n\n      const offX = (width - (cols - 1) * step) / 2;\n      const offY = (height - (rows - 1) * step) / 2;\n      let idx = 0;\n      for (let r = 0; r < rows; r++) {\n        for (let c = 0; c < cols; c++, idx++) {\n          const x = offX + c * step;\n          const y = offY + r * step;\n          let k = 0;\n          let dx = 0;\n          let dy = 0;\n          if (pointer && !reduce) {\n            const d = Math.hypot(x - pointer.x, y - pointer.y);\n            if (d < radius) k += (1 - d / radius) ** 2;\n          }\n          for (const rp of ripples) {\n            const age = t - rp.born;\n            const ox = x - rp.x;\n            const oy = y - rp.y;\n            const d = Math.sqrt(ox * ox + oy * oy);\n            const off = d - age * RING_SPEED;\n            if (off > ringW * 2.5 || off < -ringW * 2.5) continue;\n            const wave = Math.exp(-(off * off) / (2 * ringW * ringW)) * (1 - age / RING_LIFE) * rp.strength;\n            k += wave;\n            if (d > 0.001) {\n              const push = wave * step * 0.2;\n              dx += (ox / d) * push;\n              dy += (oy / d) * push;\n            }\n          }\n          px[idx] = x + dx;\n          py[idx] = y + dy;\n          level[idx] = k < 0.04 ? -1 : Math.min(LEVELS - 1, Math.floor(Math.min(1, k) * LEVELS));\n        }\n      }\n\n      ctx.clearRect(0, 0, width, height);\n      const total = cols * rows;\n      for (let l = -1; l < LEVELS; l++) {\n        const kl = l < 0 ? 0 : (l + 1) / LEVELS;\n        const rad = size * (1 + kl * 1.4);\n        let any = false;\n        ctx.beginPath();\n        for (let i = 0; i < total; i++) {\n          if (level[i] !== l) continue;\n          any = true;\n          ctx.moveTo(px[i] + rad, py[i]);\n          ctx.arc(px[i], py[i], rad, 0, Math.PI * 2);\n        }\n        if (!any) continue;\n        ctx.fillStyle = l < 0 ? `rgb(${base[0]},${base[1]},${base[2]})` : palette[l];\n        ctx.fill();\n      }\n\n      if (!reduce && visible) frame = requestAnimationFrame(draw);\n      else frame = 0;\n    };\n\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      width = canvas.clientWidth;\n      height = canvas.clientHeight;\n      canvas.width = Math.round(width * dpr);\n      canvas.height = Math.round(height * dpr);\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      cols = Math.max(1, Math.floor(width / step) + 1);\n      rows = Math.max(1, Math.floor(height / step) + 1);\n      px = new Float32Array(cols * rows);\n      py = new Float32Array(cols * rows);\n      level = new Int8Array(cols * rows);\n      if (reduce) draw(performance.now());\n    };\n\n    // Listen on the window so the grid still reacts when content sits on top of it.\n    const local = (e: PointerEvent) => {\n      const rect = canvas.getBoundingClientRect();\n      const x = e.clientX - rect.left;\n      const y = e.clientY - rect.top;\n      return x >= 0 && y >= 0 && x <= rect.width && y <= rect.height ? { x, y } : null;\n    };\n    const onMove = (e: PointerEvent) => {\n      const p = local(e);\n      if (p && !pointer) lastEmit = -Infinity; // a wave right away on enter\n      pointer = p;\n      if (p) lastPointer = performance.now();\n    };\n    const onDown = (e: PointerEvent) => {\n      const p = local(e);\n      if (!p) return;\n      const now = performance.now();\n      ripples.push({ x: p.x, y: p.y, born: now, strength: 1.5 });\n      lastEmit = now;\n      lastPointer = now;\n    };\n    const onOut = (e: PointerEvent) => {\n      if (!e.relatedTarget) pointer = null;\n    };\n\n    resize();\n    const observer = new ResizeObserver(resize);\n    observer.observe(canvas);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible && !reduce && !frame) frame = requestAnimationFrame(draw);\n    });\n    io.observe(canvas);\n    if (!reduce) {\n      frame = requestAnimationFrame(draw);\n      window.addEventListener(\"pointermove\", onMove, { passive: true });\n      window.addEventListener(\"pointerdown\", onDown, { passive: true });\n      document.addEventListener(\"pointerout\", onOut);\n    }\n\n    return () => {\n      cancelAnimationFrame(frame);\n      observer.disconnect();\n      io.disconnect();\n      window.removeEventListener(\"pointermove\", onMove);\n      window.removeEventListener(\"pointerdown\", onDown);\n      document.removeEventListener(\"pointerout\", onOut);\n    };\n  }, [color, activeColor, gap, dotSize, radius, reduce]);\n\n  return (\n    <canvas\n      ref={canvasRef}\n      className={`pointer-events-none absolute inset-0 block size-full ${className}`}\n      aria-hidden=\"true\"\n    />\n  );\n}\n"}]}