An interactive background featuring animated dots of varying sizes and speeds, simulating a dynamic and immersive starry space effect.
Installation
Usage
import { StarsBackground } from '@/components/animate-ui/backgrounds/stars';
export function Hero() {
return (
<section className="relative h-[480px] overflow-hidden">
<StarsBackground className="absolute inset-0" />
</section>
);
}Animation
- Three layers of stars (1, 2 and 3px) scroll upwards in a loop at different speeds, for depth: the smallest crosses 2000px in
speedseconds, the others take two and three times longer. They move with hardware-acceleratedtransforms. - The sky shifts away from the mouse on a spring (
factor,transition), relative to the center of the container, for parallax. On touch screens, a tap shifts it too. - The loops pause off screen and while the tab is hidden.
- Under reduced motion, the stars hold still and there is no parallax.
API Reference
StarsBackground
| Prop | Type | Default |
|---|---|---|
factor? | number | 0.05 |
speed? | number | 50 |
transition? | SpringOptions | { stiffness: 50, damping: 20 } |
starColor? | string | '#fff' |
pointerEvents? | boolean | true |
...props? | React.ComponentProps<'div'> | - |
Credits
- Credits to umangladani for the inspiration