Meteors
An animated meteor shower background for React: a few shooting stars with fine tails streak across the container at staggered intervals. Seeded so server and client render the same sky, compositor-animated, and paused off-screen.
- Background
- Shooting stars
- Seeded layout
- Pauses off-screen
- Reduced-motion safe
Launch week
One new feature every day, Monday to Friday.
import { Meteors } from "@/components/dusk/backgrounds/meteors";
export default function MeteorsDemo() {
return (
<div className="relative flex h-80 w-full items-center justify-center overflow-hidden rounded-xl border border-white/10 bg-background">
<Meteors />
<div className="relative px-6 text-center">
<h3 className="text-3xl font-semibold tracking-tight text-white sm:text-4xl">Launch week</h3>
<p className="mx-auto mt-2 max-w-sm text-sm leading-relaxed text-white/60">One new feature every day, Monday to Friday.</p>
</div>
</div>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/meteors.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/backgrounds/meteors.tsx.meteors.tsx "use client"; import * as React from "react"; import { useReducedMotion } from "motion/react"; import { cn } from "@/lib/utils"; export interface MeteorsProps extends React.ComponentProps<"div"> { /** Number of meteors. */ count?: number; /** Direction of travel in degrees: 0 is right, 90 is straight down. */ angle?: number; /** Any CSS color for the tails. Defaults to a soft white. */ color?: string; /** Speed multiplier. */ speed?: number; /** Average tail length in px. */ tailLength?: number; /** Number of faint, still stars scattered behind the meteors. */ stars?: number; /** Change it for a different, but stable, sky. */ seed?: number; } type Meteor = { /** Position across the direction of travel, 0–1 of the container's extent. */ lane: number; tail: number; pace: number; /** Pause between passes, in seconds at speed 1. */ rest: number; /** Where in its cycle the meteor starts, 0–1. */ phase: number; }; type Star = { left: string; top: string; size: number; opacity: number }; /** Base meteor speed in px per second. */ const PX_PER_SECOND = 520; function mulberry32(seed: number) { let a = seed >>> 0; return () => { a = (a + 0x6d2b79f5) >>> 0; let t = a; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; } function makeSky(count: number, stars: number, tailLength: number, seed: number) { const rand = mulberry32(seed * 374761393 + 11); const n = Math.max(0, Math.round(count)); const meteors: Meteor[] = []; for (let i = 0; i < n; i++) { meteors.push({ // Stratified lanes spread meteors evenly, jittered so they don't look gridded. lane: (i + 0.15 + rand() * 0.7) / Math.max(n, 1), tail: Math.max(8, tailLength * (0.6 + rand() * 0.7)), pace: 0.7 + rand() * 0.6, rest: 1 + rand() * 5, phase: rand(), }); } // Shuffle so neighbouring lanes don't fire in order. for (let i = meteors.length - 1; i > 0; i--) { const j = Math.floor(rand() * (i + 1)); [meteors[i], meteors[j]] = [meteors[j], meteors[i]]; } const field: Star[] = Array.from({ length: Math.max(0, Math.round(stars)) }, () => ({ left: `${(rand() * 100).toFixed(2)}%`, top: `${(rand() * 100).toFixed(2)}%`, size: rand() < 0.85 ? 1 : 2, opacity: Math.round((0.1 + rand() * 0.3) * 100) / 100, })); return { meteors, field }; } /** * Shooting stars that streak diagonally across the container at staggered * intervals, each with a small bright head and a fading tail. Fills its nearest * positioned parent. * * Every meteor and star is derived from a seed, so the server and client * render the same sky. Paths are fitted to the container so each meteor * crosses it whatever the angle or aspect ratio. Animations run on the * compositor, pause off-screen and in hidden tabs, and reduced motion shows a * still frame instead. */ export function Meteors({ count = 6, angle = 135, color = "rgb(255 255 255 / 0.7)", speed = 1, tailLength = 100, stars = 20, seed = 1, className, style, ...props }: MeteorsProps) { const reduceMotion = useReducedMotion(); const rootRef = React.useRef<HTMLDivElement>(null); const meteorRefs = React.useRef<(HTMLSpanElement | null)[]>([]); const { meteors, field } = React.useMemo( () => makeSky(count, stars, tailLength, seed), [count, stars, tailLength, seed] ); React.useEffect(() => { const root = rootRef.current; if (!root) return; const nodes = meteorRefs.current; const rad = (angle * Math.PI) / 180; const dx = Math.cos(rad); const dy = Math.sin(rad); // Perpendicular to the direction of travel. const nx = -dy; const ny = dx; const rate = Math.max(speed, 0.05); const place = (x: number, y: number) => `translate(${x.toFixed(1)}px, ${y.toFixed(1)}px) rotate(${angle}deg)`; let animations: (Animation | null)[] = []; let inView = true; const active = () => inView && !document.hidden; const build = () => { const { width, height } = root.getBoundingClientRect(); if (!width || !height) return; const times = animations.map((a) => a?.currentTime ?? null); for (const a of animations) a?.cancel(); animations = []; // Project the corners onto both axes to find the band every path must cross. const corners = [ [0, 0], [width, 0], [0, height], [width, height], ]; const across = corners.map(([x, y]) => x * nx + y * ny); const along = corners.map(([x, y]) => x * dx + y * dy); const sMin = Math.min(...across); const sMax = Math.max(...across); const tMin = Math.min(...along); const tMax = Math.max(...along); meteors.forEach((m, i) => { const el = nodes[i]; if (!el) return; const s = sMin + m.lane * (sMax - sMin); // Start with the whole meteor outside, end once its tail has left. const t0 = tMin - 8; const t1 = tMax + m.tail + 8; const at = (f: number) => { const t = t0 + (t1 - t0) * f; return place(s * nx + t * dx, s * ny + t * dy); }; if (reduceMotion) { // A still frame: a third of the meteors frozen mid-flight. el.style.transform = at(0.3 + m.phase * 0.4); el.style.opacity = i % 3 === 0 ? "0.6" : "0"; animations.push(null); return; } const travel = ((t1 - t0) / (PX_PER_SECOND * m.pace * rate)) * 1000; const total = travel + (m.rest * 1000) / rate; const p = travel / total; const animation = el.animate( [ { offset: 0, transform: at(0), opacity: 0 }, { offset: p * 0.08, transform: at(0.08), opacity: 0.8 }, { offset: p * 0.7, transform: at(0.7), opacity: 0.8 }, { offset: p, transform: at(1), opacity: 0 }, { offset: 1, transform: at(1), opacity: 0 }, ], { duration: total, iterations: Infinity, easing: "linear" } ); // Keep each meteor's place in its cycle across resizes; stagger them on first run. const prev = times[i]; animation.currentTime = typeof prev === "number" ? prev % total : m.phase * total; if (!active()) animation.pause(); animations.push(animation); }); }; const sync = () => { for (const a of animations) { if (!a) continue; if (active()) a.play(); else a.pause(); } }; build(); const resizeObserver = new ResizeObserver(build); resizeObserver.observe(root); const intersectionObserver = new IntersectionObserver(([entry]) => { inView = entry.isIntersecting; sync(); }); intersectionObserver.observe(root); document.addEventListener("visibilitychange", sync); return () => { for (const a of animations) a?.cancel(); for (const el of nodes) { if (!el) continue; el.style.transform = ""; el.style.opacity = "0"; } resizeObserver.disconnect(); intersectionObserver.disconnect(); document.removeEventListener("visibilitychange", sync); }; }, [meteors, angle, speed, reduceMotion]); const head = `color-mix(in oklab, ${color} 35%, white)`; return ( <div ref={rootRef} aria-hidden data-slot="meteors" className={cn("pointer-events-none absolute inset-0 overflow-hidden", className)} style={style} {...props} > {field.map((star, i) => ( <span key={`s${i}`} className="absolute rounded-full bg-white" style={{ left: star.left, top: star.top, width: star.size, height: star.size, opacity: star.opacity }} /> ))} {meteors.map((m, i) => ( <span key={i} ref={(el) => { meteorRefs.current[i] = el; }} className="absolute left-0 top-0 block h-px rounded-full will-change-transform" style={{ width: m.tail, marginLeft: -m.tail, marginTop: -0.5, // The head sits at the transform origin, so rotating swings the tail behind it. transformOrigin: "100% 50%", opacity: 0, background: `linear-gradient(to right, transparent, color-mix(in oklab, ${color} 40%, transparent) 65%, ${color})`, }} > <span className="absolute right-0 top-1/2 block size-[2px] -translate-y-1/2 translate-x-1/2 rounded-full" style={{ background: head }} /> </span> ))} </div> ); }
The sky fills its nearest positioned parent, so give the parent relative and overflow-hidden, and put your content after it with relative so it sits on top.
Usage
import { Meteors } from "@/components/dusk/backgrounds/meteors"
export function Hero() {
return (
<section className="relative overflow-hidden">
<Meteors />
<div className="relative">{/* your content */}</div>
</section>
)
}Examples
Angle & color
angle is the direction of travel in degrees (0 is right, 90 is straight down). Paths are fitted to the container, so every meteor crosses it at any angle. tailLength sets the average tail.
import { Meteors } from "@/components/dusk/backgrounds/meteors";
const skies = [
{ label: "angle={100}", props: { angle: 100, color: "rgb(125 211 252 / 0.7)", seed: 3 } },
{ label: "angle={200}", props: { angle: 200, tailLength: 140, seed: 8 } },
];
export default function MeteorsAngles() {
return (
<div className="grid w-full gap-4 sm:grid-cols-2">
{skies.map((sky) => (
<div
key={sky.label}
className="relative flex h-56 items-end overflow-hidden rounded-xl border border-white/10 bg-background p-4"
>
<Meteors {...sky.props} />
<span className="relative font-mono text-xs text-white/70">{sky.label}</span>
</div>
))}
</div>
);
}Inside a card
Fewer meteors, fewer stars and a slower speed suit small surfaces like a pricing card.
Pro
$24/mo
- Unlimited projects
- Priority builds
- Custom domains
import { Check } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Meteors } from "@/components/dusk/backgrounds/meteors";
export default function MeteorsCard() {
return (
<div className="flex w-full justify-center p-4">
<div className="relative w-full max-w-xs overflow-hidden rounded-xl border border-white/10 bg-card p-6">
<Meteors count={4} stars={10} speed={0.8} tailLength={80} />
<div className="relative">
<p className="text-sm font-medium text-white">Pro</p>
<p className="mt-1 text-3xl font-semibold text-white">
$24<span className="text-base font-normal text-white/50">/mo</span>
</p>
<ul className="mt-4 space-y-2 text-sm text-white/70">
{["Unlimited projects", "Priority builds", "Custom domains"].map((item) => (
<li key={item} className="flex items-center gap-2">
<Check className="size-4 text-white/50" aria-hidden />
{item}
</li>
))}
</ul>
<Button className="mt-6 w-full">Upgrade</Button>
</div>
</div>
</div>
);
}API Reference
Any other div prop is passed to the root. Meteors are animated with the Web Animations API on transform and opacity only, so they run on the compositor.
| Prop | Type | Default | Description |
|---|---|---|---|
| count | number | 6 | Number of meteors. |
| angle | number | 135 | Direction of travel in degrees: 0 is right, 90 is down. |
| color | string | "rgb(255 255 255 / 0.7)" | Tail color. Any CSS color. |
| speed | number | 1 | Speed multiplier; also shortens the pause between passes. |
| tailLength | number | 100 | Average tail length in px. |
| stars | number | 20 | Faint still stars scattered behind the meteors. |
| seed | number | 1 | Seed for a different, but stable, sky. |
| className | string | — | Merged onto the absolutely positioned root. |
Accessibility
- The sky is decorative: it is
aria-hiddenandpointer-events-none, so it never reaches screen readers or blocks clicks. - With prefers-reduced-motion a few meteors are shown frozen mid-flight instead of moving.
- Animations pause while the sky is off-screen or the tab is hidden.
- Meteors cross the foreground; keep text on a card or use fewer, slower meteors behind long copy.