Join our Discord Community

React Galaxy Background

React galaxy background with WebGL shaders. Features procedural star generation, mouse interaction, and cosmic animations.

Powered by

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:

Loading component...

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

PropTypeDefaultDescription
focal[number, number][0.5, 0.5]Focal point for star field center
rotation[number, number][1.0, 0.0]Galaxy rotation matrix components
starSpeednumber0.5Speed of star movement and animation
densitynumber1Star density multiplier
hueShiftnumber140Global hue shift in degrees
disableAnimationbooleanfalsePause all animations
speednumber1.0Overall animation speed multiplier
mouseInteractionbooleantrueEnable mouse-responsive effects
glowIntensitynumber0.3Star glow and flare intensity
saturationnumber0.0Color saturation multiplier
mouseRepulsionbooleantrueEnable mouse repulsion field
twinkleIntensitynumber0.3Star twinkling animation strength
rotationSpeednumber0.1Automatic rotation speed
repulsionStrengthnumber2Mouse repulsion field strength
autoCenterRepulsionnumber0Central repulsion field strength
transparentbooleantrueEnable alpha transparency
classNamestring-Additional CSS classes
...propsHTMLAttributes<HTMLDivElement>-All standard div props

Cosmic Themes

Different parameter combinations for various galactic aesthetics:

ThemeHue ShiftSaturationGlowDescription
Classic140°0.00.3Traditional white starfield
Nebula240°0.50.5Blue-purple cosmic dust
Golden60°0.40.4Warm golden galaxy
Aurora120°0.60.6Green-cyan stellar winds
Deep Space300°0.30.2Magenta cosmic background

Motion Settings

Different animation combinations for various cosmic effects:

StyleStar SpeedRotationTwinkleDescription
Peaceful0.20.020.2Gentle, meditative movement
Standard0.50.050.3Balanced cosmic animation
Active0.80.10.5Dynamic stellar motion
Hyperspeed1.50.20.8Fast, 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

Questions developers actually ask