Installation
pnpm dlx shadcn@latest add https://dithercn.dev/r/galaxy.jsonUsage
import { Galaxy } from "@/components/dithercn/galaxy";
export function Hero() {
return (
<section className="relative isolate min-h-[480px]">
<Galaxy className="absolute inset-0 -z-10" />
<h1>Your headline</h1>
</section>
);
}The galaxy is a flat disc seen at an angle: tilt leans it away from you and angle turns its long axis on the screen. A solid core glows at the centre and fades softly into the disc. Two bright logarithmic arms wind out of it with dark gaps between them and taper to a point, with knots of bright stars along them and a thin dark dust lane running along each one's inner edge, from the core out to near the tip. Sparse stars sit fixed behind it, spaced evenly, the brightest few drawn as small crosses.
The whole pattern turns as one piece, so the spiral keeps its shape however long the page stays open.
By default the palette maps brightness to colour, from paper to the last colour, and the demo draws it in one ink like the other backgrounds. Set colorField to colour the dots by place instead: the last colour where the core's glow outshines the disc, the first ink colour on the arms, and one palette colour for each background star. The handover is a short ordered ramp at the edge of the core; a middle colour, as in ["--dither-paper", "blue", "purple", "orange"], steps through it on the way.
Examples
Nearly edge-on
tilt={76} lays the disc almost flat, so the arms blur into a smooth bright lens and the dust on the near side cuts a dark lane across the core, with the bulge glowing on both sides of it, as in the Sombrero galaxy. The lane fades in past about 60°; at the default tilt and flatter it stays hidden. Three arms and a tighter twist fill the lens out evenly.
Beside a headline
The galaxy sits high on the right and turns counterclockwise, and the text sits in the clear space on the left. Below 560px wide, the example moves the center to the top middle and the text to the bottom, so the disc stays in frame and clear of the copy.
Props
| Prop | Type | Default |
|---|---|---|
| arms How many spiral arms, from 2 to 5. | Type: number | Default 2 |
| twist How tightly the arms wind, from 0.3 (open) to 2 (tight). | Type: number | Default 1 |
| tilt How far the disc leans away, in degrees: 0 is face-on, 80 nearly edge-on. | Type: number | Default 55 |
| angle Direction of the disc's long axis, in degrees as in CSS: 0 is up, 90 right. | Type: number | Default 70 |
| direction Which way the disc turns on screen. The arms always trail behind the turn. | Type: "clockwise" | "counterclockwise" | Default "clockwise" |
| center Centre of the galaxy, as fractions of the box from the top left. | Type: [x: number, y: number] | Default [0.5, 0.5] |
| size Radius of the galaxy, out to its arm tips, as a share of the box's shorter side. At the default, a centred galaxy fits whole in a box of any shape. | Type: number | Default 0.48 |
| dust Strength of the dark dust lanes, from 0 to 1. | Type: number | Default 0.8 |
| stars How many background stars, from 0 to 3. They never move. | Type: number | Default 1 |
| colorField Colour the dots by place: the last palette colour at the core, the first ink colour on the arms. Give the palette three or more colours. | Type: boolean | Default false |
| palette Colours from paper to ink: palette names such as "blue", CSS variable names, or any CSS colour. | Type: [DitherColor, DitherColor, ...DitherColor[]] | Default ["--dither-paper", "--dither-ink"] |
| matrix The threshold pattern. See Dither engine. | Type: "bayer2" | "bayer4" | "bayer8" | "blue-noise" | Default "bayer4" |
| cell Size of one dither cell in CSS pixels. | Type: number | Default --dither-cell |
| speed How fast it turns. At 1 the galaxy takes about 90 seconds for a full turn. | Type: number | Default 1 |
| animate false stops the motion: a still frame if it starts off, the current frame if it was moving. | Type: boolean | Default true |
| className Size and position. The galaxy fills this box. | Type: string | Default – |
Notes
- The galaxy is decorative and hidden from assistive technology. Put real text on top of it, not inside it.
- With reduced motion turned on, it draws one still frame and stops.
- It pauses when scrolled out of view or when the tab is hidden, and releases its WebGL context when far offscreen. Without WebGL it shows a still SVG of the same tilted spiral, with its arms, core, dust lanes and a fixed scatter of stars, in the same colours.
- On a light theme the paper is light and the galaxy is drawn in ink, so the dust lanes read as pale gaps, like a printed negative. Pass a dark first palette colour for a dark sky on any theme.