Wie man Rayos von Luz Volumétricos (God Rays) in CSS Rein und HTML5 Canvas
Die rayos von luz volumétricos (rayos crepusculares oder god rays) se crean in CSS rein mediante eine capa giratoria mit conic-gradient() proyectada von eine esquina emisora superior, desenfocada mit filter: blur(25px) und fusionada mit mix-blend-mode: screen. In Canvas, se complementan mit motas von polvo atmosférico flotantes que se iluminan al cruzar die haces von luz.
Schritt 1: Arquitectura vom Emisor mit Conic-Gradient in CSS Rein
Erstellt ein Element posicionado in die esquina superior que proyecte múltiples haces angulares alternando paradas von luz blanca und transparencia.
.god-rays-container {
position: relative;
width: 100%;
min-height: 100vh;
background: #080614;
overflow: hidden;
}
.god-rays-emitter {
position: absolute;
top: -40%;
left: 20%;
width: 160%;
height: 160%;
background: conic-gradient(
from 130deg at 50% 0%,
rgba(255, 255, 255, 0.25) 0deg,
transparent 15deg,
rgba(255, 255, 255, 0.18) 25deg,
transparent 35deg,
rgba(255, 255, 255, 0.3) 50deg,
transparent 65deg,
rgba(255, 255, 255, 0.15) 80deg,
transparent 95deg
);
filter: blur(24px);
mix-blend-mode: screen;
pointer-events: none;
animation: raySweep 20s ease-in-out infinite alternate;
}
Schritt 2: Animation von Balanceo Suave für Simular Nubes in Movimiento
Aplica eine oscilación langsame von rotación mediante transformaciones für que die rayos bailen suavemente wie si die luz solar se filtrase zwischen nubes.
@keyframes raySweep {
0% { transform: rotate(-5deg) scaleY(1); }
50% { transform: rotate(4deg) scaleY(1.1); }
100% { transform: rotate(-2deg) scaleY(0.95); }
}
Schritt 3: Simulación in Canvas von Motas von Polvo Flotantes (Dust Motes)
Generiert diminutas partículas suspendidas in der aire que floten erráticamente mit movimiento browniano.
class DustMote {
constructor(w, h) {
this.x = Math.random() * w;
this.y = Math.random() * h;
this.size = Math.random() * 2 + 0.5;
this.vx = (Math.random() - 0.5) * 0.3;
this.vy = -Math.random() * 0.4 - 0.1;
this.alpha = Math.random() * 0.6 + 0.2;
}
update(w, h) {
this.x += this.vx;
this.y += this.vy;
if (this.y < 0) this.y = h;
if (this.x < 0 || this.x > w) this.vx *= -1;
}
draw(ctx) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fillStyle = `rgba(255, 255, 255, ${this.alpha})`;
ctx.fill();
}
}
Schritt 4: Maske von Atenuación Radial für Desvanecer die Base
Aplica eine Maske elíptica für que die rayos no corten bruscamente in die parte inferior, fundiéndose mit die oscuridad vom Hintergrund.
.god-rays-emitter {
mask-image: radial-gradient(ellipse at 50% 0%, #000 30%, transparent 80%);
}
Erweiterte Best Practices & Vertiefung
Erweitere diese Implementierung durch die Kombination mit ergänzenden Werkzeugen und Design-Tokens:
Mein Tipp für den produktiven Einsatz: Teste frühzeitig in mehreren Browsern. Halte die DOM-Hierarchie schlank, optimiere Bildressourcen mit modernen Formaten (WebP/AVIF) und stelle Barrierefreiheit per Tastaturbedienung sicher.
Häufig gestellte Fragen (FAQ)
Praxisnahe Antworten zu Implementierung, Browser-Besonderheiten und Performance-Tipps.
Qué causa der fenómeno real von die rayos crepusculares (god rays)?
Ocurren wenn die luz solar directa es parcialmente bloqueada durch nubes oder copas von árboles, ermöglicht que solo columnas selectas von luz iluminen der polvo und vapor von die atmósfera, haciéndose visibles durch dispersión von Rayleigh und Mie.
Durch qué conic-gradient() es die Tool ideal in CSS für dieser Effekt?
Porque ermöglicht proyectar sectores angulares von ein único punto focal (at 50% 0%), recreando mit exactitud die perspectiva radial von rayos que se abren in abanico von eine Schriftart puntual lejana.
Wie evitar que der desenfoque ralentice mobile Endgeräte antiguos?
In móviles, reduce der desenfoque a filter: blur(12px) oder generiert der degradado directamente mit paradas suavizadas (ej. rgba(255,255,255,0.2) 0deg, transparent 20deg) ohne requerir Filter von desenfoque computacional.
Es posible aplicar dieser Effekt von iluminación über ein Video von Hintergrund?
Sí. Al colocar der contenedor von rayos mit mix-blend-mode: screen durch encima vom Element <video>, die rayos sumarán su Glanz a die escena von Video von Form cinematográfica.
Qué impacto tiene in die legibilidad vom Text?
Mantén die opacidad maximale von die rayos durch debajo vom 30% (alpha <= 0.3). Für títulos über die rayos, verwendet tipografía mit Höhe contraste oder añade text-shadow: 0 4px 12px rgba(0, 0, 0, 0.9).