An animated background that quantizes a smooth field into two colors with an 8x8 Bayer matrix. It renders to a 2D canvas at low resolution and scales up with crisp pixels. Default colors come from theme tokens and update when the theme changes.
Preview
Switch the pattern, pixel size and speed.
Ship in pixels
Bayer dithering over a moving field
Installation
Usage
The background fills the nearest positioned parent. Put content after it with relative positioning.
import { DitherBackground } from "@/components/ui/dither-background"
export default function Hero() {
return (
<section className="relative h-96 overflow-hidden">
<DitherBackground />
<div className="relative">Your content</div>
</section>
)
}Examples
Noise
Drifting value noise with a soft vertical gradient.
<DitherBackground pattern="noise" pixelSize={2} />Radial pulse
Rings that spread from the center, with large pixels.
<DitherBackground pattern="radial" pixelSize={6} speed={0.6} />Custom colors
Pass any CSS colors or theme variables. Here the field uses muted tones for a quiet section background.
<DitherBackground
colorFront="var(--muted-foreground)"
colorBack="var(--muted)"
scale={1.6}
/>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| pattern | "waves" | "noise" | "radial" | "waves" | Shape of the smooth field before quantization |
| pixelSize | number | 3 | Size of one dither cell in CSS pixels |
| speed | number | 1 | Animation speed multiplier, 0 freezes the field |
| colorFront | string | "var(--primary)" | Color of lit cells |
| colorBack | string | "var(--background)" | Color of unlit cells and the fallback background |
| scale | number | 1 | Field zoom, higher values give larger shapes |
| fps | number | 30 | Frame rate cap |
| className | string | - | Additional CSS classes for the wrapper |
Features
- Pauses when hidden: the loop stops off screen and in a background tab
- Bounded cost: work depends on CSS pixels, not devicePixelRatio, and the cell count is capped
- Theme aware: colors are re-read when the html class or color scheme changes
- Reduced motion: with prefers-reduced-motion a single static frame is drawn