React Light Rays Background
React light rays background with WebGL rendering. Features customizable ray origins, mouse interaction, pulsing effects, and volumetric lighting using OGL.
Creating cinematic lighting effects?
Join our Discord community for help from other developers working with WebGL volumetric lighting.
Remember those breathtaking moments in movies when dramatic light rays pierce through smoke or mist, creating that cinematic atmosphere that makes everything feel epic? The way sunlight streams through clouds or spotlights cut through darkness with visible beams that seem to have weight and presence? Most interfaces settle for flat gradients or simple shadows that feel lifeless. This React component recreates that volumetric lighting magic with mathematically accurate light ray simulation, complete with mouse interaction, pulsing effects, and customizable origins that make your interface feel like a movie scene.
Volumetric light rays with mouse interaction
Watch cinematic light beams emanate from customizable origins with realistic falloff:
Built for React applications with TypeScript and Next.js. Uses WebGL through OGL for hardware-accelerated volumetric rendering that maintains 60fps with complex ray calculations and distance falloffs. The mathematical lighting model creates authentic ray scattering while mouse interaction adds dynamic realism. Perfect for hero sections, cinematic interfaces, or any application needing dramatic atmospheric lighting. Seamless integration with shadcn/ui design systems.
Installation
npx shadcn@latest add https://www.shadcn.io/registry/light-rays-background.json
npx shadcn@latest add https://www.shadcn.io/registry/light-rays-background.json
pnpm dlx shadcn@latest add https://www.shadcn.io/registry/light-rays-background.json
bunx shadcn@latest add https://www.shadcn.io/registry/light-rays-background.json
Why flat lighting feels amateur
Developers add a linear gradient or drop shadow and consider the lighting complete. Maybe they throw in a subtle glow effect. The problem? Flat lighting doesn't create depth or atmosphere. It doesn't communicate drama or guide the viewer's attention like professional cinematography.
Volumetric light rays change everything. They tap into our instinctive response to dramatic lighting—the way our eyes are naturally drawn to light sources, how atmospheric perspective creates depth, the emotional impact of rays cutting through darkness. The mathematical precision of ray strength calculations and falloff functions creates lighting that feels physically authentic.
This component handles the complex WebGL shader calculations automatically. Ray origin positioning, directional vectors, distance-based falloffs, and mouse influence all work together seamlessly. The result feels like professional lighting design, not a simple CSS effect.
Features
- Volumetric ray calculation using mathematically accurate light scattering and falloff algorithms
- Eight ray origins including corners, sides, and centers for flexible light source positioning
- Mouse interaction with smooth tracking and configurable influence strength
- Pulsating effects with customizable frequency for breathing light animations
- Distance-based falloff creating realistic light attenuation over distance
- Color saturation control for fine-tuning the lighting mood and intensity
- Noise integration for organic light variation and atmospheric effects
- Ray distortion with sine wave modulation for dynamic beam movement
- Performance optimization with intersection observer and cleanup management
- TypeScript support with complete prop forwarding and type safety
API Reference
LightRaysBackground
Prop | Type | Default | Description |
---|---|---|---|
raysOrigin | RaysOrigin | "top-center" | Light source position (8 predefined locations) |
raysColor | string | "#ffffff" | Hex color for the light rays |
raysSpeed | number | 1 | Animation speed multiplier for ray movement |
lightSpread | number | 1 | Ray cone angle - higher values create wider spread |
rayLength | number | 2 | Maximum ray distance as screen width multiplier |
pulsating | boolean | false | Enable rhythmic pulsing animation |
fadeDistance | number | 1.0 | Distance-based fade intensity (0.1 to 2.0) |
saturation | number | 1.0 | Color saturation level (0.0 = grayscale, 2.0 = hyper-saturated) |
followMouse | boolean | true | Enable mouse tracking for ray direction |
mouseInfluence | number | 0.1 | Mouse influence strength (0.0 to 1.0) |
noiseAmount | number | 0.0 | Random noise intensity for organic variation |
distortion | number | 0.0 | Sine wave distortion strength for ray movement |
className | string | - | Additional CSS classes for the container |
...props | HTMLAttributes<HTMLDivElement> | - | All standard div props supported |
Ray Origins
Choose from eight strategic light source positions:
Origin | Position | Visual Effect | Use Case |
---|---|---|---|
"top-center" | Top middle (default) | Classic sunbeam from above | Hero sections, divine light |
"top-left" | Top left corner | Angular dramatic lighting | Photography-style side lighting |
"top-right" | Top right corner | Complementary side lighting | Balanced composition |
"left" | Middle left edge | Horizontal side lighting | Spotlight effects |
"right" | Middle right edge | Stage lighting from wings | Theater-style illumination |
"bottom-center" | Bottom middle | Upward stage lighting | Footer illumination |
"bottom-left" | Bottom left corner | Ground-up lighting | Dramatic under-lighting |
"bottom-right" | Bottom right corner | Corner accent lighting | Asymmetrical composition |
Effect Combinations
// Cinematic hero lighting
<LightRaysBackground
raysOrigin="top-center"
raysColor="#ffffff"
lightSpread={1.5}
pulsating={true}
followMouse={true}
/>
// Dramatic side lighting
<LightRaysBackground
raysOrigin="left"
raysColor="#ff6b35"
lightSpread={0.8}
rayLength={3.0}
distortion={0.3}
/>
// Subtle atmospheric effect
<LightRaysBackground
raysColor="#f0f9ff"
raysSpeed={0.5}
fadeDistance={0.5}
noiseAmount={0.2}
mouseInfluence={0.05}
/>
// High-energy club lighting
<LightRaysBackground
raysOrigin="bottom-center"
raysColor="#8b5cf6"
pulsating={true}
raysSpeed={2.0}
distortion={1.0}
/>
Mathematical lighting model
Ray Strength Calculation: Uses dot product mathematics to calculate the angular relationship between ray direction and pixel position, combined with distance-based falloff functions for realistic light attenuation.
Volumetric Scattering: The shader simulates how light scatters through atmospheric particles using layered sine waves with different frequencies and phases, creating the visible beam effect.
Distance Falloff: Implements physically-based inverse square law approximations for authentic light diminishing over distance, combined with customizable fade curves for artistic control.
Mouse Integration: Uses smooth interpolation between default ray direction and mouse-influenced direction, with configurable influence strength for subtle or dramatic interactive effects.
Performance considerations
Intersection Observer: The component only renders when visible in the viewport, automatically pausing expensive shader calculations when off-screen to conserve battery and GPU resources.
WebGL Context Management: Proper initialization and cleanup prevents memory leaks. The component handles WebGL context loss gracefully and provides fallback behavior for unsupported devices.
Shader Complexity: The fragment shader performs trigonometric calculations per pixel. On lower-end devices, consider reducing rayLength
or lightSpread
to decrease computational load.
Mouse Smoothing: Uses exponential smoothing (92% retention) to prevent jittery mouse movements from causing performance issues or visual artifacts in the ray calculations.
Common gotchas
Pointer Events: The container uses pointer-events-none
by default to allow interaction with underlying content. Override with pointer-events-auto
if you need the rays to capture mouse events.
Z-Index Layering: Default z-[3]
ensures rays appear above most content. Adjust the z-index in your className if you need different layering behavior with complex interfaces.
Color Blending: Light rays work best over dark backgrounds. On light backgrounds, consider using darker ray colors or blend modes like mix-blend-mode: multiply
for better visibility.
Ray Direction: Origins outside the visible area (by 20%) ensure rays enter the viewport naturally. This means the actual light source position is slightly off-screen for more realistic beam angles.
Animation Continuity: The pulsing and distortion effects use time-based functions. Rapidly changing props during animation may cause brief visual discontinuities until the next animation cycle stabilizes.
You might also like
Aurora Background
Northern lights effect with flowing gradient animations
Shooting Stars
Animated streaking lights across dark space
Sparkles Background
Twinkling particle effects with customizable density
Background Beams
Animated light beams with geometric patterns
Questions developers actually ask
React Letter Glitch Background
React letter glitch background with canvas animation. Features matrix-style text effects, customizable glitch colors, and smooth color transitions for cyberpunk interfaces.
React Lightning Background
React lightning background with WebGL. Features fractal noise, HSV color space, and dynamic electric patterns with customizable intensity.