Gravity Stars
Interactive canvas background with gravity-driven star particles, mouse attraction/repulsion, click-to-spawn, and star collisions.
Installation
Pro components require registry authentication. Add your Unlumen UI Pro key as UNLUMEN_LICENSE_KEY in your .env.local file and follow the setup guide.
File Structure
Usage
import { GravityStarsBackground } from "@/components/unlumen-ui/gravity-stars";<GravityStarsBackground className="absolute inset-0" />Interactions
- Hover — stars are attracted or repelled based on
mouseGravity - Click — spawns a burst of new stars at the cursor position
- Star collisions — stars can bounce off or merge with each other when
starsInteractionis enabled
Examples
Repel on hover
<GravityStarsBackground mouseGravity="repel" gravityStrength={120} />Star collisions with merge
<GravityStarsBackground starsInteraction starsInteractionType="merge" />Custom colors and density
<GravityStarsBackground
starsCount={150}
starsColor="#60a5fa"
starsSize={3}
glowIntensity={25}
/>API Reference
GravityStarsBackground
starsCount?number75Initial number of stars.
starsSize?number2Maximum radius of each star in pixels.
starsOpacity?number0.75Base opacity of the stars.
starsColor?string"#ffcad4"Color of the stars (any CSS color).
glowIntensity?number15Shadow blur intensity applied to each star.
glowAnimation?'instant' | 'ease' | 'spring'"ease"How the glow responds to mouse proximity — instant snap, smooth ease, or spring physics.
movementSpeed?number0.3Base movement speed of the stars.
mouseInfluence?number100Radius in pixels around the cursor that affects stars.
mouseGravity?'attract' | 'repel'"attract"Whether the cursor attracts or repels stars.
gravityStrength?number75Strength of the gravitational force applied to stars.
starsInteraction?booleanfalseWhether stars interact with each other on collision.
starsInteractionType?'bounce' | 'merge'"bounce"Collision behavior between stars — rigid bounce or soft merge with glow.
...props?React.ComponentProps<"div">—All other props are forwarded to the root div.
Notes
- The canvas uses
devicePixelRatio(capped at 2) for crisp rendering on retina displays. The DPR is applied to canvas dimensions and draw calls automatically. - An
IntersectionObserverpauses the animation loop when the component is off-screen — no wasted frames. - A
ResizeObserveron the container keeps the canvas sized correctly inside flex/grid layouts. No need to handle resize yourself. - Clicking anywhere spawns 4 new stars at the cursor position. Stars are capped at 80 total — older stars are removed first when the cap is exceeded.
- Star collisions are O(n²) — with
starsInteractionenabled and highstarsCount, performance can drop. KeepstarsCountunder ~100 when collisions are on. - The
glowAnimationprop controls how the glow responds to mouse proximity:"instant"snaps immediately,"ease"interpolates smoothly (15% per frame),"spring"uses a damped spring with velocity tracking. - Stars wrap around edges (toroidal space) — a star leaving the right side reappears on the left.
- All physics run in logical (CSS) pixel space, not canvas pixels. Mouse coordinates are converted from
getBoundingClientRectoffsets.
Credits
- Thanks to Animate UI for the original inspiration and base component idea.
- Credits to Brett Jackson for the initial Framer component concept.
Keep in mind
Most components on this site are inspired by or recreated from existing work across the web. I'm not here to take credit; just to learn, experiment, and sometimes push things a bit further. If something looks familiar and I forgot to mention you, reach out and I'll fix that right away.
This component is part of Unlumen UI [ Pro ]
Includes advanced motion, patterns, and premium support.