{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "shad-fx-dither",
  "title": "shad-fx: dither renderer",
  "author": "Piergiorgio Gonni <https://www.sekei.design>",
  "description": "DitherCanvas: fills its positioned parent with a pixelated canvas that draws any effect as Bayer-dithered cells.",
  "registryDependencies": [
    "sekeidesign/shad-fx/shad-fx-engine",
    "sekeidesign/shad-fx/use-prefers-reduced-motion",
    "utils"
  ],
  "files": [
    {
      "path": "registry/shad-fx/dither/painter.ts",
      "content": "import { blendOver, clamp01, type Renderer, type Rgb, rgba, type Surface } from \"../engine\";\n\n/**\n * 4×4 Bayer thresholds in 0–1, indexed by `((y & 3) << 2) | (x & 3)`.\n *\n * The ordered-dither rendering here — this matrix, the low-resolution backing\n * canvas scaled up pixelated, and the two-alpha-tier fill — derives from\n * dither-kit (MIT, https://github.com/Boring-Software-Inc/dither-kit). The shadcn\n * CLI drops a file's leading comment on install, so the notice lives here.\n * Full attribution: https://github.com/sekeidesign/shad-fx/blob/main/NOTICE.md\n */\nexport const BAYER4: readonly number[] = [\n\t0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5,\n].map((v) => (v + 0.5) / 16);\n\n/**\n * Pixel writer over one ImageData. Every write lands in a Uint32 view and the\n * frame is blitted with a single putImageData, which is what keeps a few\n * thousand cells a frame cheap — one fillRect per cell would not be.\n */\nexport class Painter implements Surface {\n\treadonly image: ImageData;\n\tprivate readonly px: Uint32Array;\n\n\tconstructor(\n\t\treadonly cols: number,\n\t\treadonly rows: number,\n\t) {\n\t\tthis.image = new ImageData(cols, rows);\n\t\tthis.px = new Uint32Array(this.image.data.buffer);\n\t}\n\n\tclear() {\n\t\tthis.px.fill(0);\n\t}\n\n\tthreshold(x: number, y: number) {\n\t\treturn BAYER4[((y & 3) << 2) | (x & 3)];\n\t}\n\n\tinside(x: number, y: number) {\n\t\treturn x >= 0 && y >= 0 && x < this.cols && y < this.rows;\n\t}\n\n\tset(x: number, y: number, [r, g, b]: Rgb, alpha: number) {\n\t\tif (!this.inside(x, y) || alpha <= 0) return;\n\t\tthis.px[y * this.cols + x] = rgba(r, g, b, Math.round(clamp01(alpha) * 255));\n\t}\n\n\t/** Source-over onto whatever is already in the cell. */\n\tblend(x: number, y: number, color: Rgb, alpha: number) {\n\t\tif (!this.inside(x, y) || alpha <= 0) return;\n\t\tblendOver(this.px, y * this.cols + x, color, alpha);\n\t}\n\n\t/**\n\t * Ordered dither in one colour, at two alpha tiers rather than one alpha and\n\t * a hole. Every cell in range is painted; clearing the Bayer threshold only\n\t * decides whether it lands at full strength or a faint one. Punching actual\n\t * holes makes the falloff crawl, and on a pale surface the gaps read as\n\t * white specks instead of a fade.\n\t */\n\tdither(x: number, y: number, density: number, color: Rgb, gain = 1) {\n\t\tif (density <= 0.004) return;\n\t\tconst d = clamp01(density);\n\t\tconst lit = d > this.threshold(x, y);\n\t\tconst alpha = (lit ? 0.35 + 0.65 * d : 0.12 * d) * gain;\n\t\tthis.blend(x, y, color, alpha);\n\t}\n\n\t/** A sparkle: the centre pixel plus four arms that flare in at `arms` (0–1). */\n\tglint(x: number, y: number, color: Rgb, alpha: number, arms = 0) {\n\t\tthis.blend(x, y, color, alpha);\n\t\tif (arms <= 0) return;\n\t\tconst a = alpha * arms;\n\t\tthis.blend(x - 1, y, color, a);\n\t\tthis.blend(x + 1, y, color, a);\n\t\tthis.blend(x, y - 1, color, a);\n\t\tthis.blend(x, y + 1, color, a);\n\t}\n}\n\nexport interface DitherOptions {\n\t/** CSS px per dither cell. */\n\tcell?: number;\n\tmaxCols?: number;\n\tmaxRows?: number;\n}\n\n/**\n * Sizes a canvas's backing store to the CSS box divided by `cell`, so each\n * cell is one pixel scaled up without smoothing, and blits the `Painter` onto\n * it in one call.\n */\nexport class DitherRenderer implements Renderer {\n\tprivate painter: Painter | null = null;\n\tprivate readonly ctx: CanvasRenderingContext2D;\n\tprivate readonly cell: number;\n\tprivate readonly maxCols: number;\n\tprivate readonly maxRows: number;\n\n\tconstructor(\n\t\tprivate readonly canvas: HTMLCanvasElement,\n\t\t{ cell = 2, maxCols = 640, maxRows = 400 }: DitherOptions = {},\n\t) {\n\t\tconst ctx = canvas.getContext(\"2d\");\n\t\tif (!ctx) throw new Error(\"DitherRenderer: 2d context unavailable\");\n\t\tthis.ctx = ctx;\n\t\tthis.cell = cell;\n\t\tthis.maxCols = maxCols;\n\t\tthis.maxRows = maxRows;\n\t}\n\n\tresize(width: number, height: number) {\n\t\tconst cols = Math.min(this.maxCols, Math.max(4, Math.round(width / this.cell)));\n\t\tconst rows = Math.min(this.maxRows, Math.max(4, Math.round(height / this.cell)));\n\t\tif (this.painter && this.painter.cols === cols && this.painter.rows === rows) return null;\n\t\tthis.canvas.width = cols;\n\t\tthis.canvas.height = rows;\n\t\tthis.painter = new Painter(cols, rows);\n\t\treturn this.painter;\n\t}\n\n\tpresent() {\n\t\tif (this.painter) this.ctx.putImageData(this.painter.image, 0, 0);\n\t}\n}\n",
      "type": "registry:lib",
      "target": "@components/shad-fx/dither/painter.ts"
    },
    {
      "path": "registry/shad-fx/dither/dither-canvas.tsx",
      "content": "\"use client\";\n\nimport { usePrefersReducedMotion } from \"@/hooks/use-prefers-reduced-motion\";\nimport { cn } from \"@/lib/utils\";\nimport type { Fx } from \"../engine\";\nimport { useFxEngine } from \"../use-fx-engine\";\nimport { DitherRenderer } from \"./painter\";\n\nexport interface DitherCanvasProps {\n\t/** From `useFx`. A different effect starts from scratch; the canvas is kept. */\n\teffect: Fx;\n\t/** Eases the effect in and out. Defaults to on. */\n\tactive?: boolean;\n\tcell?: number;\n\tseed?: number;\n\t/** Ceiling on the backing grid, so a large box can't cost a large frame. */\n\tmaxCols?: number;\n\tmaxRows?: number;\n\tclassName?: string;\n}\n\n/**\n * Fills its positioned parent with a pixelated dither canvas running `effect`.\n * The backing resolution is the box divided by `cell`, scaled back up without\n * smoothing, so every dither cell is a crisp square of `cell` CSS px.\n */\nexport function DitherCanvas({\n\teffect,\n\tactive = true,\n\tcell = 2,\n\tseed = 1,\n\tmaxCols,\n\tmaxRows,\n\tclassName,\n}: DitherCanvasProps) {\n\tconst reduced = usePrefersReducedMotion();\n\tconst { wrapRef, canvasRef } = useFxEngine(\n\t\t(canvas) => new DitherRenderer(canvas, { cell, maxCols, maxRows }),\n\t\t{ effect, active, reduced, seed },\n\t\t[cell, maxCols, maxRows],\n\t);\n\n\treturn (\n\t\t<div\n\t\t\tref={wrapRef}\n\t\t\taria-hidden\n\t\t\tclassName={cn(\n\t\t\t\t\"pointer-events-none absolute inset-0 overflow-hidden\",\n\t\t\t\tclassName,\n\t\t\t)}\n\t\t>\n\t\t\t<canvas\n\t\t\t\tref={canvasRef}\n\t\t\t\tclassName=\"absolute inset-0 size-full\"\n\t\t\t\tstyle={{ imageRendering: \"pixelated\" }}\n\t\t\t/>\n\t\t</div>\n\t);\n}\n",
      "type": "registry:ui",
      "target": "@components/shad-fx/dither/dither-canvas.tsx"
    },
    {
      "path": "registry/shad-fx/dither/index.ts",
      "content": "export { DitherCanvas, type DitherCanvasProps } from \"./dither-canvas\";\nexport {\n\tBAYER4,\n\ttype DitherOptions,\n\tDitherRenderer,\n\tPainter,\n} from \"./painter\";\n",
      "type": "registry:lib",
      "target": "@components/shad-fx/dither/index.ts"
    }
  ],
  "docs": "Pair it with any effect: `const fx = useFx(fire, { height: 0.6 })`, then `<DitherCanvas effect={fx} />`. Options passed to useFx update in place without restarting the simulation. The ordered-dither rendering derives from dither-kit (MIT, https://github.com/Boring-Software-Inc/dither-kit). See https://github.com/sekeidesign/shad-fx/blob/main/NOTICE.md",
  "categories": [
    "shad-fx",
    "dither",
    "canvas"
  ],
  "type": "registry:ui"
}