Join our Discord Community

React Light Rays Background

React light rays background with WebGL rendering. Features customizable ray origins, mouse interaction, pulsing effects, and volumetric lighting using OGL.

Powered by

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:

Loading component...

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

PropTypeDefaultDescription
raysOriginRaysOrigin"top-center"Light source position (8 predefined locations)
raysColorstring"#ffffff"Hex color for the light rays
raysSpeednumber1Animation speed multiplier for ray movement
lightSpreadnumber1Ray cone angle - higher values create wider spread
rayLengthnumber2Maximum ray distance as screen width multiplier
pulsatingbooleanfalseEnable rhythmic pulsing animation
fadeDistancenumber1.0Distance-based fade intensity (0.1 to 2.0)
saturationnumber1.0Color saturation level (0.0 = grayscale, 2.0 = hyper-saturated)
followMousebooleantrueEnable mouse tracking for ray direction
mouseInfluencenumber0.1Mouse influence strength (0.0 to 1.0)
noiseAmountnumber0.0Random noise intensity for organic variation
distortionnumber0.0Sine wave distortion strength for ray movement
classNamestring-Additional CSS classes for the container
...propsHTMLAttributes<HTMLDivElement>-All standard div props supported

Ray Origins

Choose from eight strategic light source positions:

OriginPositionVisual EffectUse Case
"top-center"Top middle (default)Classic sunbeam from aboveHero sections, divine light
"top-left"Top left cornerAngular dramatic lightingPhotography-style side lighting
"top-right"Top right cornerComplementary side lightingBalanced composition
"left"Middle left edgeHorizontal side lightingSpotlight effects
"right"Middle right edgeStage lighting from wingsTheater-style illumination
"bottom-center"Bottom middleUpward stage lightingFooter illumination
"bottom-left"Bottom left cornerGround-up lightingDramatic under-lighting
"bottom-right"Bottom right cornerCorner accent lightingAsymmetrical 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

Questions developers actually ask