Loading…
← All components

Background · Silk

Iridescent Silk

Graphite satin folding slowly under a light that follows the cursor, with violet-to-ice thin-film highlights.

<IridescentSilkBackground />Live

Customize

Fabric#1c1c22

Installation

Use it in your project

Or skip the copy-paste

One command: writes the file and installs the dependencies.

$ npx deepui-cli add iridescent-silk
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/IridescentSilkBackground.tsx and paste it in.

components/IridescentSilkBackground.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 IridescentSilkProps extends Pick<MaterialProps, "primaryColor">, Pick<PerformanceProps, "pauseWhenOffscreen" | "lowPowerMode"> {
357 /** Color of the fabric itself. */
358 primaryColor?: string;
359 /** @deprecated Use `primaryColor`. */
360 baseColor?: string;
361 /** Strength of the thin-film (soap-bubble) colors in the highlights, 0–1. Default 0.8. */
362 sheen?: number;
363 /** Size of the folds: higher = more, smaller folds. Default 1. */
364 foldScale?: number;
365 /** Flow speed multiplier. Default 1. */
366 speed?: number;
367 /** Film grain amount, 0–1. Default 0.25. */
368 grain?: number;
369 /** The light follows the cursor across the folds. Default true. */
370 interactive?: boolean;
371}
372
373const vertexShader = /* glsl */ `
374void main() {
375 gl_Position = vec4(position.xy, 0.0, 1.0);
376}
377`;
378
379const fragmentShader = /* glsl */ `
380uniform float uTime;
381uniform float uSheen;
382uniform float uFold;
383uniform float uGrain;
384uniform vec2 uResolution;
385uniform vec2 uLight; // light position, same space as p below
386uniform vec3 uBase;
387
388${SIMPLEX_3D}
389
390// Height of the fabric: long diagonal folds, bent and drifting with low-frequency noise
391float fabric(vec2 p, float t) {
392 float n = snoise(vec3(p * 0.32, t * 0.12));
393 vec2 q = p + vec2(n * 0.9, n * 0.45);
394 float h = sin(q.x * 1.25 + q.y * 0.55 + t * 0.55) * 0.62;
395 h += sin(q.x * 0.62 - q.y * 1.05 - t * 0.38 + n * 2.2) * 0.42;
396 h += snoise(vec3(q * 0.85, t * 0.22)) * 0.32;
397 return h;
398}
399
400void main() {
401 vec2 uv = gl_FragCoord.xy / uResolution;
402 vec2 p = (gl_FragCoord.xy - 0.5 * uResolution) / uResolution.y * 3.2 * uFold;
403 float t = uTime;
404
405 // Surface normal from the height field (central differences)
406 float e = 0.01;
407 float h = fabric(p, t);
408 float hx = fabric(p + vec2(e, 0.0), t) - fabric(p - vec2(e, 0.0), t);
409 float hy = fabric(p + vec2(0.0, e), t) - fabric(p - vec2(0.0, e), t);
410 vec3 n = normalize(vec3(-hx / (2.0 * e) * 0.55, -hy / (2.0 * e) * 0.55, 1.0));
411
412 vec3 viewDir = vec3(0.0, 0.0, 1.0);
413 vec3 lightDir = normalize(vec3(uLight - p / (3.2 * uFold), 0.9));
414 float diffuse = max(dot(n, lightDir), 0.0);
415 float specular = pow(max(dot(reflect(-lightDir, n), viewDir), 0.0), 28.0);
416 float rim = pow(1.0 - n.z, 1.6);
417
418 // Thin-film interference, kept to a cool range (violet → ice blue → silver): the highlight color
419 // shifts with the angle of the fold
420 float film = dot(n.xy, vec2(0.75, 0.45)) * 1.6 + h * 0.25 + t * 0.04;
421 float phase = 0.5 + 0.5 * sin(film * 6.2831);
422 vec3 iridescence = mix(vec3(0.66, 0.55, 1.0), vec3(0.6, 0.9, 1.0), phase);
423 iridescence = mix(iridescence, vec3(0.95, 0.75, 1.0), pow(0.5 + 0.5 * sin(film * 12.566 + 1.3), 4.0) * 0.5);
424 vec3 highlight = mix(vec3(1.0), iridescence, uSheen);
425
426 vec3 color = uBase * (0.18 + 0.9 * diffuse);
427 color += highlight * (specular * 1.15 + rim * 0.35 * diffuse);
428 color += highlight * smoothstep(0.55, 1.0, diffuse) * 0.08;
429
430 float vignette = 1.0 - 0.55 * length((uv - 0.5) * vec2(1.1, 1.0));
431 color *= vignette;
432
433 float grain = fract(sin(dot(gl_FragCoord.xy + fract(uTime * 11.0) * 83.0, vec2(12.9898, 78.233))) * 43758.5453);
434 color += (grain - 0.5) * uGrain * 0.08;
435
436 gl_FragColor = vec4(color, 1.0);
437 #include <colorspace_fragment>
438}
439`;
440
441const SilkPlane = memo(function SilkPlane({ baseColor, sheen, foldScale, speed, grain, interactive }: Required<Omit<IridescentSilkProps, "primaryColor" | "pauseWhenOffscreen" | "lowPowerMode">>) {
442 const pointer = useCanvasPointer();
443 const geometry = useMemo(() => new THREE.PlaneGeometry(2, 2), []);
444 const material = useMemo(
445 () =>
446 new THREE.ShaderMaterial({
447 uniforms: {
448 uTime: { value: 0 },
449 uSheen: { value: 0.8 },
450 uFold: { value: 1 },
451 uGrain: { value: 0.25 },
452 uResolution: { value: new THREE.Vector2(1, 1) },
453 uLight: { value: new THREE.Vector2(0.35, 0.3) },
454 uBase: { value: new THREE.Color() },
455 },
456 vertexShader,
457 fragmentShader,
458 depthWrite: false,
459 depthTest: false,
460 }),
461 []
462 );
463
464 useEffect(() => {
465 const u = material.uniforms;
466 (u.uBase.value as THREE.Color).set(baseColor);
467 u.uSheen.value = sheen;
468 u.uFold.value = foldScale;
469 u.uGrain.value = grain;
470 }, [material, baseColor, sheen, foldScale, grain]);
471
472 useEffect(() => () => geometry.dispose(), [geometry]);
473 useEffect(() => () => material.dispose(), [material]);
474
475 useFrame((state, delta) => {
476 const u = material.uniforms;
477 u.uTime.value += delta * speed;
478 state.gl.getDrawingBufferSize(u.uResolution.value as THREE.Vector2);
479
480 // The light follows the cursor; without one it drifts slowly across the fabric
481 const p = pointer.current;
482 const aspect = state.size.width / state.size.height;
483 const light = u.uLight.value as THREE.Vector2;
484 const t = state.clock.elapsedTime;
485 const targetX = interactive && p.inside ? p.ndc.x * 0.5 * aspect : Math.sin(t * 0.17) * 0.45 * aspect;
486 const targetY = interactive && p.inside ? p.ndc.y * 0.5 : 0.25 + Math.sin(t * 0.23) * 0.15;
487 light.x = THREE.MathUtils.damp(light.x, targetX, 2.5, delta);
488 light.y = THREE.MathUtils.damp(light.y, targetY, 2.5, delta);
489 });
490
491 return <mesh geometry={geometry} material={material} frustumCulled={false} />;
492});
493
494function IridescentSilkBackground({
495 primaryColor,
496 baseColor = primaryColor ?? "#1c1c22",
497 sheen = 0.8,
498 foldScale = 1,
499 speed = 1,
500 grain = 0.25,
501 interactive = true,
502 pauseWhenOffscreen,
503 lowPowerMode,
504}: IridescentSilkProps) {
505 return (
506 <BackgroundCanvas pauseWhenOffscreen={pauseWhenOffscreen} lowPowerMode={lowPowerMode} dpr={[1, 1.5]}>
507 <SilkPlane baseColor={baseColor} sheen={sheen} foldScale={foldScale} speed={speed} grain={grain} interactive={interactive} />
508 </BackgroundCanvas>
509 );
510}
511
512export default memo(IridescentSilkBackground);
03

Drop it in a page

Updated live with the settings above.

app/page.tsx
1import IridescentSilkBackground from "@/components/IridescentSilkBackground";
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 <IridescentSilkBackground
8 sheen={0.8}
9 foldScale={1}
10 speed={1}
11 grain={0.25}
12 primaryColor="#1c1c22"
13 />
14 </div>
15 );
16}
Next componentPlexus Network→