Stars Background
animate-ui/components-backgrounds-starsFreeComponent
An interactive background featuring animated dots of varying sizes and speeds, simulating a dynamic and immersive starry space effect.
Install it
npx shadcn@latest add https://animate-ui.com/r/components-backgrounds-stars.jsonSource
1'use client';23import * as React from 'react';4import {5 type HTMLMotionProps,6 motion,7 useMotionValue,8 useSpring,9 type SpringOptions,10 type Transition,11} from 'motion/react';1213import { cn } from '@/lib/utils';1415type StarLayerProps = HTMLMotionProps<'div'> & {16 count: number;17 size: number;18 transition: Transition;19 starColor: string;20};2122function generateStars(count: number, starColor: string) {23 const shadows: string[] = [];24 for (let i = 0; i < count; i++) {25 const x = Math.floor(Math.random() * 4000) - 2000;26 const y = Math.floor(Math.random() * 4000) - 2000;27 shadows.push(`${x}px ${y}px ${starColor}`);28 }29 return shadows.join(', ');30}3132function StarLayer({33 count = 1000,34 size = 1,35 transition = { repeat: Infinity, duration: 50, ease: 'linear' },36 starColor = '#fff',37 className,38 ...props39}: StarLayerProps) {40 const [boxShadow, setBoxShadow] = React.useState<string>('');4142 React.useEffect(() => {43 setBoxShadow(generateStars(count, starColor));44 }, [count, starColor]);4546 return (47 <motion.div48 data-slot="star-layer"49 animate={{ y: [0, -2000] }}50 transition={transition}51 className={cn('absolute top-0 left-0 w-full h-[2000px]', className)}52 {...props}53 >54 <div55 className="absolute bg-transparent rounded-full"56 style={{57 width: `${size}px`,58 height: `${size}px`,59 boxShadow: boxShadow,60 }}61 />62 <div63 className="absolute bg-transparent rounded-full top-[2000px]"64 style={{65 width: `${size}px`,66 height: `${size}px`,67 boxShadow: boxShadow,68 }}69 />70 </motion.div>71 );72}7374type StarsBackgroundProps = React.ComponentProps<'div'> & {75 factor?: number;76 speed?: number;77 transition?: SpringOptions;78 starColor?: string;79 pointerEvents?: boolean;80};8182function StarsBackground({83 children,84 className,85 factor = 0.05,86 speed = 50,87 transition = { stiffness: 50, damping: 20 },88 starColor = '#fff',89 pointerEvents = true,90 ...props91}: StarsBackgroundProps) {92 const offsetX = useMotionValue(1);93 const offsetY = useMotionValue(1);9495 const springX = useSpring(offsetX, transition);96 const springY = useSpring(offsetY, transition);9798 const handleMouseMove = React.useCallback(99 (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => {100 const centerX = window.innerWidth / 2;101 const centerY = window.innerHeight / 2;102// … truncated
What it pulls in
npm packages
Files it writes
More from Animate UI
All 580 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Avatar Groupcomponents-animate-avatar-group | Animate UI | Components | Free | 1 dep | |
| Codecomponents-animate-code | Animate UI | Components | Free | 1 dep | |
| Code Tabscomponents-animate-code-tabs | Animate UI | Components | Free | 1 dep | |
| Cursorcomponents-animate-cursor | Animate UI | Components | Free | no deps | |
| GitHub Stars Wheelcomponents-animate-github-stars-wheel | Animate UI | Components | Free | 1 dep | |
| Tabscomponents-animate-tabs | Animate UI | Components | Free | no deps | |
| Tooltipcomponents-animate-tooltip | Animate UI | Components | Free | 1 dep | |
| Bubble Backgroundcomponents-backgrounds-bubble | Animate UI | Components | Free | 1 dep |
