# Dither Hero
*URL: https://www.grettech.com/components/dither-hero*

A bold product hero on a panel of pixel-dithered plasma in your brand colour that lights up around the cursor, with a screenshot overlapping its edge.

- Ordered (Bayer) dithering on canvas, scaled up crisp
- Lights up around the cursor; a quiet zone behind the copy
- Glass-framed screenshot overlapping the panel
- 24fps cap, paused off-screen; still for reduced motion

## Install with the shadcn CLI
```bash
npx shadcn@latest add https://www.grettech.com/api/registry/dither-hero
```
With its demo: `npx shadcn@latest add https://www.grettech.com/api/registry/dither-hero-demo`

**Dependencies:** npm: motion, clsx, tailwind-merge

## Manual installation
**Step 1: Install dependencies**
Install the packages this component uses and the shadcn/ui components it builds on. It also needs the `cn` helper in `lib/utils.ts`, which `npx shadcn@latest init` adds.

```bash
npm install motion clsx tailwind-merge
```

**Step 2: Copy the source code**
Add these files to your project components directory.

File: `components/ui/dither-hero.tsx`
```tsx
"use client"

import { cn } from "@/lib/utils"
import { motion, MotionConfig } from "motion/react"
import * as React from "react"

export interface DitherHeroProps extends Omit<
  React.HTMLAttributes<HTMLElement>,
  "title"
> {
  /** Small label above the title */
  eyebrow?: string
  title: React.ReactNode
  description?: string
  /** Calls to action, under the description */
  children?: React.ReactNode
  /** Screenshot that overlaps the bottom of the panel */
  image?: { src: string; alt: string }
  /** Panel and dot colours of the dither, as hex */
  colors?: [string, string]
  /** Size of one dither pixel, in CSS pixels */
  pixelSize?: number
  /** Animation speed; 0 holds the pattern still */
  speed?: number
  /** Light the pattern up around the cursor */
  interactive?: boolean
}

const REDUCED = "(prefers-reduced-motion: reduce)"

/** Rise into place after `delay` seconds */
const rise = (delay: number) => ({
  initial: { opacity: 0, y: 16 },
  animate: { opacity: 1, y: 0 },
  transition: { duration: 0.7, ease: [0.2, 0.7, 0.2, 1] as const, delay },
})

// 8×8 ordered-dither (Bayer) thresholds, 0–1
const BAYER = (() => {
  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,
  ]
  return Float32Array.from(m, (v) => (v + 0.5) / 64)
})()

/** "#1235e8" → packed RGBA for an ImageData buffer (little-endian) */
function pack(hex: string) {
  const h = hex.replace("#", "")
  const full =
    h.length === 3 ? [...h].map((c) => c + c).join("") : h.slice(0, 6)
  const n = parseInt(full, 16) || 0
  const r = (n >> 16) & 255
  const g = (n >> 8) & 255
  const b = n & 255
  return (255 << 24) | (b << 16) | (g << 8) | r
}

/**
 * A plasma of slow sine waves, thresholded against a Bayer matrix and drawn
 * at 1/pixelSize resolution, then scaled up crisp with image-rendering:
 * pixelated. 24fps cap, paused off-screen and in background tabs; one
 * still frame for reduced motion.
 */
function DitherCanvas({
  colors,
  pixelSize,
  speed,
  pointer,
}: {
  colors: [string, string]
  pixelSize: number
  speed: number
  pointer: React.RefObject<{ x: number; y: number } | null>
}) {
  const wrap = React.useRef<HTMLDivElement>(null)
  const canvas = React.useRef<HTMLCanvasElement>(null)
  const [panel, dot] = colors

  React.useEffect(() => {
    const el = canvas.current
    const box = wrap.current
    const ctx = el?.getContext("2d")
    if (!el || !box || !ctx) return
    const off = pack(panel)
    const on = pack(dot)
    const px = Math.max(2, Math.round(pixelSize))
    let cols = 0
    let rows = 0
    let image: ImageData | null = null
    let pixels: Uint32Array | null = null
    let frame = 0
    let last = 0
    let visible = true
    const still = window.matchMedia(REDUCED).matches || speed === 0

    const draw = (time: number) => {
      if (!image || !pixels) return
      const t = (time / 1000) * speed
      const p = pointer.current
      const pxp = p ? p.x / px : -1e4
      const pyp = p ? p.y / px : -1e4
      const reach = 90 / px
      // Narrow panels stack the copy taller, so the quiet zone grows
      const narrow = cols * px < 640
      const fallX = narrow ? 0.6 : 2.4
      const fallY = narrow ? 2.2 : 5
      for (let y = 0; y < rows; y++) {
        const ny = y / rows
        const rowWave = Math.sin(ny * 5 - t * 0.45)
        // A quiet zone behind the copy, so the text stays readable
        const calmY = (ny - (narrow ? 0.38 : 0.32)) ** 2 * fallY
        for (let x = 0; x < cols; x++) {
          const nx = x / cols
          let v =
            0.56 +
            0.22 * Math.sin(nx * 6 + t * 0.6) +
            0.18 * Math.sin(rowWave * 1.6 + nx * 3) +
            0.12 * Math.sin((nx + ny) * 9 + t * 0.8) -
            0.8 * Math.exp(-((nx - 0.5) * (nx - 0.5) * fallX + calmY))
          v -= ny * 0.35
          if (p) {
            const dx = x - pxp
            const dy = y - pyp
            v += 0.55 * Math.exp(-(dx * dx + dy * dy) / (reach * reach))
          }
          pixels[y * cols + x] = v > BAYER[(y & 7) * 8 + (x & 7)] ? on : off
        }
      }
      ctx.putImageData(image, 0, 0)
    }

    const resize = () => {
      const rect = box.getBoundingClientRect()
      cols = Math.max(1, Math.ceil(rect.width / px))
      rows = Math.max(1, Math.ceil(rect.height / px))
      el.width = cols
      el.height = rows
      image = ctx.createImageData(cols, rows)
      pixels = new Uint32Array(image.data.buffer)
      draw(last)
    }
    const loop = (time: number) => {
      frame = requestAnimationFrame(loop)
      if (!visible || document.hidden || time - last < 1000 / 24) return
      last = time
      draw(time)
    }

    const sizer = new ResizeObserver(resize)
    sizer.observe(box)
    const seen = new IntersectionObserver(([entry]) => {
      visible = entry?.isIntersecting ?? true
    })
    seen.observe(box)
    resize()
    if (!still) frame = requestAnimationFrame(loop)
    return () => {
      cancelAnimationFrame(frame)
      sizer.disconnect()
      seen.disconnect()
    }
  }, [panel, dot, pixelSize, speed, pointer])

  return (
    <div ref={wrap} aria-hidden="true" className="absolute inset-0">
      <canvas
        ref={canvas}
        className="absolute inset-0 size-full"
        style={{ imageRendering: "pixelated", backgroundColor: panel }}
      />
    </div>
  )
}

/**
 * A bold product hero on a panel of pixel-dithered plasma in your brand
 * colour, which lights up around the cursor. The title sits in white on
 * the panel, and an optional screenshot in a glass frame overlaps its
 * bottom edge. All text is server-rendered; Motion brings the copy in and
 * lifts the screenshot into place.
 */
export const DitherHero = React.forwardRef<HTMLElement, DitherHeroProps>(
  (
    {
      eyebrow,
      title,
      description,
      children,
      image,
      colors = ["#1235e8", "#6d9bff"],
      pixelSize = 4,
      speed = 1,
      interactive = true,
      className,
      ...props
    },
    ref
  ) => {
    const pointer = React.useRef<{ x: number; y: number } | null>(null)
    return (
      <section
        ref={ref}
        className={cn(
          "w-full bg-background px-3 py-3 sm:px-4 sm:py-4",
          className
        )}
        {...props}
      >
        {/* Reduced motion keeps the fades but drops the movement */}
        <MotionConfig reducedMotion="user">
          <div
            className={cn(
              "relative isolate overflow-hidden rounded-[28px] px-6 pt-20 text-center text-white sm:pt-28",
              image ? "pb-44 sm:pb-64" : "pb-20 sm:pb-28"
            )}
            style={{ backgroundColor: colors[0] }}
            onPointerMove={(e) => {
              if (!interactive) return
              const box = e.currentTarget.getBoundingClientRect()
              pointer.current = {
                x: e.clientX - box.left,
                y: e.clientY - box.top,
              }
            }}
            onPointerLeave={() => {
              pointer.current = null
            }}
          >
            <div
              aria-hidden="true"
              className="pointer-events-none absolute inset-0 -z-10"
            >
              <DitherCanvas
                colors={colors}
                pixelSize={pixelSize}
                speed={speed}
                pointer={pointer}
              />
              {/* Vignette and a faint inner edge */}
              <div className="absolute inset-0 bg-[radial-gradient(ellipse_80%_70%_at_50%_35%,transparent_40%,rgba(0,0,0,0.35))]" />
              <div className="absolute inset-0 rounded-[28px] ring-1 ring-white/15 ring-inset" />
            </div>

            <div className="mx-auto flex max-w-3xl flex-col items-center">
              {eyebrow && (
                <motion.p
                  {...rise(0)}
                  className="mb-6 rounded-full bg-white/10 px-3 py-1 text-xs font-medium text-white ring-1 ring-white/25 backdrop-blur-md"
                >
                  {eyebrow}
                </motion.p>
              )}
              <motion.h1
                {...rise(0.08)}
                className="text-4xl leading-[1.05] font-semibold tracking-[-0.03em] text-balance [text-shadow:0_2px_24px_rgba(0,0,0,0.25)] sm:text-6xl md:text-7xl"
              >
                {title}
              </motion.h1>
              {description && (
                <motion.p
                  {...rise(0.18)}
                  className="mt-6 max-w-xl text-base text-pretty text-white/80 sm:text-lg"
                >
                  {description}
                </motion.p>
              )}
              {children && (
                <motion.div
                  {...rise(0.28)}
                  className="mt-9 flex flex-wrap items-center justify-center gap-3"
                >
                  {children}
                </motion.div>
              )}
            </div>
          </div>

          {image?.src && (
            // The screenshot rises up over the panel's edge
            <motion.div
              initial={{ opacity: 0, y: 48, scale: 0.97 }}
              animate={{ opacity: 1, y: 0, scale: 1 }}
              transition={{
                type: "spring",
                stiffness: 90,
                damping: 20,
                delay: 0.35,
              }}
              className="relative z-10 mx-auto -mt-32 max-w-5xl px-2 sm:-mt-52 sm:px-6"
            >
              <div className="rounded-[26px] bg-background/40 p-2 shadow-[0_40px_100px_-30px_rgba(0,0,0,0.5)] ring-1 ring-foreground/10 backdrop-blur-xl sm:p-2.5">
                {/* eslint-disable-next-line @next/next/no-img-element */}
                <img
                  src={image.src}
                  alt={image.alt}
                  className="aspect-video w-full rounded-[18px] object-cover object-top ring-1 ring-foreground/10"
                />
              </div>
            </motion.div>
          )}
        </MotionConfig>
      </section>
    )
  }
)
DitherHero.displayName = "DitherHero"

```

