{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"striped-grid","type":"registry:ui","title":"Striped Grid","description":"A slowly scrolling grid of diagonally hatched cells that light up and fade as the cursor passes. Canvas 2D.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/striped-grid.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface StripedGridProps {\n  /** Px per cell, including the gap. */\n  cellSize?: number;\n  /** Px between cells. */\n  gap?: number;\n  /** Px between stripes inside a cell. */\n  stripeSpacing?: number;\n  /** Stripe and outline color of resting cells. */\n  stripeColor?: string;\n  /** Color of cells under the cursor. */\n  highlightColor?: string;\n  /** Scroll speed in px per second; 0 holds the grid still. */\n  speed?: number;\n  className?: string;\n}\n\nfunction rgb(hex: string) {\n  let h = hex.replace(\"#\", \"\");\n  if (h.length === 3) h = h.replace(/./g, \"$&$&\");\n  const n = parseInt(h.padEnd(6, \"0\").slice(0, 6), 16) || 0;\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\nexport function StripedGrid({\n  cellSize = 56,\n  gap = 8,\n  stripeSpacing = 7,\n  stripeColor = \"#52525b\",\n  highlightColor = \"#c6ff3d\",\n  speed = 12,\n  className,\n}: StripedGridProps) {\n  const ref = useRef<HTMLCanvasElement>(null);\n  const opts = useRef({ cellSize, gap, stripeSpacing, c: rgb(stripeColor), hl: rgb(highlightColor), speed });\n  const redraw = useRef<() => void>(undefined);\n\n  useEffect(() => {\n    opts.current = { cellSize, gap, stripeSpacing, c: rgb(stripeColor), hl: rgb(highlightColor), speed };\n    redraw.current?.();\n  }, [cellSize, gap, stripeSpacing, stripeColor, highlightColor, speed]);\n\n  useEffect(() => {\n    const canvas = ref.current!;\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n    const area = canvas.parentElement ?? canvas;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    // Heat per world cell (\"i,j\"), so highlights stay attached to cells as the grid scrolls.\n    const heat = new Map<string, number>();\n    const mouse = { x: -1e4, y: -1e4 };\n    let w = 0;\n    let h = 0;\n    let off = 0;\n    let raf = 0;\n    let last = 0;\n\n    // Diagonal stripes covering a rect, phase-locked to world space so they scroll with the grid.\n    const stripes = (x: number, y: number, size: number, sp: number) => {\n      const phase = (((x + y - 2 * off) % sp) + sp) % sp;\n      for (let d = (sp - phase) % sp; d < size * 2; d += sp) {\n        const x0 = x + Math.max(0, d - size);\n        const y0 = y + Math.min(d, size);\n        const x1 = x + Math.min(d, size);\n        const y1 = y + Math.max(0, d - size);\n        ctx.moveTo(x0, y0);\n        ctx.lineTo(x1, y1);\n      }\n    };\n\n    const draw = (dt: number) => {\n      const o = opts.current;\n      const s = Math.max(12, o.cellSize);\n      const gp = Math.min(Math.max(0, o.gap), s - 4);\n      const inner = s - gp;\n      const sp = Math.max(3, o.stripeSpacing);\n      off += dt * o.speed;\n      const sx = ((off % s) + s) % s;\n      const base = Math.floor(off / s);\n\n      const decay = Math.exp(-dt * 2.2);\n      for (const [k, v] of heat) {\n        const nv = v * decay;\n        if (nv < 0.01) heat.delete(k);\n        else heat.set(k, nv);\n      }\n      // Cell under the cursor (in world indices).\n      const mi = Math.floor((mouse.x - sx) / s) - base;\n      const mj = Math.floor((mouse.y - sx) / s) - base;\n      if (mouse.x > -1e3) {\n        const lx = mouse.x - sx - (mi + base) * s;\n        const ly = mouse.y - sx - (mj + base) * s;\n        if (lx >= gp / 2 && lx < s - gp / 2 && ly >= gp / 2 && ly < s - gp / 2) heat.set(`${mi},${mj}`, 1);\n      }\n\n      ctx.clearRect(0, 0, w, h);\n      const [r, g, b] = o.c;\n      ctx.lineWidth = 1;\n      ctx.beginPath();\n      for (let x = sx - s; x < w; x += s) {\n        for (let y = sx - s; y < h; y += s) {\n          stripes(x + gp / 2, y + gp / 2, inner, sp);\n        }\n      }\n      ctx.strokeStyle = `rgba(${r},${g},${b},0.45)`;\n      ctx.stroke();\n      ctx.beginPath();\n      for (let x = sx - s; x < w; x += s) {\n        for (let y = sx - s; y < h; y += s) ctx.rect(x + gp / 2 + 0.5, y + gp / 2 + 0.5, inner - 1, inner - 1);\n      }\n      ctx.strokeStyle = `rgba(${r},${g},${b},0.6)`;\n      ctx.stroke();\n\n      const [hr, hg, hb] = o.hl;\n      for (const [k, v] of heat) {\n        const [i, j] = k.split(\",\").map(Number);\n        const x = sx + (i + base) * s + gp / 2;\n        const y = sx + (j + base) * s + gp / 2;\n        if (x > w || y > h || x + inner < 0 || y + inner < 0) continue;\n        const e = v * v * (3 - 2 * v);\n        ctx.fillStyle = `rgba(${hr},${hg},${hb},${0.12 * e})`;\n        ctx.fillRect(x, y, inner, inner);\n        ctx.beginPath();\n        stripes(x, y, inner, sp);\n        ctx.strokeStyle = `rgba(${hr},${hg},${hb},${0.85 * e})`;\n        ctx.lineWidth = 1.2;\n        ctx.stroke();\n        ctx.strokeStyle = `rgba(${hr},${hg},${hb},${e})`;\n        ctx.lineWidth = 1;\n        ctx.strokeRect(x + 0.5, y + 0.5, inner - 1, inner - 1);\n      }\n    };\n\n    const loop = (now: number) => {\n      draw(last ? Math.min((now - last) / 1000, 0.1) : 0);\n      last = now;\n      raf = requestAnimationFrame(loop);\n    };\n\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      w = canvas.offsetWidth;\n      h = canvas.offsetHeight;\n      canvas.width = Math.max(1, Math.round(w * dpr));\n      canvas.height = Math.max(1, Math.round(h * dpr));\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      draw(0);\n    };\n\n    const onMove = (e: PointerEvent) => {\n      const r = canvas.getBoundingClientRect();\n      mouse.x = e.clientX - r.left;\n      mouse.y = e.clientY - r.top;\n      if (!raf) draw(0);\n    };\n    const onLeave = () => {\n      mouse.x = mouse.y = -1e4;\n    };\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(canvas);\n    const io = new IntersectionObserver(([e]) => {\n      cancelAnimationFrame(raf);\n      raf = 0;\n      last = 0;\n      if (e.isIntersecting && !reduced) raf = requestAnimationFrame(loop);\n    });\n    io.observe(canvas);\n    redraw.current = () => {\n      if (!raf) draw(0);\n    };\n    area.addEventListener(\"pointermove\", onMove, { passive: true });\n    area.addEventListener(\"pointerleave\", onLeave);\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      redraw.current = undefined;\n      area.removeEventListener(\"pointermove\", onMove);\n      area.removeEventListener(\"pointerleave\", onLeave);\n    };\n  }, []);\n\n  const mask = \"radial-gradient(ellipse 80% 80% at 50% 50%, #000 25%, transparent 100%)\";\n  return (\n    <canvas\n      ref={ref}\n      aria-hidden\n      className={className}\n      style={{\n        position: \"absolute\",\n        inset: 0,\n        width: \"100%\",\n        height: \"100%\",\n        pointerEvents: \"none\",\n        maskImage: mask,\n        WebkitMaskImage: mask,\n      }}\n    />\n  );\n}\n"}]}