Loading…
← All components

Background · Gradient

Aurora Gradient

Soft colored lights drifting and melting into each other on a dark field, with film grain. The cursor parts them like smoke.

<AuroraGradientBackground />Live

Customize

Base#05030c

Installation

Use it in your project

Or skip the copy-paste

One command: writes the file and installs the dependencies.

$ npx deepui-cli add aurora-gradient
01

Install

The 3D packages this component is built on.

$ npm install three @react-three/fiber && npm install -D @types/three
02

Add the component

One self-contained file: create components/AuroraGradientBackground.tsx and paste it in.

components/AuroraGradientBackground.tsx
1"use client";
2
3import {
4 memo,
5 useEffect,
6 useMemo,
7 useRef,
8 useState,
9 type CSSProperties,
10 type ReactNode,
11 type RefObject,
12} from "react";
13import * as THREE from "three";
14import { Canvas, useFrame, useThree, type CanvasProps } from "@react-three/fiber";
15
16/**
17 * The shared props API of DeepUI's individual 3D components (not the full-page templates, which keep
18 * their own tailored scenes).
19 *
20 * The props are grouped by concern. A component extends ONLY the groups it really applies, so its
21 * type never advertises a knob that does nothing (a card has no particles, a background has no tilt):
22 *
23 * interface MyCardProps extends CursorProps, TiltProps, MaterialProps, PerformanceProps { … }
24 *
25 * `Base3DComponentProps` is every group at once, for tooling that handles any component. Same name,
26 * same unit and same meaning everywhere; the default values live in DEFAULT_3D_PROPS. These files are
27 * inlined into each component's single-file bundle (lib/bundle-source.mjs), only what it uses.
28 */
29
30/** Which rotation axes follow the pointer. "x-only": the object nods (up / down); "y-only": it turns (left / right). */
31export type TiltAxis = "both" | "x-only" | "y-only" | "none";
32
33/** 1. Cursor: how the pointer pulls, pushes and drags the object. */
34interface CursorProps {
35 /** Reach of the pointer's influence, in the component's own space: CSS pixels for pixel-laid-out widgets, world units for 3D scenes (each component says which). Default 220 (px). */
36 cursorRadius?: number;
37 /** Positive pulls things towards the pointer, negative pushes them away (repulsion, shockwave). Default 1. */
38 cursorForce?: number;
39 /** How far the object slides towards the pointer, 0 (stays put) to 1 (follows closely). Default 0. */
40 magneticStrength?: number;
41 /** Share of the remaining distance the motion closes each frame (at 60 fps), 0.01 (heavy, slow) to 1 (instant). Default 0.1. */
42 cursorDamping?: number;
43}
44
45/** 2. Tilt and parallax: the object leans towards the pointer, its layers slide against each other. */
46interface TiltProps {
47 /** Largest lean towards the pointer, in degrees. Default 15. */
48 maxTiltAngle?: number;
49 /** Axes that follow the pointer. Default "both". */
50 tiltAxis?: TiltAxis;
51 /** Lean away from the pointer instead of towards it. Default false. */
52 invertTilt?: boolean;
53 /** How far apart the depth layers slide when the object tilts, 0 (flat) to 2. Default 1. */
54 parallaxOffset?: number;
55}
56
57/** 3. Text and 3D typography, for components that draw a label. */
58interface Text3DProps {
59 /** The label drawn by the component. */
60 text?: string;
61 /** Scale of the lettering. Default 1. */
62 fontSize?: number;
63 /** Extrusion depth of 3D letters. Default 0.2. */
64 textDepth?: number;
65 /** Bevelled, light-catching edges on 3D letters. Default true. */
66 bevelEnabled?: boolean;
67 /** Custom font: a .woff / .ttf (flat text) or a typeface .json (extruded text). */
68 fontUrl?: string;
69}
70
71/** 4. Rendering, materials and glow. */
72interface MaterialProps {
73 /** Main color of the object. Default "#6366f1". */
74 primaryColor?: string;
75 /** Color of the glow. Default "#00f0ff". */
76 emissiveColor?: string;
77 /** Strength of the glow, 0 (none) to 3. Default 1. */
78 emissiveIntensity?: number;
79 /** 0 (mirror, glass) to 1 (matte). Default 0.3. */
80 roughness?: number;
81 /** 0 (plastic, organic) to 1 (metal). Default 0.5. */
82 metalness?: number;
83 /** 0 (opaque) to 1 (clear glass): see-through, refracting surfaces. Default 0. */
84 transmission?: number;
85 /** Technical wireframe rendering. Default false. */
86 wireframe?: boolean;
87}
88
89/** 5. Particles, idle motion and shader animation. */
90interface MotionProps {
91 /** Number of instanced elements. Default depends on the component. */
92 particleCount?: number;
93 /** Spin on its own, in radians per second (negative: the other way; 0.5 ≈ one turn in 12 s). Default 0. */
94 autoRotateSpeed?: number;
95 /** Pulses or ripples per second. Default 1. */
96 pulseRate?: number;
97 /** Amplitude of the noise deformation, 0 (none) to 2. Default 1. */
98 displacementScale?: number;
99}
100
101/** 6. Environment and camera. */
102interface EnvironmentProps {
103 /** Camera field of view, in degrees. Default depends on the component. */
104 cameraFov?: number;
105 /** Color of the depth fog. Default "#0a0a0a". */
106 fogColor?: string;
107 /** Thickness of the fog, 0 (none) to 0.2. Default 0. */
108 fogDensity?: number;
109}
110
111/** 7. Performance and accessibility. */
112interface PerformanceProps {
113 /** Lighter rendering for weak devices: pixel ratio 1, fewer instances, smaller environment maps. Default false. */
114 lowPowerMode?: boolean;
115 /** Under the OS "reduce motion" setting, stop idle spin, pulses and deformation (pointer feedback stays). Default true. */
116 respectReducedMotion?: boolean;
117 /** Stop the render loop while the component is scrolled out of view. Default true. */
118 pauseWhenOffscreen?: boolean;
119}
120
121/** Every group at once. Components extend only the groups they apply (see the top of this file). */
122interface Base3DComponentProps
123 extends CursorProps,
124 TiltProps,
125 Text3DProps,
126 MaterialProps,
127 MotionProps,
128 EnvironmentProps,
129 PerformanceProps {}
130
131type Defaults = Required<Omit<Base3DComponentProps, "text" | "fontUrl" | "particleCount" | "cameraFov">>;
132
133/** The values a prop takes when it is left out. A component may override one, and then documents it. */
134const DEFAULT_3D_PROPS: Defaults = {
135 cursorRadius: 220,
136 cursorForce: 1,
137 magneticStrength: 0,
138 cursorDamping: 0.1,
139 maxTiltAngle: 15,
140 tiltAxis: "both",
141 invertTilt: false,
142 parallaxOffset: 1,
143 fontSize: 1,
144 textDepth: 0.2,
145 bevelEnabled: true,
146 primaryColor: "#6366f1",
147 emissiveColor: "#00f0ff",
148 emissiveIntensity: 1,
149 roughness: 0.3,
150 metalness: 0.5,
151 transmission: 0,
152 wireframe: false,
153 autoRotateSpeed: 0,
154 pulseRate: 1,
155 displacementScale: 1,
156 fogColor: "#0a0a0a",
157 fogDensity: 0,
158 lowPowerMode: false,
159 respectReducedMotion: true,
160 pauseWhenOffscreen: true,
161};
162
163/** Fills the positioned parent; never intercepts clicks meant for the page content above it. */
164const FILL_STYLE: CSSProperties = {
165 position: "absolute",
166 top: 0,
167 left: 0,
168 width: "100%",
169 height: "100%",
170 pointerEvents: "none",
171};
172
173interface BackgroundCanvasProps extends Omit<CanvasProps, "children" | "style" | "frameloop" | "gl">, Pick<PerformanceProps, "pauseWhenOffscreen" | "lowPowerMode"> {
174 children: ReactNode;
175}
176
177/**
178 * Ready-to-use <Canvas> shared by every background.
179 * - Pauses the render loop while the canvas is scrolled out of view.
180 * - Renders a single still frame when the user prefers reduced motion.
181 * - Caps the pixel ratio; R3F disposes the renderer and releases the WebGL
182 * context (forceContextLoss) when the Canvas unmounts.
183 */
184function BackgroundCanvas({ children, dpr = [1, 1.75], pauseWhenOffscreen = DEFAULT_3D_PROPS.pauseWhenOffscreen, lowPowerMode = DEFAULT_3D_PROPS.lowPowerMode, ...canvasProps }: BackgroundCanvasProps) {
185 const containerRef = useRef<HTMLDivElement>(null);
186 // Starts "in view" so the first frame is always drawn before the loop can be paused
187 const [inView, setInView] = useState(true);
188 const [reducedMotion, setReducedMotion] = useState(false);
189
190 useEffect(() => {
191 const el = containerRef.current;
192 if (!el) return undefined;
193 const observer = new IntersectionObserver(([entry]) => setInView(entry.isIntersecting));
194 observer.observe(el);
195
196 const query = window.matchMedia("(prefers-reduced-motion: reduce)");
197 const sync = () => setReducedMotion(query.matches);
198 sync();
199 query.addEventListener("change", sync);
200
201 return () => {
202 observer.disconnect();
203 query.removeEventListener("change", sync);
204 };
205 }, []);
206
207 return (
208 <div ref={containerRef} style={FILL_STYLE} aria-hidden="true">
209 <Canvas
210 dpr={lowPowerMode ? 1 : dpr}
211 frameloop={!inView && pauseWhenOffscreen ? "never" : reducedMotion ? "demand" : "always"}
212 gl={{ antialias: true, alpha: true, powerPreference: "high-performance" }}
213 style={{ pointerEvents: "none" }}
214 {...canvasProps}
215 >
216 {children}
217 </Canvas>
218 </div>
219 );
220}
221
222interface CanvasPointer {
223 /** Pointer position in normalized device coordinates of the canvas (-1..1). */
224 ndc: THREE.Vector2;
225 /** True while the pointer is over the canvas area. */
226 inside: boolean;
227 /** True while a button is held down. */
228 pressed: boolean;
229}
230
231/**
232 * Tracks the pointer relative to the canvas through window listeners: the
233 * canvas itself has `pointer-events: none`, so R3F's own events never fire.
234 * Returns a ref read inside useFrame — pointer moves never re-render React.
235 */
236function useCanvasPointer(onDown?: (ndc: THREE.Vector2) => void): RefObject<CanvasPointer> {
237 const gl = useThree((state) => state.gl);
238 const pointer = useRef<CanvasPointer>({ ndc: new THREE.Vector2(), inside: false, pressed: false });
239 const onDownRef = useRef(onDown);
240
241 useEffect(() => {
242 onDownRef.current = onDown;
243 }, [onDown]);
244
245 useEffect(() => {
246 const el = gl.domElement;
247 const update = (event: PointerEvent) => {
248 const rect = el.getBoundingClientRect();
249 const p = pointer.current;
250 p.ndc.set(((event.clientX - rect.left) / rect.width) * 2 - 1, -((event.clientY - rect.top) / rect.height) * 2 + 1);
251 p.inside = Math.abs(p.ndc.x) <= 1 && Math.abs(p.ndc.y) <= 1;
252 };
253 const down = (event: PointerEvent) => {
254 update(event);
255 pointer.current.pressed = true;
256 if (pointer.current.inside) onDownRef.current?.(pointer.current.ndc);
257 };
258 const up = () => {
259 pointer.current.pressed = false;
260 };
261 const leave = () => {
262 pointer.current.inside = false;
263 pointer.current.pressed = false;
264 };
265
266 window.addEventListener("pointermove", update, { passive: true });
267 window.addEventListener("pointerdown", down, { passive: true });
268 window.addEventListener("pointerup", up, { passive: true });
269 document.documentElement.addEventListener("pointerleave", leave);
270 window.addEventListener("blur", leave);
271 return () => {
272 window.removeEventListener("pointermove", update);
273 window.removeEventListener("pointerdown", down);
274 window.removeEventListener("pointerup", up);
275 document.documentElement.removeEventListener("pointerleave", leave);
276 window.removeEventListener("blur", leave);
277 };
278 }, [gl]);
279
280 return pointer;
281}
282
283/**
284 * Shared GLSL noise functions, injected into the background shaders as strings.
285 *
286 * Both are the classic webgl-noise implementations by Ian McEwan and Stefan
287 * Gustavson (Ashima Arts), MIT License — https://github.com/ashima/webgl-noise
288 */
289
290/** 3D simplex noise: `float snoise(vec3 v)`, returns roughly -1..1. */
291const SIMPLEX_3D = /* glsl */ `
292vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
293vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
294vec4 permute(vec4 x) { return mod289(((x * 34.0) + 10.0) * x); }
295vec4 taylorInvSqrt(vec4 r) { return 1.79284291400159 - 0.85373472095314 * r; }
296
297float snoise(vec3 v) {
298 const vec2 C = vec2(1.0 / 6.0, 1.0 / 3.0);
299 const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
300
301 vec3 i = floor(v + dot(v, C.yyy));
302 vec3 x0 = v - i + dot(i, C.xxx);
303
304 vec3 g = step(x0.yzx, x0.xyz);
305 vec3 l = 1.0 - g;
306 vec3 i1 = min(g.xyz, l.zxy);
307 vec3 i2 = max(g.xyz, l.zxy);
308
309 vec3 x1 = x0 - i1 + C.xxx;
310 vec3 x2 = x0 - i2 + C.yyy;
311 vec3 x3 = x0 - D.yyy;
312
313 i = mod289(i);
314 vec4 p = permute(permute(permute(
315 i.z + vec4(0.0, i1.z, i2.z, 1.0))
316 + i.y + vec4(0.0, i1.y, i2.y, 1.0))
317 + i.x + vec4(0.0, i1.x, i2.x, 1.0));
318
319 float n_ = 0.142857142857;
320 vec3 ns = n_ * D.wyz - D.xzx;
321
322 vec4 j = p - 49.0 * floor(p * ns.z * ns.z);
323 vec4 x_ = floor(j * ns.z);
324 vec4 y_ = floor(j - 7.0 * x_);
325
326 vec4 x = x_ * ns.x + ns.yyyy;
327 vec4 y = y_ * ns.x + ns.yyyy;
328 vec4 h = 1.0 - abs(x) - abs(y);
329
330 vec4 b0 = vec4(x.xy, y.xy);
331 vec4 b1 = vec4(x.zw, y.zw);
332 vec4 s0 = floor(b0) * 2.0 + 1.0;
333 vec4 s1 = floor(b1) * 2.0 + 1.0;
334 vec4 sh = -step(h, vec4(0.0));
335
336 vec4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;
337 vec4 a1 = b1.xzyw + s1.xzyw * sh.zzww;
338
339 vec3 p0 = vec3(a0.xy, h.x);
340 vec3 p1 = vec3(a0.zw, h.y);
341 vec3 p2 = vec3(a1.xy, h.z);
342 vec3 p3 = vec3(a1.zw, h.w);
343
344 vec4 norm = taylorInvSqrt(vec4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));
345 p0 *= norm.x;
346 p1 *= norm.y;
347 p2 *= norm.z;
348 p3 *= norm.w;
349
350 vec4 m = max(0.5 - vec4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);
351 m = m * m;
352 return 105.0 * dot(m * m, vec4(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3)));
353}
354`;
355
356export interface AuroraGradientProps extends Pick<MaterialProps, "primaryColor" | "emissiveIntensity">, Pick<PerformanceProps, "pauseWhenOffscreen" | "lowPowerMode"> {
357 /** Up to four colors blended into the flowing mesh gradient. */
358 colors?: string[];
359 /** Color of the deepest areas. */
360 primaryColor?: string;
361 /** @deprecated Use `primaryColor`. */
362 baseColor?: string;
363 /** Flow speed multiplier. Default 1. */
364 speed?: number;
365 /** Overall brightness. Default 1. */
366 emissiveIntensity?: number;
367 /** @deprecated Use `emissiveIntensity`. */
368 intensity?: number;
369 /** Film grain amount, 0–1. Default 0.35. */
370 grain?: number;
371 /** The flow bends around the cursor and a soft light follows it. Default true. */
372 interactive?: boolean;
373}
374
375const DEFAULT_COLORS = ["#4c1d95", "#8b5cf6", "#1e3a8a", "#c4b5fd"];
376
377const vertexShader = /* glsl */ `
378void main() {
379 // Full-screen quad: the plane's -1..1 vertices are already in clip space
380 gl_Position = vec4(position.xy, 0.0, 1.0);
381}
382`;
383
384const fragmentShader = /* glsl */ `
385uniform float uTime;
386uniform float uIntensity;
387uniform float uGrain;
388uniform vec2 uResolution;
389uniform vec2 uMouse; // cursor, same space as p below
390uniform float uMouseStrength;
391uniform vec3 uBase;
392uniform vec3 uC0;
393uniform vec3 uC1;
394uniform vec3 uC2;
395uniform vec3 uC3;
396
397${SIMPLEX_3D}
398
399// One soft color light drifting on its own slow Lissajous path
400vec3 blob(vec2 p, vec3 color, vec2 speed, vec2 phase, vec2 reach, float radius, float t) {
401 vec2 center = vec2(sin(t * speed.x + phase.x) * reach.x, cos(t * speed.y + phase.y) * reach.y);
402 vec2 d = p - center;
403 return color * exp(-dot(d, d) / (radius * radius));
404}
405
406void main() {
407 vec2 uv = gl_FragCoord.xy / uResolution;
408 float aspect = uResolution.x / uResolution.y;
409 vec2 p = (gl_FragCoord.xy - 0.5 * uResolution) / uResolution.y;
410 float t = uTime;
411
412 // The cursor pushes the lights away from it, like a hand through smoke
413 vec2 toMouse = p - uMouse;
414 float mouseDistance = length(toMouse);
415 vec2 push = toMouse / (mouseDistance + 0.2) * exp(-mouseDistance * 2.5) * 0.18 * uMouseStrength;
416
417 // Low-frequency warp: the lights melt into each other instead of reading as circles
418 vec2 warp = vec2(snoise(vec3(p * 0.9, t * 0.6)), snoise(vec3(p * 0.9 + 7.3, t * 0.6))) * 0.2;
419 vec2 q = p + warp + push;
420
421 vec3 light = vec3(0.0);
422 light += blob(q, uC0, vec2(0.71, 0.53), vec2(0.0, 1.3), vec2(0.42 * aspect, 0.3), 0.42, t);
423 light += blob(q, uC1, vec2(0.47, 0.83), vec2(2.1, 0.2), vec2(0.38 * aspect, 0.34), 0.34, t);
424 light += blob(q, uC2, vec2(0.93, 0.39), vec2(4.0, 2.6), vec2(0.45 * aspect, 0.28), 0.4, t);
425 light += blob(q, uC3, vec2(0.37, 0.67), vec2(1.2, 4.4), vec2(0.3 * aspect, 0.32), 0.2, t);
426
427 // Soft saturation: overlapping lights bloom towards their brightest color instead of clipping
428 vec3 color = uBase + (1.0 - exp(-light * 0.85 * uIntensity));
429
430 float vignette = 1.0 - 0.6 * length((uv - 0.5) * vec2(1.1, 1.0));
431 // Contrast curve: the gaps between the lights fall back to near-black, the lights keep their glow
432 color = pow(color * vignette, vec3(1.35));
433 color += uC3 * exp(-mouseDistance * mouseDistance * 9.0) * 0.08 * uMouseStrength;
434
435 // Animated film grain: breaks up gradient banding and gives a print-like texture
436 float grain = fract(sin(dot(gl_FragCoord.xy + fract(uTime * 13.0) * 97.0, vec2(12.9898, 78.233))) * 43758.5453);
437 color += (grain - 0.5) * uGrain * 0.12;
438
439 gl_FragColor = vec4(color, 1.0);
440 #include <colorspace_fragment>
441}
442`;
443
444const AuroraPlane = memo(function AuroraPlane({ colors, baseColor, speed, intensity, grain, interactive }: Required<Omit<AuroraGradientProps, "primaryColor" | "emissiveIntensity" | "pauseWhenOffscreen" | "lowPowerMode">>) {
445 const pointer = useCanvasPointer();
446 const geometry = useMemo(() => new THREE.PlaneGeometry(2, 2), []);
447 const material = useMemo(
448 () =>
449 new THREE.ShaderMaterial({
450 uniforms: {
451 uTime: { value: 0 },
452 uIntensity: { value: 1 },
453 uGrain: { value: 0.35 },
454 uResolution: { value: new THREE.Vector2(1, 1) },
455 uMouse: { value: new THREE.Vector2() },
456 uMouseStrength: { value: 0 },
457 uBase: { value: new THREE.Color() },
458 uC0: { value: new THREE.Color() },
459 uC1: { value: new THREE.Color() },
460 uC2: { value: new THREE.Color() },
461 uC3: { value: new THREE.Color() },
462 },
463 vertexShader,
464 fragmentShader,
465 depthWrite: false,
466 depthTest: false,
467 }),
468 []
469 );
470
471 const colorsKey = colors.join("|");
472 useEffect(() => {
473 const u = material.uniforms;
474 const palette = colorsKey ? colorsKey.split("|") : DEFAULT_COLORS;
475 ["uC0", "uC1", "uC2", "uC3"].forEach((key, i) => (u[key].value as THREE.Color).set(palette[i % palette.length]));
476 (u.uBase.value as THREE.Color).set(baseColor);
477 u.uIntensity.value = intensity;
478 u.uGrain.value = grain;
479 }, [material, colorsKey, baseColor, intensity, grain]);
480
481 useEffect(() => () => geometry.dispose(), [geometry]);
482 useEffect(() => () => material.dispose(), [material]);
483
484 useFrame((state, delta) => {
485 const u = material.uniforms;
486 u.uTime.value += delta * speed * 0.35;
487 state.gl.getDrawingBufferSize(u.uResolution.value as THREE.Vector2);
488
489 // Cursor → the shader's space (centered, height = 1)
490 const p = pointer.current;
491 const active = interactive && p.inside;
492 const aspect = state.size.width / state.size.height;
493 const mouse = u.uMouse.value as THREE.Vector2;
494 if (active) {
495 mouse.x = THREE.MathUtils.damp(mouse.x, p.ndc.x * 0.5 * aspect, 3, delta);
496 mouse.y = THREE.MathUtils.damp(mouse.y, p.ndc.y * 0.5, 3, delta);
497 }
498 u.uMouseStrength.value = THREE.MathUtils.damp(u.uMouseStrength.value as number, active ? 1 : 0, 2, delta);
499 });
500
501 return <mesh geometry={geometry} material={material} frustumCulled={false} />;
502});
503
504function AuroraGradientBackground({
505 colors = DEFAULT_COLORS,
506 primaryColor,
507 baseColor = primaryColor ?? "#05030c",
508 speed = 1,
509 emissiveIntensity,
510 intensity = emissiveIntensity ?? 1,
511 grain = 0.35,
512 interactive = true,
513 pauseWhenOffscreen,
514 lowPowerMode,
515}: AuroraGradientProps) {
516 return (
517 // Full-screen fragment shader: the pixel ratio is capped (the gradient is soft, more pixels add nothing)
518 <BackgroundCanvas pauseWhenOffscreen={pauseWhenOffscreen} lowPowerMode={lowPowerMode} dpr={[1, 1.25]}>
519 <AuroraPlane colors={colors} baseColor={baseColor} speed={speed} intensity={intensity} grain={grain} interactive={interactive} />
520 </BackgroundCanvas>
521 );
522}
523
524export default memo(AuroraGradientBackground);
03

Drop it in a page

Updated live with the settings above.

app/page.tsx
1import AuroraGradientBackground from "@/components/AuroraGradientBackground";
2
3export default function Section() {
4 return (
5 // The component fills this box: give it a position and a height
6 <div style={{ position: "relative", width: "100%", height: "100vh" }}>
7 <AuroraGradientBackground
8 speed={1}
9 emissiveIntensity={1}
10 grain={0}
11 primaryColor="#05030c"
12 />
13 </div>
14 );
15}
Next componentGlassmorphism→