{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-grid-motion",
  "type": "registry:component",
  "title": "Grid Motion",
  "description": "A tilted wall of gradient tiles with small charts and cards, its rows sliding in alternating directions. Rows under the pointer speed up and brighten.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "grid",
    "tiles",
    "marquee",
    "tilt",
    "canvas"
  ],
  "docs": "https://hyperblur.dev/effects/bg-grid-motion",
  "files": [
    {
      "path": "registry/hyperblur/bg-grid-motion.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-grid-motion.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgGridMotionProps extends React.HTMLAttributes<HTMLDivElement> {\n  tileSize?: number;\n  gap?: number;\n  angle?: number;\n  speed?: number;\n  boost?: number;\n  accent?: string;\n  colors?: string[];\n  tileColor?: string;\n}\n\nconst css = `\n.hb-qq5 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-qq5-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-qq5-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nconst qq5Palette = [\"#7c5cff\", \"#ff5c8a\", \"#22d3a7\", \"#ffb547\", \"#38bdf8\"];\n\nfunction qq5Hash(a: number, b: number) {\n  let x = Math.imul(a | 0, 73856093) ^ Math.imul(b | 0, 19349663);\n  x = Math.imul(x ^ (x >>> 15), 2246822519);\n  x ^= x >>> 13;\n  return (x >>> 0) / 4294967296;\n}\n\nfunction qq5Round(c: CanvasRenderingContext2D, x: number, y: number, w: number, h: number, r: number) {\n  c.beginPath();\n  c.moveTo(x + r, y);\n  c.arcTo(x + w, y, x + w, y + h, r);\n  c.arcTo(x + w, y + h, x, y + h, r);\n  c.arcTo(x, y + h, x, y, r);\n  c.arcTo(x, y, x + w, y, r);\n  c.closePath();\n}\n\nexport function BgGridMotion({\n  tileSize = 64,\n  gap = 10,\n  angle = -14,\n  speed = 1,\n  boost = 4,\n  accent = \"#2f7bff\",\n  colors,\n  tileColor = \"#141414\",\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: BgGridMotionProps) {\n  const wrapRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n  const colorKey = (colors && colors.length ? colors : qq5Palette).join(\"|\");\n\n  React.useEffect(() => {\n    const wrap = wrapRef.current;\n    const host = wrap?.parentElement;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    const atlas = document.createElement(\"canvas\");\n    const actx = atlas.getContext(\"2d\");\n    if (!wrap || !host || !canvas || !ctx || !actx) return;\n\n    const pal = [accent, ...colorKey.split(\"|\")];\n    const kinds = 12;\n    const size = Math.max(24, tileSize);\n    const pitch = size + Math.max(0, gap);\n    const rad = (angle * Math.PI) / 180;\n    const ca = Math.cos(rad);\n    const sa = Math.sin(rad);\n\n    let w = 0;\n    let h = 0;\n    let t = 0;\n    let dpr = 1;\n    let rows = 0;\n    let cols = 0;\n    let off = new Float32Array(0);\n    let gain = new Float32Array(0);\n    let inside = false;\n    let tx = 0;\n    let ty = 0;\n    let px = 0;\n    let py = 0;\n    let act = 0;\n    let placed = false;\n    let built = false;\n\n    function paintTile(k: number) {\n      if (!actx) return;\n      const s = size * dpr;\n      const ox = k * (s + 4);\n      actx.save();\n      actx.translate(ox, 0);\n      actx.scale(dpr, dpr);\n      const c1 = pal[k % pal.length];\n      const c2 = pal[(k * 3 + 1) % pal.length];\n      const r = Math.min(14, size * 0.18);\n      qq5Round(actx, 0.5, 0.5, size - 1, size - 1, r);\n      actx.fillStyle = tileColor;\n      actx.fill();\n      actx.save();\n      actx.clip();\n      const g = actx.createLinearGradient(0, 0, size, size);\n      g.addColorStop(0, c1);\n      g.addColorStop(1, c2);\n      actx.globalAlpha = 0.18 + (k % 4) * 0.07;\n      actx.fillStyle = g;\n      actx.fillRect(0, 0, size, size);\n      actx.globalAlpha = 1;\n      const kind = k % 4;\n      if (kind === 0) {\n        const rg = actx.createRadialGradient(size * 0.32, size * 0.38, 0, size * 0.32, size * 0.38, size * 0.3);\n        rg.addColorStop(0, c1);\n        rg.addColorStop(1, \"rgba(0,0,0,0)\");\n        actx.fillStyle = rg;\n        actx.fillRect(0, 0, size, size);\n        actx.fillStyle = \"rgba(255,255,255,0.55)\";\n        qq5Round(actx, size * 0.16, size * 0.72, size * 0.5, size * 0.07, size * 0.035);\n        actx.fill();\n        actx.fillStyle = \"rgba(255,255,255,0.25)\";\n        qq5Round(actx, size * 0.16, size * 0.84, size * 0.32, size * 0.06, size * 0.03);\n        actx.fill();\n      } else if (kind === 1) {\n        for (let b = 0; b < 5; b++) {\n          const bh = size * (0.18 + qq5Hash(k, b) * 0.46);\n          actx.fillStyle = b === 3 ? c1 : \"rgba(255,255,255,0.28)\";\n          qq5Round(actx, size * (0.16 + b * 0.14), size * 0.84 - bh, size * 0.09, bh, size * 0.03);\n          actx.fill();\n        }\n      } else if (kind === 2) {\n        actx.beginPath();\n        for (let p = 0; p <= 20; p++) {\n          const x = size * (0.1 + (p / 20) * 0.8);\n          const y = size * (0.58 - Math.sin(p * 0.5 + k) * 0.14 - (p / 20) * 0.12);\n          if (p === 0) actx.moveTo(x, y);\n          else actx.lineTo(x, y);\n        }\n        actx.strokeStyle = c1;\n        actx.lineWidth = Math.max(1.5, size * 0.035);\n        actx.lineCap = \"round\";\n        actx.stroke();\n        actx.fillStyle = \"rgba(255,255,255,0.4)\";\n        qq5Round(actx, size * 0.12, size * 0.14, size * 0.3, size * 0.07, size * 0.035);\n        actx.fill();\n      } else {\n        actx.lineWidth = Math.max(1, size * 0.025);\n        for (let ring = 3; ring >= 1; ring--) {\n          actx.beginPath();\n          actx.arc(size * 0.68, size * 0.36, size * 0.09 * ring, 0, Math.PI * 2);\n          actx.strokeStyle = ring === 1 ? c2 : \"rgba(255,255,255,0.2)\";\n          actx.stroke();\n        }\n        actx.fillStyle = \"rgba(255,255,255,0.45)\";\n        qq5Round(actx, size * 0.14, size * 0.74, size * 0.42, size * 0.07, size * 0.035);\n        actx.fill();\n      }\n      actx.restore();\n      qq5Round(actx, 0.5, 0.5, size - 1, size - 1, r);\n      actx.strokeStyle = \"rgba(255,255,255,0.08)\";\n      actx.lineWidth = 1;\n      actx.stroke();\n      actx.restore();\n    }\n\n    function buildAtlas() {\n      if (!actx) return;\n      const s = size * dpr;\n      atlas.width = Math.ceil(kinds * (s + 4));\n      atlas.height = Math.ceil(s);\n      actx.clearRect(0, 0, atlas.width, atlas.height);\n      for (let k = 0; k < kinds; k++) paintTile(k);\n    }\n\n    function idleX(tt: number) {\n      return w * (0.5 + 0.34 * Math.sin(tt * 0.37 + 0.5));\n    }\n    function idleY(tt: number) {\n      return h * (0.5 + 0.34 * Math.sin(tt * 0.23 + 1.4));\n    }\n\n    function track(dt: number) {\n      const gx = inside ? tx : idleX(t);\n      const gy = inside ? ty : idleY(t);\n      if (!placed) {\n        px = gx;\n        py = gy;\n        placed = true;\n        return;\n      }\n      const k = 1 - Math.exp(-dt * (inside ? 10 : 2));\n      px += (gx - px) * k;\n      py += (gy - py) * k;\n      act += ((inside ? 1 : 0) - act) * (1 - Math.exp(-dt * 3));\n    }\n\n    function layout() {\n      const D = Math.sqrt(w * w + h * h) + pitch * 2;\n      rows = Math.ceil(D / pitch) + 1;\n      cols = Math.ceil(D / pitch) + 2;\n      off = new Float32Array(rows);\n      gain = new Float32Array(rows);\n      for (let r = 0; r < rows; r++) off[r] = qq5Hash(r, 91) * pitch * 4;\n    }\n\n    function update(dt: number) {\n      const cx = w / 2;\n      const cy = h / 2;\n      const dx = px - cx;\n      const dy = py - cy;\n      const ly = -dx * sa + dy * ca;\n      const top = -(rows * pitch) / 2;\n      const strength = boost * (0.35 + 0.65 * act);\n      const base = 16 * speed;\n      for (let r = 0; r < rows; r++) {\n        const rc = top + r * pitch + pitch / 2;\n        const d = (ly - rc) / pitch;\n        const target = strength * Math.exp(-d * d * 0.9);\n        gain[r] += (target - gain[r]) * (1 - Math.exp(-dt * 5));\n        const dir = r % 2 === 0 ? 1 : -1;\n        off[r] += dir * base * (1 + gain[r]) * dt;\n      }\n    }\n\n    function draw() {\n      if (!ctx || !w || !h) return;\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      ctx.clearRect(0, 0, w, h);\n      const cx = w / 2;\n      const cy = h / 2;\n      ctx.save();\n      ctx.translate(cx, cy);\n      ctx.rotate(rad);\n      const top = -(rows * pitch) / 2;\n      const left = -(cols * pitch) / 2;\n      const dxp = px - cx;\n      const dyp = py - cy;\n      const lpx = dxp * ca + dyp * sa;\n      const lpy = -dxp * sa + dyp * ca;\n      const s = size * dpr;\n      for (let r = 0; r < rows; r++) {\n        const o = off[r];\n        const shift = Math.floor(o / pitch);\n        const frac = o - shift * pitch;\n        const y = top + r * pitch;\n        const glow = Math.min(1, gain[r] / Math.max(0.5, boost));\n        for (let c = -1; c < cols; c++) {\n          const x = left + c * pitch + frac;\n          const idx = c - shift;\n          const k = Math.floor(qq5Hash(idx, r * 7 + 3) * kinds);\n          const ddx = x + size / 2 - lpx;\n          const ddy = y + size / 2 - lpy;\n          const near = Math.exp(-(ddx * ddx + ddy * ddy) / (pitch * pitch * 2.2));\n          const sc = 1 + near * 0.06 * (0.4 + 0.6 * act);\n          ctx.globalAlpha = 0.55 + glow * 0.3 + near * 0.15;\n          const sw = size * sc;\n          ctx.drawImage(atlas, k * (s + 4), 0, s, s, x + (size - sw) / 2, y + (size - sw) / 2, sw, sw);\n        }\n      }\n      ctx.restore();\n      ctx.globalAlpha = 1;\n      ctx.globalCompositeOperation = \"destination-out\";\n      const R = Math.max(w, h) * 0.75;\n      const v = ctx.createRadialGradient(cx, cy, Math.min(w, h) * 0.2, cx, cy, R);\n      v.addColorStop(0, \"rgba(0,0,0,0)\");\n      v.addColorStop(1, \"rgba(0,0,0,0.92)\");\n      ctx.fillStyle = v;\n      ctx.fillRect(0, 0, w, h);\n      ctx.globalCompositeOperation = \"source-over\";\n    }\n\n    function resize() {\n      if (!wrap || !canvas || !ctx) return;\n      const nw = wrap.clientWidth;\n      const nh = wrap.clientHeight;\n      if (!nw || !nh) return;\n      const nd = Math.min(2, window.devicePixelRatio || 1);\n      const rebuild = nd !== dpr || !built;\n      dpr = nd;\n      w = nw;\n      h = nh;\n      canvas.width = Math.max(1, Math.round(w * dpr));\n      canvas.height = Math.max(1, Math.round(h * dpr));\n      if (rebuild) {\n        buildAtlas();\n        built = true;\n      }\n      layout();\n      if (motion.matches) still();\n      else {\n        track(0);\n        draw();\n      }\n    }\n\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    let visible = true;\n    let raf = 0;\n    let prev = 0;\n\n    function still() {\n      t = 0;\n      inside = false;\n      placed = false;\n      act = 0;\n      track(0);\n      gain.fill(0);\n      draw();\n    }\n\n    function frame(now: number) {\n      const dt = Math.min(0.033, (now - prev) / 1000);\n      prev = now;\n      t += dt;\n      track(dt);\n      update(dt);\n      draw();\n      raf = requestAnimationFrame(frame);\n    }\n\n    function sync() {\n      const run = visible && !document.hidden && !motion.matches;\n      if (run && !raf) {\n        prev = performance.now();\n        raf = requestAnimationFrame(frame);\n      } else if (!run && raf) {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n      if (motion.matches) still();\n    }\n\n    function move(e: PointerEvent) {\n      if (!wrap || motion.matches) return;\n      const rect = wrap.getBoundingClientRect();\n      const kx = rect.width ? wrap.clientWidth / rect.width : 1;\n      const ky = rect.height ? wrap.clientHeight / rect.height : 1;\n      tx = (e.clientX - rect.left) * kx;\n      ty = (e.clientY - rect.top) * ky;\n      inside = true;\n    }\n\n    function leave(e: PointerEvent) {\n      if (e.type === \"pointerup\" && e.pointerType === \"mouse\") return;\n      inside = false;\n    }\n\n    const ro = new ResizeObserver(resize);\n    const io = new IntersectionObserver((entries) => {\n      visible = entries[entries.length - 1].isIntersecting;\n      sync();\n    });\n    ro.observe(wrap);\n    io.observe(wrap);\n    document.addEventListener(\"visibilitychange\", sync);\n    motion.addEventListener(\"change\", sync);\n    host.addEventListener(\"pointermove\", move, { passive: true });\n    host.addEventListener(\"pointerdown\", move, { passive: true });\n    host.addEventListener(\"pointerleave\", leave);\n    host.addEventListener(\"pointerup\", leave);\n    host.addEventListener(\"pointercancel\", leave);\n    resize();\n    sync();\n\n    return () => {\n      if (raf) cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", sync);\n      motion.removeEventListener(\"change\", sync);\n      host.removeEventListener(\"pointermove\", move);\n      host.removeEventListener(\"pointerdown\", move);\n      host.removeEventListener(\"pointerleave\", leave);\n      host.removeEventListener(\"pointerup\", leave);\n      host.removeEventListener(\"pointercancel\", leave);\n    };\n  }, [tileSize, gap, angle, speed, boost, accent, colorKey, tileColor]);\n\n  return (\n    <div ref={wrapRef} className={`hb-qq5 ${className}`} style={style} {...rest}>\n      <style href=\"hb-bg-grid-motion\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-qq5-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-qq5-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}