React Galaxy Background
React galaxy background with WebGL shaders. Features procedural star generation, mouse interaction, and cosmic animations.
Building cosmic interfaces?
Join our Discord community for help from other developers working with WebGL shaders and astronomical effects.
Ever wanted to recreate that mesmerizing cosmic atmosphere you see in space documentaries, sci-fi films, or astronomical visualizations? Those infinite starfields that seem to stretch into eternity, complete with twinkling stars, color variations, and that sense of vast cosmic depth? The way stars appear to move through three-dimensional space, each with its own brightness, color temperature, and subtle animation patterns? Most star effects use simple particle systems or static images, but they lack the sophisticated procedural algorithms that create truly immersive galactic experiences.
This React component creates that captivating galaxy effect using WebGL shaders with OGL library, featuring procedural star generation, realistic color distributions, multi-layered depth systems, and interactive mouse controls that bring the cosmos to your interface.
Dynamic star field with cosmic depth and interaction
Watch thousands of procedurally generated stars twinkle and move with realistic astronomical behavior:
Built for React applications with TypeScript and optimized WebGL rendering. Uses advanced GLSL shaders with hash-based randomization algorithms for authentic star distribution while providing extensive controls for colors, density, motion, and interactive effects. Perfect for space-themed applications, scientific visualizations, or any project needing that infinite cosmic aesthetic.
Installation
npx shadcn@latest add https://www.shadcn.io/registry/galaxy.json
npx shadcn@latest add https://www.shadcn.io/registry/galaxy.json
pnpm dlx shadcn@latest add https://www.shadcn.io/registry/galaxy.json
bunx shadcn@latest add https://www.shadcn.io/registry/galaxy.json
Why simple particle systems feel artificial
Developers create star effects with basic particles and consider them cosmic. Maybe they add some random positioning or simple twinkling animations. The problem? Basic particle systems can't capture the complex, mathematically-driven nature of real astronomical distributions that emerge from spatial hashing, color temperature physics, and multi-layered depth perception.
Shader-based galaxy rendering with WebGL changes everything. It taps into the fundamental mathematics of space visualization—the way hash functions create natural randomization, how HSV color space mimics stellar temperature, the hypnotic beauty that emerges from layered depth systems and procedural generation. The algorithms use real astronomical mathematics and color science to create authentic cosmic experiences.
This component handles complex astronomical mathematics automatically. Hash-based star placement, stellar color temperature calculations, multi-layer depth systems, and interactive physics all work together seamlessly. The result feels like peering through an actual telescope into deep space.
Features
- WebGL shader rendering with advanced GLSL for real-time star field generation
- Procedural star placement using hash-based randomization for natural cosmic distribution
- Stellar color physics with HSV color space and temperature-based color generation
- Multi-layered depth system with parallax effects and distance-based scaling
- Interactive mouse controls with repulsion fields, smooth tracking, and dynamic responses
- Twinkling animations with period-based oscillation and randomized timing patterns
- Configurable cosmic parameters with precise control over density, colors, and motion
- Performance optimization with efficient shader compilation and WebGL context management
- Responsive design with automatic canvas scaling and device pixel ratio handling
- Alpha transparency support for seamless integration with various backgrounds
API Reference
Galaxy
Prop | Type | Default | Description |
---|---|---|---|
focal | [number, number] | [0.5, 0.5] | Focal point for star field center |
rotation | [number, number] | [1.0, 0.0] | Galaxy rotation matrix components |
starSpeed | number | 0.5 | Speed of star movement and animation |
density | number | 1 | Star density multiplier |
hueShift | number | 140 | Global hue shift in degrees |
disableAnimation | boolean | false | Pause all animations |
speed | number | 1.0 | Overall animation speed multiplier |
mouseInteraction | boolean | true | Enable mouse-responsive effects |
glowIntensity | number | 0.3 | Star glow and flare intensity |
saturation | number | 0.0 | Color saturation multiplier |
mouseRepulsion | boolean | true | Enable mouse repulsion field |
twinkleIntensity | number | 0.3 | Star twinkling animation strength |
rotationSpeed | number | 0.1 | Automatic rotation speed |
repulsionStrength | number | 2 | Mouse repulsion field strength |
autoCenterRepulsion | number | 0 | Central repulsion field strength |
transparent | boolean | true | Enable alpha transparency |
className | string | - | Additional CSS classes |
...props | HTMLAttributes<HTMLDivElement> | - | All standard div props |
Cosmic Themes
Different parameter combinations for various galactic aesthetics:
Theme | Hue Shift | Saturation | Glow | Description |
---|---|---|---|---|
Classic | 140° | 0.0 | 0.3 | Traditional white starfield |
Nebula | 240° | 0.5 | 0.5 | Blue-purple cosmic dust |
Golden | 60° | 0.4 | 0.4 | Warm golden galaxy |
Aurora | 120° | 0.6 | 0.6 | Green-cyan stellar winds |
Deep Space | 300° | 0.3 | 0.2 | Magenta cosmic background |
Motion Settings
Different animation combinations for various cosmic effects:
Style | Star Speed | Rotation | Twinkle | Description |
---|---|---|---|---|
Peaceful | 0.2 | 0.02 | 0.2 | Gentle, meditative movement |
Standard | 0.5 | 0.05 | 0.3 | Balanced cosmic animation |
Active | 0.8 | 0.1 | 0.5 | Dynamic stellar motion |
Hyperspeed | 1.5 | 0.2 | 0.8 | Fast, energetic movement |
Shader architecture
Procedural Generation: Uses hash-based randomization with spatial distribution algorithms to create natural star placement patterns without repetition or obvious tiling artifacts.
Color Temperature Physics: Implements HSV color space mathematics with stellar temperature modeling to generate realistic star colors based on astronomical color-magnitude relationships.
Multi-Layer Depth: Combines multiple star layers with different scales and fade patterns to create convincing three-dimensional depth perception and parallax effects.
Interactive Physics: Transforms mouse coordinates to UV space with repulsion field calculations using distance-based falloff functions for natural interaction behaviors.
Performance considerations
WebGL Optimization: Uses efficient shader compilation with optimized uniform management, minimal GPU state changes, and hash-based calculations for consistent 60fps performance.
Fragment Shader Efficiency: Implements mathematical optimizations including loop unrolling, efficient hash functions, and reduced branching to minimize GPU computation overhead.
Memory Management: Properly handles WebGL context lifecycle, canvas management, and event listener cleanup to prevent memory leaks during component unmounting.
Responsive Scaling: Uses device pixel ratio awareness with efficient canvas resizing and automatic viewport calculations for optimal visual quality across displays.
Common gotchas
WebGL Context: Component requires WebGL-enabled browser. Gracefully handles context creation failures but won't render cosmic effects without WebGL support.
Performance Scaling: High density values combined with complex interactions can impact performance. Monitor frame rates when increasing star count or effect complexity.
Color Range: HSV calculations expect hue values in degrees (0-360). Values outside this range may cause unexpected color behavior or mathematical errors.
Container Sizing: Component fills its container completely. Ensure parent element has defined dimensions for proper star field rendering and aspect ratios.
Mouse Coordinates: Uses normalized screen coordinates for consistent interaction behavior across different container sizes and device pixel ratios.
Transparency Handling: When transparent=true, component uses alpha blending. Ensure proper CSS background handling for optimal visual integration.
You might also like
Particles Background
Floating particle systems with physics simulation
Shooting Stars
Animated meteor trails with cosmic effects
Aurora Background
Northern lights effects with atmospheric gradients
Sparkles
Twinkling sparkle effects with magical animations
Questions developers actually ask
React Flickering Grid Background
React flickering grid background with CRT monitor effects. Canvas-based subtle visual noise animations using TypeScript and shadcn/ui styling.
Gradient Animation Background
React animated gradient background with flowing orbs and mouse tracking. CSS blend modes create aurora-like effects with TypeScript and shadcn/ui.