Skip to content

feat(dither): Dither layer with colors, shader and 17 patterns on one WebGL context - #1048

Merged
dillionverma merged 9 commits into
mainfrom
bb/expand-and-optimize-ditherfield-component-thr_afpfn4ddmv
Nov 26, 2026
Merged

dillionverma merged 9 commits into
mainfrom
bb/expand-and-optimize-ditherfield-component-thr_afpfn4ddmv

Conversation

@dillionverma

@dillionverma dillionverma commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

TL;DR

Dither is a layer. It fills its positioned parent and takes no children. It has 17 built-in patterns, an image or video src, and a new shader prop for your own GLSL. One colors prop takes one color or a list of stops. All instances share one WebGL2 context.

<section className="relative isolate">
  <Dither pattern="horizon" colors="var(--primary)" className="-z-10" />
  <h1>Your content</h1>
</section>

API

export type DitherPattern =
  | "horizon" | "glow" | "aurora" | "waves" | "clouds"
  | "sunset" | "planet" | "moonrise" | "orbit" | "meridian" | "eclipse"
  | "ridgeline" | "contour" | "dunes" | "corona" | "orb" | "veil"

export const DITHER_PATTERNS: readonly DitherPattern[]

type DitherSource =
  | { pattern?: DitherPattern; src?: never; shader?: never }
  | { src: string; pattern?: never; shader?: never }
  | { shader: string; pattern?: never; src?: never }

export type DitherProps = Omit<React.ComponentProps<"div">, "children" | "color"> &
  DitherSource & {
    colors?: string | readonly string[] // one color, or stops from dim to bright
    matrix?: 2 | 4 | 8                  // palette patterns default to their own
    dotSize?: number                    // 1
    intensity?: number                  // 0.75
    speed?: number                      // 1
    fps?: number                        // 30
    interactive?: boolean               // false
  }

The root is <div data-slot="dither" data-state="pending | ready | fallback" aria-hidden> with pointer-events-none absolute inset-0 overflow-hidden rounded-[inherit]. It forwards ref and merges className.

pattern, src and shader are exclusive at the type level. dither.type-test.tsx has a @ts-expect-error line for each pair and for children.

Shader contract

float field(vec2 uv, float t) {
  return 0.5 + 0.5 * sin((uv.x + uv.y) * 12.0 + t);
}
  • uv runs from 0 to 1 from the bottom-left corner. t is seconds times speed. The return value is brightness from 0 to 1, mapped onto colors.
  • The field can read uSize, uAspect, uDot, uPointer, uPointerOn, noise, fbm and ss.
  • Each unique shader string compiles once and all instances share the program. The cache key is the string itself.
  • On a compile error, the console shows Dither: the shader did not compile. and the error with its line number in your source. The layer stays blank and gets data-state="fallback".

All 17 built-in patterns now use this same contract. Light patterns dither in colors over a clear background. Palette patterns keep their own ground, colors and matrix, and colors recolors them.

Files to review

Core:

  • apps/www/registry/magicui/dither.tsx. The component, the shared WebGL2 hub, the 17 field shaders and the shader error report.
  • apps/www/registry/magicui/dither.type-test.tsx. The type test for the exclusive source props.
  • apps/www/content/docs/components/dither.mdx. Docs in the same layout as the other component pages.
  • apps/www/registry/example/dither-*.tsx. The 6 examples moved to the layer API, plus the new dither-shader-demo.