**Step 3: Framework Adjustments (Optional)**
If you are manually copying this code into a Vite or Remix project (without using the CLI), make sure to remove the `"use client";` directive from the very top of the file.

## Props
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| `title` | `React.ReactNode` | - | Yes | The main headline, shown in white on the panel. |
| `eyebrow` | `string` | - | No | Small label above the title. |
| `description` | `string` | - | No | Supporting text under the title. |
| `children` | `React.ReactNode` | - | No | Calls to action, under the description. |
| `image` | `{ src: string; alt: string }` | - | No | Screenshot that overlaps the bottom of the panel. |
| `image.src` | `string` | - | Yes | - |
| `image.alt` | `string` | - | Yes | - |
| `colors` | `[string, string]` | `["#1235e8", "#6d9bff"]` | No | Panel and dot colours of the dither, as hex. |
| `pixelSize` | `number` | `4` | No | Size of one dither pixel, in CSS pixels. |
| `speed` | `number` | `1` | No | Animation speed; 0 holds the pattern still. |
| `interactive` | `boolean` | `true` | No | Light the pattern up around the cursor. |
| `className` | `string` | - | No | Extra classes for the section. |

## Usage
```tsx
"use client"

import { DitherHero } from "@/components/ui/dither-hero"

export default function DitherHeroDemo() {
  return (
    <DitherHero
      eyebrow="Ledgerline for finance teams"
      title="Month-end close in days, not weeks"
      description="Ledgerline matches transactions, chases missing receipts and drafts the reports, so your team reviews instead of retyping."
      image={{
        src:
          "https://images.unsplash.com/photo-1551288049-bebda4e38f71?w=800&q=75&auto=format",
        alt: "Ledgerline reconciliation dashboard",
      }}
      className="rounded-3xl"
    >
      <a
        href="#start"
        className="rounded-full bg-white px-5 py-2.5 text-sm font-medium text-neutral-900 shadow-sm transition-transform hover:scale-[1.02]"
      >
        Book a walkthrough
      </a>
      <a
        href="#how"
        className="rounded-full bg-white/10 px-5 py-2.5 text-sm font-medium text-white ring-1 ring-white/30 backdrop-blur-md transition-colors hover:bg-white/20"
      >
        See how it works
      </a>
    </DitherHero>
  )
}

```
