Gravity Stars Background
animate-ui/components-backgrounds-gravity-starsFreeComponent
A background component featuring a subtle yet engaging animated gravity stars effect.
Install it
npx shadcn@latest add https://animate-ui.com/r/components-backgrounds-gravity-stars.jsonSource
1'use client';23import * as React from 'react';45import { cn } from '@/lib/utils';67type MouseGravity = 'attract' | 'repel';8type GlowAnimation = 'instant' | 'ease' | 'spring';9type StarsInteractionType = 'bounce' | 'merge';1011type GravityStarsProps = {12 starsCount?: number;13 starsSize?: number;14 starsOpacity?: number;15 glowIntensity?: number;16 glowAnimation?: GlowAnimation;17 movementSpeed?: number;18 mouseInfluence?: number;19 mouseGravity?: MouseGravity;20 gravityStrength?: number;21 starsInteraction?: boolean;22 starsInteractionType?: StarsInteractionType;23} & React.ComponentProps<'div'>;2425type Particle = {26 x: number;27 y: number;28 vx: number;29 vy: number;30 size: number;31 opacity: number;32 baseOpacity: number;33 mass: number;34 glowMultiplier?: number;35 glowVelocity?: number;36};3738function GravityStarsBackground({39 starsCount = 75,40 starsSize = 2,41 starsOpacity = 0.75,42 glowIntensity = 15,43 glowAnimation = 'ease',44 movementSpeed = 0.3,45 mouseInfluence = 100,46 mouseGravity = 'attract',47 gravityStrength = 75,48 starsInteraction = false,49 starsInteractionType = 'bounce',50 className,51 ...props52}: GravityStarsProps) {53 const containerRef = React.useRef<HTMLDivElement | null>(null);54 const canvasRef = React.useRef<HTMLCanvasElement | null>(null);55 const animRef = React.useRef<number | null>(null);56 const starsRef = React.useRef<Particle[]>([]);57 const mouseRef = React.useRef<{ x: number; y: number }>({ x: 0, y: 0 });58 const [dpr, setDpr] = React.useState(1);59 const [canvasSize, setCanvasSize] = React.useState({60 width: 800,61 height: 600,62 });6364 const readColor = React.useCallback(() => {65 const el = containerRef.current;66 if (!el) return '#ffffff';67 const cs = getComputedStyle(el);68 return cs.color || '#ffffff';69 }, []);7071 const initStars = React.useCallback(72 (w: number, h: number) => {73 starsRef.current = Array.from({ length: starsCount }).map(() => {74 const angle = Math.random() * Math.PI * 2;75 const speed = movementSpeed * (0.5 + Math.random() * 0.5);76 return {77 x: Math.random() * w,78 y: Math.random() * h,79 vx: Math.cos(angle) * speed,80 vy: Math.sin(angle) * speed,81 size: Math.random() * starsSize + 1,82 opacity: starsOpacity,83 baseOpacity: starsOpacity,84 mass: Math.random() * 0.5 + 0.5,85 glowMultiplier: 1,86 glowVelocity: 0,87 };88 });89 },90// … 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 |
