Loading…
← All components

Background · Procedural

Wave Field

A plane displaced live in the vertex shader, meant to sit behind your content as a page background.

<WaveScene />Live

Customize

Color 1#ffffff
Color 2#404040

Installation

Use it in your project

Or skip the copy-paste

One command: writes the file and installs the dependencies.

$ npx deepui-cli add wave-background
01

Install

The 3D packages this component is built on.

$ npm install three @react-three/fiber
02

Add the component

One self-contained file: create components/WaveScene.js and paste it in.

components/WaveScene.js
1"use client";
2
3import { Suspense, useEffect, useMemo, useRef, useState } from "react";
4import * as THREE from "three";
5import { Canvas, useFrame } from "@react-three/fiber";
6
7/**
8 * Reusable <Canvas> shell for every scene in /components/3d.
9 *
10 * - Pauses the render loop (frameloop="never") while the canvas is
11 * scrolled out of view, so idle scenes stop burning GPU/battery.
12 * - Caps devicePixelRatio to [1, 2] to avoid over-rendering on retina/4K.
13 * - WebGL resources (geometries, materials, the GL context itself) are
14 * disposed automatically by R3F when the Canvas unmounts.
15 */
16function Canvas3DWrapper({
17 children,
18 className = "",
19 camera = { position: [0, 0, 5], fov: 45 },
20 dpr = [1, 2],
21 fallback = null,
22 ...canvasProps
23}) {
24 const containerRef = useRef(null);
25 // Start "in view" so every scene renders at least one real frame before
26 // a canvas below the fold can be paused — some WebGL backends show an
27 // opaque black canvas if frameloop="never" is set before anything has
28 // ever been drawn, instead of the expected transparent clear color.
29 const [inView, setInView] = useState(true);
30
31 useEffect(() => {
32 const el = containerRef.current;
33 if (!el) return;
34 const observer = new IntersectionObserver(
35 ([entry]) => setInView(entry.isIntersecting),
36 { threshold: 0.1 }
37 );
38 observer.observe(el);
39 return () => observer.disconnect();
40 }, []);
41
42 return (
43 <div ref={containerRef} className={className}>
44 <Canvas
45 dpr={dpr}
46 camera={camera}
47 frameloop={inView ? "always" : "never"}
48 gl={{ antialias: true, alpha: true, powerPreference: "high-performance" }}
49 onCreated={({ gl }) => gl.setClearColor(0x000000, 0)}
50 {...canvasProps}
51 >
52 <Suspense fallback={fallback}>{children}</Suspense>
53 </Canvas>
54 </div>
55 );
56}
57
58// The plane is much larger than the view and fades out from its center: no edge is ever visible, even
59// while it turns. Same mesh density as before over the visible area.
60const SIZE = 10;
61const SEGMENTS = 100;
62const FADE_START = 0.55; // share of the half-size where the fade begins
63const FADE_END = 0.97;
64
65function WaveGrid({ primaryColor, secondaryColor, progressRef, amplitude, speed, zoom }) {
66 const wireRef = useRef(null);
67 const fillRef = useRef(null);
68 const geometryRef = useRef(null);
69
70 const basePositions = useMemo(() => {
71 const geo = new THREE.PlaneGeometry(SIZE, SIZE, SEGMENTS, SEGMENTS);
72 geo.rotateX(-Math.PI / 2.4);
73 // Per-vertex alpha (RGBA vertex colors): opaque in the middle, transparent towards the border
74 const pos = geo.attributes.position;
75 const colors = new Float32Array(pos.count * 4);
76 for (let i = 0; i < pos.count; i++) {
77 const r = Math.hypot(pos.getX(i), pos.getZ(i)) / (SIZE / 2);
78 const t = THREE.MathUtils.clamp((r - FADE_START) / (FADE_END - FADE_START), 0, 1);
79 colors.set([1, 1, 1, 1 - t * t * (3 - 2 * t)], i * 4);
80 }
81 geo.setAttribute("color", new THREE.BufferAttribute(colors, 4));
82 geometryRef.current = geo;
83 return Float32Array.from(pos.array);
84 }, []);
85
86 // Manually constructed (not JSX-owned), so R3F won't auto-dispose it.
87 useEffect(() => () => geometryRef.current?.dispose(), []);
88
89 useFrame(({ clock }) => {
90 const geo = geometryRef.current;
91 if (!geo) return;
92 const t = clock.elapsedTime;
93 const progress = progressRef?.current ?? 0;
94 const pos = geo.attributes.position;
95
96 const t2 = t * speed;
97 for (let i = 0; i < pos.count; i++) {
98 const x = basePositions[i * 3];
99 const z = basePositions[i * 3 + 2];
100 const wave =
101 (Math.sin(x * 1.1 + t2 * 0.6) * 0.22 +
102 Math.cos(z * 0.9 - t2 * 0.5) * 0.18 +
103 Math.sin((x + z) * 0.6 + progress * 6) * 0.15 * progress) *
104 amplitude;
105 pos.setY(i, wave);
106 }
107 // Unlit materials: no normals to recompute
108 pos.needsUpdate = true;
109
110 const group = wireRef.current?.parent;
111 if (group) group.rotation.y = progress * 0.9;
112 });
113
114 return (
115 <group scale={zoom}>
116 <mesh ref={wireRef} geometry={geometryRef.current}>
117 <meshBasicMaterial color={secondaryColor} wireframe vertexColors transparent depthWrite={false} />
118 </mesh>
119 <mesh ref={fillRef} geometry={geometryRef.current} position={[0, -0.01, 0]}>
120 <meshBasicMaterial color={primaryColor} vertexColors transparent opacity={0.12} depthWrite={false} side={THREE.DoubleSide} />
121 </mesh>
122 </group>
123 );
124}
125
126/**
127 * A displaced terrain-like wireframe plane, meant to sit inside a
128 * GSAP ScrollTrigger-pinned section: `progressRef.current` (0-1, updated
129 * from the scrub callback, no React re-render) drives extra wave energy
130 * and a slow reveal rotation while the section is pinned. `amplitude`,
131 * `speed` and `zoom` are exposed so a demo page can drive the look live
132 * from a Leva panel.
133 */
134export default function WaveScene({
135 primaryColor = "#ffffff",
136 secondaryColor = "#404040",
137 progressRef,
138 amplitude = 1,
139 speed = 1,
140 zoom = 1,
141 className = "",
142}) {
143 return (
144 <Canvas3DWrapper className={className} camera={{ position: [0, 1.6, 4.2], fov: 48 }}>
145 <WaveGrid
146 primaryColor={primaryColor}
147 secondaryColor={secondaryColor}
148 progressRef={progressRef}
149 amplitude={amplitude}
150 speed={speed}
151 zoom={zoom}
152 />
153 </Canvas3DWrapper>
154 );
155}
03

Drop it in a page

Updated live with the settings above.

app/page.jsx
1import WaveScene from "@/components/WaveScene";
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 <WaveScene
8 amplitude={1}
9 speed={1}
10 zoom={1}
11 primaryColor="#ffffff"
12 secondaryColor="#404040"
13 />
14 </div>
15 );
16}
Next componentPoint Cloud Field→