Generated by pnpm build:registry: registry.json, apps/www/registry.json, apps/www/registry/__index__.tsx, apps/www/public/r/*, apps/www/public/registry.json and apps/www/public/llms*.txt.

Docs page

  1. Examples. Card Backgrounds, Mouse Interaction, Text Mask, Image and Custom Shader (dither-shader-demo, warped ribbons in --primary).
  2. Patterns (last example). All 17 patterns as tiles from DITHER_PATTERNS. All tiles share one WebGL context.
  3. Usage, Props, Shader and Notes.

Notes and limits

  • DITHER_PATTERNS comes from a "use client" module. A server component gets a client reference, so .map fails there. dither-patterns-demo is a client component for this reason.
  • Palette patterns clamp the field to 0 to 1 before the gain.
  • The light color mix is premultiplied. With one color, the stops fade from 35% to full opacity.
  • The CSS fallback uses the last color stop.
  • The repaint callback was empty for still fields. It now redraws the frame.

Rendering fixes from earlier commits

  • A still field (speed={0} or an image src) stayed blank when the shader compiled after the first draw. The retry now runs until the first frame is on screen.
  • If createProgram() returns null, the field is marked failed and the CSS fallback shows.
  • The noise table uses % 256 instead of & 255.

Performance

These numbers come from an earlier commit on this branch, before the layer API. The hub and the context model did not change after that commit.

All instances share one WebGL2 context, and shaders compile in the background (KHR_parallel_shader_compile). Before, each instance made its own context and compiled two programs on every scroll-in.

Metric (docs page, 1440x900, DPR 2) Before After
Live WebGL contexts, peak while scrolling 8 1
Contexts created over load plus one full scroll 15 1
Main-thread time in WebGL and getContext calls 104 ms 31 ms
Page frame time, median and p95 16.7 and 16.7 ms 16.7 and 16.7 ms

For one large hero instance, the CPU busy time per frame is about the same as main. At 1920x1080, DPR 2 and 30 fps, the medians are 1.43 ms on this branch and 1.39 ms on main, and the run ranges overlap. A field that is alone on the shared context shows that canvas directly and skips the copy. With several fields, each field copies its own rectangle.

Kept as before: reduced motion (still frame), off-screen pause, hidden-tab pause, SSR safety, DPR and resize handling, and the CSS fallback when WebGL2 is missing or the context is lost.

Checks

  • eslint, tsc --noEmit, prettier --check and registry-deps --check pass. The pnpm build:registry output is committed.
  • rg -n "gruvbox|nord-orbit|kanagawa|eclipse-rose|eclipse-nord|palette=|color=" apps/www/registry apps/www/content/docs/components/dither.mdx finds no Dither match.
  • When a @ts-expect-error line is removed from the type test, tsc gives TS2322.
  • Headless Chrome on the current docs page, light and dark: every example draws, and all 17 pattern tiles draw with 1 live WebGL context. The before and after shots of the demo, card, text and patterns examples match, apart from the animation phase.
  • A broken shader (ribbn for ribbon) logs line 6: 'ribbn' : undeclared identifier and sets data-state="fallback".

@vercel

vercel Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
magicui Ready Ready Preview Nov 26, 2026 12:54am UTC

@dillionverma dillionverma changed the title feat(dither-field): add 12 palette patterns and a shared WebGL context feat(dither): rename Dither Field to Dither, add 12 palette patterns and a shared WebGL context Nov 5, 2026
@dillionverma dillionverma changed the title feat(dither): rename Dither Field to Dither, add 12 palette patterns and a shared WebGL context feat(dither)!: Dither layer with colors, shader and 17 patterns on one WebGL context Nov 8, 2026
@dillionverma dillionverma changed the title feat(dither)!: Dither layer with colors, shader and 17 patterns on one WebGL context feat(dither): Dither layer with colors, shader and 17 patterns on one WebGL context Nov 12, 2026
@dillionverma
dillionverma marked this pull request as ready for review November 26, 2026 00:49
dillionverma and others added 9 commits November 25, 2026 19:51
Add 12 palette patterns to DitherField: gruvbox-dusk, nord-orbit,
kanagawa-moonrise, orbit, meridian, eclipse-rose, ridgeline, contour,
dunes, eclipse-nord, orb and veil. Each one ships its own colors,
Bayer matrix and dot size. New optional props: palette and matrix.
dotSize is now optional. Existing props and patterns are unchanged.

All instances share one WebGL2 context and compile shaders in the
background, so the docs page holds 1 live context instead of up to 8.

Add 12 installable examples (dither-<pattern>) and a Patterns section
in the docs page. Regenerate the registry files.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… single-field cost

- Rename DitherField to Dither: component, registry item, docs page and
  the nine examples (dither-field-* to dither-*). No alias, no redirect.
- Retry the pending draw until the first frame presents, so a still field
  (speed 0 or an image src) no longer stays blank while a shader compiles.
- Mark the entry failed when createProgram returns null, so the CSS
  fallback shows.
- Replace the bitwise AND in the noise table with a modulo.
- A field that has the shared hub to itself now shows the hub canvas
  directly. Only a second field makes each one copy its rectangle into a
  2D canvas. This removes the drawImage copy from the one-hero case.
- Veil footer example: text on top, beams below, so the dither fills the card.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
When two fields drop to one, the survivor now moves from its 2D copy
back onto the shared WebGL canvas. releaseHub() calls attach() on the
last member. Renderer setMedia and dispose are optional, so the scene
renderer no longer carries empty method bodies.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…erns grid

Examples now show card backgrounds, a mouse interaction on/off toggle,
a text mask and an image. A Patterns section shows all 17 patterns as
same-size tiles that share one WebGL context. Remove the 12 per-pattern
examples and the monochrome, brand and colors demos.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… in patterns

- `interactive` now defaults to false. The card and mouse demos set it.
- The patterns grid has 2 columns, and each tile uses `dotSize={1}`.
- Add a `uUnit` uniform so scene shapes keep their size at any dot size.
- Brighten the orbit ring so it reaches the accent color at tile size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Dither is now a layer that fills its positioned parent and takes no
children. One `colors` prop replaces the single-color and palette props.
A `shader` prop takes GLSL that defines `float field(vec2 uv, float t)`.
Each unique string compiles once. A compile error logs its line and sets
data-state="fallback". All 17 built-in patterns use the same contract.

Five palette ids change: gruvbox-dusk is sunset, nord-orbit is planet,
kanagawa-moonrise is moonrise, eclipse-rose is eclipse and eclipse-nord
is corona. The repaint callback is no longer empty. The examples move to
the layer API and a dither-shader-demo example is added.

color props, five pattern ids are renamed and dotSize defaults to 1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Main now enforces react-hooks/refs as an error (#1100). This applies the
same fix #1097 made to dither-field.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dillionverma
dillionverma force-pushed the bb/expand-and-optimize-ditherfield-component-thr_afpfn4ddmv branch from a65a55d to c096ab2 Compare November 26, 2026 00:53
@dillionverma
dillionverma merged commit a54d9ed into main Nov 26, 2026
9 checks passed
@dillionverma
dillionverma deleted the bb/expand-and-optimize-ditherfield-component-thr_afpfn4ddmv branch November 26, 2026 01:03

This branch was successfully deployed

1 active deployment
Preview — c096ab27 Deployed Nov 26, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant