{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"bitmask-veil","type":"registry:ui","title":"Bitmask Veil","description":"A flowing 1-bit ordered-dither veil that dissolves around the cursor to reveal what is underneath.","dependencies":[],"registryDependencies":["utils"],"files":[{"path":"registry/ui/bitmask-veil.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef, type ReactNode } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface BitmaskVeilProps {\n  /** Content hidden under the veil. */\n  children?: ReactNode;\n  /** Size of one dither pixel in px. */\n  pixelSize?: number;\n  /** Veil base color (6-digit hex). */\n  color?: string;\n  /** Dithered highlight color (6-digit hex). */\n  accent?: string;\n  /** 0–1, how much of the veil is highlight. */\n  density?: number;\n  /** Px radius the cursor dissolves. */\n  radius?: number;\n  /** Ms for a revealed patch to close again. */\n  heal?: number;\n  /** Flow speed of the veil pattern. */\n  speed?: number;\n  className?: string;\n}\n\n// 8x8 Bayer matrix, normalised to 0..1 thresholds.\nconst BAYER = (() => {\n  const m = [0, 32, 8, 40, 2, 34, 10, 42, 48, 16, 56, 24, 50, 18, 58, 26, 12, 44, 4, 36, 14, 46, 6, 38, 60, 28, 52, 20, 62, 30, 54, 22, 3, 35, 11, 43, 1, 33, 9, 41, 51, 19, 59, 27, 49, 17, 57, 25, 15, 47, 7, 39, 13, 45, 5, 37, 63, 31, 55, 23, 61, 29, 53, 21];\n  return Float32Array.from(m, (v) => (v + 0.5) / 64);\n})();\n\n// Packs a hex color as little-endian RGBA for a Uint32Array view of ImageData.\nconst pack = (hex: string) => {\n  const n = parseInt(hex.replace(\"#\", \"\").padEnd(6, \"0\").slice(0, 6), 16);\n  return (255 << 24) | ((n & 255) << 16) | (n & 0xff00) | ((n >> 16) & 255);\n};\n\nexport function BitmaskVeil({\n  children,\n  pixelSize = 4,\n  color = \"#0d0d0f\",\n  accent = \"#c6ff3d\",\n  density = 0.4,\n  radius = 150,\n  heal = 1400,\n  speed = 1,\n  className,\n}: BitmaskVeilProps) {\n  const rootRef = useRef<HTMLDivElement>(null);\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n\n  useEffect(() => {\n    const root = rootRef.current!;\n    const canvas = canvasRef.current!;\n    const ctx = canvas.getContext(\"2d\")!;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const ink = pack(color);\n    const hi = pack(accent);\n    let gw = 0;\n    let gh = 0;\n    let img: ImageData;\n    let px = new Uint32Array(0);\n    let reveal = new Float32Array(0);\n    let rowWarp = new Float32Array(0);\n    let colWarp = new Float32Array(0);\n    const mouse = { x: -1e4, y: -1e4 };\n    let raf = 0;\n    let last = 0;\n    let visible = true;\n\n    const frame = (now: number) => {\n      const dt = last ? Math.min(now - last, 64) : 16;\n      last = now;\n      const t = reduced ? 0 : (now / 1000) * speed;\n      const r = radius / pixelSize;\n      const mx = mouse.x / pixelSize;\n      const my = mouse.y / pixelSize;\n      const decay = Math.max(0, 1 - dt / heal);\n      for (let y = 0; y < gh; y++) rowWarp[y] = 1.6 * Math.sin(y * 0.045 + t * 0.35);\n      for (let x = 0; x < gw; x++) colWarp[x] = 1.6 * Math.sin(x * 0.03 - t * 0.25);\n      const lift = density * 2 - 1;\n      for (let y = 0; y < gh; y++) {\n        const dy = y - my;\n        const brow = (y & 7) << 3;\n        for (let x = 0; x < gw; x++) {\n          const i = y * gw + x;\n          const dx = x - mx;\n          const d = Math.sqrt(dx * dx + dy * dy);\n          // Soft hole around the pointer; the trail heals back over `heal` ms.\n          const hole = d < r ? 1 - (d / r) ** 2 : 0;\n          const rv = Math.max(reveal[i] * decay, hole);\n          reveal[i] = rv;\n          const th = BAYER[brow + (x & 7)];\n          if (rv > th) {\n            px[i] = 0;\n            continue;\n          }\n          const g = 0.5 + 0.25 * (Math.sin(x * 0.022 + t * 0.7 + rowWarp[y]) + Math.sin(y * 0.03 - t * 0.5 + colWarp[x]));\n          px[i] = g * g + lift * 0.5 > th ? hi : ink;\n        }\n      }\n      ctx.putImageData(img, 0, 0);\n    };\n\n    const loop = (now: number) => {\n      frame(now);\n      raf = visible && !reduced ? requestAnimationFrame(loop) : 0;\n    };\n\n    const ro = new ResizeObserver(() => {\n      gw = Math.max(1, Math.ceil(root.offsetWidth / pixelSize));\n      gh = Math.max(1, Math.ceil(root.offsetHeight / pixelSize));\n      canvas.width = gw;\n      canvas.height = gh;\n      canvas.style.width = `${gw * pixelSize}px`;\n      canvas.style.height = `${gh * pixelSize}px`;\n      img = ctx.createImageData(gw, gh);\n      px = new Uint32Array(img.data.buffer);\n      reveal = new Float32Array(gw * gh);\n      rowWarp = new Float32Array(gh);\n      colWarp = new Float32Array(gw);\n      frame(performance.now());\n    });\n    ro.observe(root);\n\n    const onMove = (e: PointerEvent) => {\n      const b = root.getBoundingClientRect();\n      mouse.x = e.clientX - b.left;\n      mouse.y = e.clientY - b.top;\n      if (reduced) frame(performance.now());\n    };\n    const onLeave = () => {\n      mouse.x = mouse.y = -1e4;\n    };\n\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible && !raf && !reduced) raf = requestAnimationFrame(loop);\n    });\n    io.observe(root);\n    root.addEventListener(\"pointermove\", onMove, { passive: true });\n    root.addEventListener(\"pointerleave\", onLeave);\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      root.removeEventListener(\"pointermove\", onMove);\n      root.removeEventListener(\"pointerleave\", onLeave);\n    };\n  }, [pixelSize, color, accent, density, radius, heal, speed]);\n\n  return (\n    <div ref={rootRef} className={cn(\"relative overflow-hidden\", className)}>\n      {children}\n      <canvas\n        ref={canvasRef}\n        aria-hidden\n        style={{ position: \"absolute\", left: 0, top: 0, pointerEvents: \"none\", imageRendering: \"pixelated\" }}\n      />\n    </div>\n  );\n}\n"}]}