← All components Next componentHolo Menu→
Advanced · Media
Liquid Slider
A full-bleed image slider where each slide melts into the next through a displacement wave.
<LiquidSlider3D />Live
Arrows, dots, swipe or ← →
Customize
Installation
Use it in your project
Or skip the copy-paste
One command: writes the file and installs the dependencies.
$ npx deepui-cli add liquid-slider
01
Install
The 3D packages this component is built on.
$ npm install three @react-three/fiber @react-three/drei && npm install -D @types/three
02
Add the component
One self-contained file: create components/LiquidSlider3D.tsx and paste it in.
components/LiquidSlider3D.tsx
1"use client";23import {4 memo,5 Suspense,6 useCallback,7 useEffect,8 useMemo,9 useRef,10 useState,11 type CSSProperties,12 type ReactNode,13} from "react";14import * as THREE from "three";15import { Canvas, useFrame, useThree, type CanvasProps } from "@react-three/fiber";16import { useTexture } from "@react-three/drei";1718/**19 * Shared GLSL noise functions, injected into the background shaders as strings.20 *21 * Both are the classic webgl-noise implementations by Ian McEwan and Stefan22 * Gustavson (Ashima Arts), MIT License — https://github.com/ashima/webgl-noise23 */2425/** 3D simplex noise: `float snoise(vec3 v)`, returns roughly -1..1. */26const SIMPLEX_3D = /* glsl */ `27vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }28vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }29vec4 permute(vec4 x) { return mod289(((x * 34.0) + 10.0) * x); }30vec4 taylorInvSqrt(vec4 r) { return 1.79284291400159 - 0.85373472095314 * r; }3132float snoise(vec3 v) {33 const vec2 C = vec2(1.0 / 6.0, 1.0 / 3.0);34 const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);3536 vec3 i = floor(v + dot(v, C.yyy));37 vec3 x0 = v - i + dot(i, C.xxx);3839 vec3 g = step(x0.yzx, x0.xyz);40 vec3 l = 1.0 - g;41 vec3 i1 = min(g.xyz, l.zxy);42 vec3 i2 = max(g.xyz, l.zxy);4344 vec3 x1 = x0 - i1 + C.xxx;45 vec3 x2 = x0 - i2 + C.yyy;46 vec3 x3 = x0 - D.yyy;4748 i = mod289(i);49 vec4 p = permute(permute(permute(50 i.z + vec4(0.0, i1.z, i2.z, 1.0))51 + i.y + vec4(0.0, i1.y, i2.y, 1.0))52 + i.x + vec4(0.0, i1.x, i2.x, 1.0));5354 float n_ = 0.142857142857;55 vec3 ns = n_ * D.wyz - D.xzx;5657 vec4 j = p - 49.0 * floor(p * ns.z * ns.z);58 vec4 x_ = floor(j * ns.z);59 vec4 y_ = floor(j - 7.0 * x_);6061 vec4 x = x_ * ns.x + ns.yyyy;62 vec4 y = y_ * ns.x + ns.yyyy;63 vec4 h = 1.0 - abs(x) - abs(y);6465 vec4 b0 = vec4(x.xy, y.xy);66 vec4 b1 = vec4(x.zw, y.zw);67 vec4 s0 = floor(b0) * 2.0 + 1.0;68 vec4 s1 = floor(b1) * 2.0 + 1.0;69 vec4 sh = -step(h, vec4(0.0));7071 vec4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;72 vec4 a1 = b1.xzyw + s1.xzyw * sh.zzww;7374 vec3 p0 = vec3(a0.xy, h.x);75 vec3 p1 = vec3(a0.zw, h.y);76 vec3 p2 = vec3(a1.xy, h.z);77 vec3 p3 = vec3(a1.zw, h.w);7879 vec4 norm = taylorInvSqrt(vec4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));80 p0 *= norm.x;81 p1 *= norm.y;82 p2 *= norm.z;83 p3 *= norm.w;8485 vec4 m = max(0.5 - vec4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);86 m = m * m;87 return 105.0 * dot(m * m, vec4(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3)));88}89`;9091/**92 * The shared props API of DeepUI's individual 3D components (not the full-page templates, which keep93 * their own tailored scenes).94 *95 * The props are grouped by concern. A component extends ONLY the groups it really applies, so its96 * type never advertises a knob that does nothing (a card has no particles, a background has no tilt):97 *98 * interface MyCardProps extends CursorProps, TiltProps, MaterialProps, PerformanceProps { … }99 *100 * `Base3DComponentProps` is every group at once, for tooling that handles any component. Same name,101 * same unit and same meaning everywhere; the default values live in DEFAULT_3D_PROPS. These files are102 * inlined into each component's single-file bundle (lib/bundle-source.mjs), only what it uses.103 */104105/** Which rotation axes follow the pointer. "x-only": the object nods (up / down); "y-only": it turns (left / right). */106export type TiltAxis = "both" | "x-only" | "y-only" | "none";107108/** 1. Cursor: how the pointer pulls, pushes and drags the object. */109interface CursorProps {110 /** 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). */111 cursorRadius?: number;112 /** Positive pulls things towards the pointer, negative pushes them away (repulsion, shockwave). Default 1. */113 cursorForce?: number;114 /** How far the object slides towards the pointer, 0 (stays put) to 1 (follows closely). Default 0. */115 magneticStrength?: number;116 /** Share of the remaining distance the motion closes each frame (at 60 fps), 0.01 (heavy, slow) to 1 (instant). Default 0.1. */117 cursorDamping?: number;118}119120/** 2. Tilt and parallax: the object leans towards the pointer, its layers slide against each other. */121interface TiltProps {122 /** Largest lean towards the pointer, in degrees. Default 15. */123 maxTiltAngle?: number;124 /** Axes that follow the pointer. Default "both". */125 tiltAxis?: TiltAxis;126 /** Lean away from the pointer instead of towards it. Default false. */127 invertTilt?: boolean;128 /** How far apart the depth layers slide when the object tilts, 0 (flat) to 2. Default 1. */129 parallaxOffset?: number;130}131132/** 3. Text and 3D typography, for components that draw a label. */133interface Text3DProps {134 /** The label drawn by the component. */135 text?: string;136 /** Scale of the lettering. Default 1. */137 fontSize?: number;138 /** Extrusion depth of 3D letters. Default 0.2. */139 textDepth?: number;140 /** Bevelled, light-catching edges on 3D letters. Default true. */141 bevelEnabled?: boolean;142 /** Custom font: a .woff / .ttf (flat text) or a typeface .json (extruded text). */143 fontUrl?: string;144}145146/** 4. Rendering, materials and glow. */147interface MaterialProps {148 /** Main color of the object. Default "#6366f1". */149 primaryColor?: string;150 /** Color of the glow. Default "#00f0ff". */151 emissiveColor?: string;152 /** Strength of the glow, 0 (none) to 3. Default 1. */153 emissiveIntensity?: number;154 /** 0 (mirror, glass) to 1 (matte). Default 0.3. */155 roughness?: number;156 /** 0 (plastic, organic) to 1 (metal). Default 0.5. */157 metalness?: number;158 /** 0 (opaque) to 1 (clear glass): see-through, refracting surfaces. Default 0. */159 transmission?: number;160 /** Technical wireframe rendering. Default false. */161 wireframe?: boolean;162}163164/** 5. Particles, idle motion and shader animation. */165interface MotionProps {166 /** Number of instanced elements. Default depends on the component. */167 particleCount?: number;168 /** Spin on its own, in radians per second (negative: the other way; 0.5 ≈ one turn in 12 s). Default 0. */169 autoRotateSpeed?: number;170 /** Pulses or ripples per second. Default 1. */171 pulseRate?: number;172 /** Amplitude of the noise deformation, 0 (none) to 2. Default 1. */173 displacementScale?: number;174}175176/** 6. Environment and camera. */177interface EnvironmentProps {178 /** Camera field of view, in degrees. Default depends on the component. */179 cameraFov?: number;180 /** Color of the depth fog. Default "#0a0a0a". */181 fogColor?: string;182 /** Thickness of the fog, 0 (none) to 0.2. Default 0. */183 fogDensity?: number;184}185186/** 7. Performance and accessibility. */187interface PerformanceProps {188 /** Lighter rendering for weak devices: pixel ratio 1, fewer instances, smaller environment maps. Default false. */189 lowPowerMode?: boolean;190 /** Under the OS "reduce motion" setting, stop idle spin, pulses and deformation (pointer feedback stays). Default true. */191 respectReducedMotion?: boolean;192 /** Stop the render loop while the component is scrolled out of view. Default true. */193 pauseWhenOffscreen?: boolean;194}195196/** Every group at once. Components extend only the groups they apply (see the top of this file). */197interface Base3DComponentProps198 extends CursorProps,199 TiltProps,200 Text3DProps,201 MaterialProps,202 MotionProps,203 EnvironmentProps,204 PerformanceProps {}205206type Defaults = Required<Omit<Base3DComponentProps, "text" | "fontUrl" | "particleCount" | "cameraFov">>;207208/** The values a prop takes when it is left out. A component may override one, and then documents it. */209const DEFAULT_3D_PROPS: Defaults = {210 cursorRadius: 220,211 cursorForce: 1,212 magneticStrength: 0,213 cursorDamping: 0.1,214 maxTiltAngle: 15,215 tiltAxis: "both",216 invertTilt: false,217 parallaxOffset: 1,218 fontSize: 1,219 textDepth: 0.2,220 bevelEnabled: true,221 primaryColor: "#6366f1",222 emissiveColor: "#00f0ff",223 emissiveIntensity: 1,224 roughness: 0.3,225 metalness: 0.5,226 transmission: 0,227 wireframe: false,228 autoRotateSpeed: 0,229 pulseRate: 1,230 displacementScale: 1,231 fogColor: "#0a0a0a",232 fogDensity: 0,233 lowPowerMode: false,234 respectReducedMotion: true,235 pauseWhenOffscreen: true,236};237238/** Inline widget: fills its positioned parent and receives pointer events. */239const FILL_STYLE: CSSProperties = { position: "absolute", top: 0, left: 0, width: "100%", height: "100%" };240241/** Cursor overlay: covers the viewport, above everything, never intercepts the page's events. */242const OVERLAY_STYLE: CSSProperties = {243 position: "fixed",244 top: 0,245 left: 0,246 width: "100%",247 height: "100%",248 pointerEvents: "none",249 zIndex: 9999,250};251252interface UICanvasProps extends Omit<CanvasProps, "children" | "style" | "gl">, Pick<PerformanceProps, "pauseWhenOffscreen" | "lowPowerMode"> {253 children: ReactNode;254 /** Full-viewport, click-through overlay (cursors) instead of an inline widget. */255 overlay?: boolean;256}257258/**259 * Ready-to-use <Canvas>: pauses its render loop while scrolled out of view (pauseWhenOffscreen), pixel260 * ratio 1 in lowPowerMode; R3F disposes the renderer and its WebGL context on unmount.261 */262function UICanvas({ children, overlay = false, dpr = [1, 2], pauseWhenOffscreen = DEFAULT_3D_PROPS.pauseWhenOffscreen, lowPowerMode = DEFAULT_3D_PROPS.lowPowerMode, ...canvasProps }: UICanvasProps) {263 const containerRef = useRef<HTMLDivElement>(null);264 // In view at first: the first frame is always drawn before the loop can be paused265 const [inView, setInView] = useState(true);266 useEffect(() => {267 const el = containerRef.current;268 if (!el || !pauseWhenOffscreen) return undefined;269 const observer = new IntersectionObserver(([entry]) => setInView(entry.isIntersecting));270 observer.observe(el);271 return () => observer.disconnect();272 }, [pauseWhenOffscreen]);273 return (274 <div ref={containerRef} style={overlay ? OVERLAY_STYLE : FILL_STYLE} aria-hidden="true">275 <Canvas276 dpr={lowPowerMode ? 1 : dpr}277 frameloop={inView || !pauseWhenOffscreen ? "always" : "never"}278 gl={{ antialias: true, alpha: true, powerPreference: "high-performance" }}279 style={overlay ? { pointerEvents: "none" } : undefined}280 {...canvasProps}281 >282 {children}283 </Canvas>284 </div>285 );286}287288export interface LiquidSliderProps extends Pick<PerformanceProps, "pauseWhenOffscreen" | "lowPowerMode"> {289 images: string[];290 /** Grayscale displacement map driving the distortion. A procedural noise is used when omitted. */291 displacementImage?: string;292 /** Strength of the distortion. Default 0.35. */293 intensity?: number;294 /** Transition speed multiplier. Default 1. */295 speed?: number;296 /** Advance on its own every N seconds (0 = off). Default 0. */297 autoplay?: number;298}299300const vertexShader = /* glsl */ `301varying vec2 vUv;302void main() {303 vUv = uv;304 gl_Position = vec4(position.xy, 0.0, 1.0);305}306`;307308const fragmentShader = /* glsl */ `309uniform sampler2D uFrom;310uniform sampler2D uTo;311uniform sampler2D uDisplacement;312uniform float uHasDisplacement;313uniform float uProgress; // 0 → 1 during a transition314uniform float uIntensity;315uniform float uDirection; // 1 = next, -1 = previous316uniform float uTime;317uniform vec2 uResolution;318uniform vec2 uFromAspect; // (image aspect, 0) per texture319uniform vec2 uToAspect;320varying vec2 vUv;321322${SIMPLEX_3D}323324vec2 cover(vec2 uv, float imageAspect) {325 float planeAspect = uResolution.x / uResolution.y;326 vec2 scale = planeAspect > imageAspect ? vec2(1.0, imageAspect / planeAspect) : vec2(planeAspect / imageAspect, 1.0);327 return (uv - 0.5) * scale + 0.5;328}329330void main() {331 vec2 uv = vUv;332 float d;333 if (uHasDisplacement > 0.5) {334 d = texture2D(uDisplacement, uv).r;335 } else {336 // Procedural displacement: soft, flowing noise337 d = snoise(vec3(uv * vec2(2.2, 1.6), uTime * 0.15)) * 0.5 + 0.5;338 d = mix(d, snoise(vec3(uv * 6.0, uTime * 0.3)) * 0.5 + 0.5, 0.25);339 }340341 float p = uProgress;342 // The liquid front sweeps across in the direction of travel, broken up by the displacement343 float sweep = uDirection > 0.0 ? uv.x : 1.0 - uv.x;344 float mask = smoothstep(0.0, 1.0, (p * 1.8 - 0.4) + (d - 0.5) * 0.9 - (sweep - 0.5) * 0.7);345346 // Outgoing image is pushed and smeared away, incoming one settles in from the opposite warp347 vec2 push = vec2(uDirection * 1.0, 0.25) * (d - 0.5);348 vec2 uvFrom = uv + push * mask * uIntensity;349 vec2 uvTo = uv - push * (1.0 - mask) * uIntensity;350351 // Slight RGB split while the image is moving352 float wobble = sin(p * 3.14159) * uIntensity * 0.02;353 vec3 from = vec3(354 texture2D(uFrom, cover(uvFrom + vec2(wobble, 0.0), uFromAspect.x)).r,355 texture2D(uFrom, cover(uvFrom, uFromAspect.x)).g,356 texture2D(uFrom, cover(uvFrom - vec2(wobble, 0.0), uFromAspect.x)).b357 );358 vec3 to = vec3(359 texture2D(uTo, cover(uvTo + vec2(wobble, 0.0), uToAspect.x)).r,360 texture2D(uTo, cover(uvTo, uToAspect.x)).g,361 texture2D(uTo, cover(uvTo - vec2(wobble, 0.0), uToAspect.x)).b362 );363364 gl_FragColor = vec4(mix(from, to, mask), 1.0);365 #include <colorspace_fragment>366}367`;368369interface SlidesProps {370 images: string[];371 displacementImage?: string;372 intensity: number;373 speed: number;374 index: number;375 direction: number;376 onSettled: () => void;377}378379function aspectOf(texture: THREE.Texture) {380 const image = texture.image as { width?: number; height?: number } | undefined;381 return image?.width && image?.height ? image.width / image.height : 1;382}383384function Slides({ images, displacementImage, intensity, speed, index, direction, onSettled }: SlidesProps) {385 const textures = useTexture(images); // suspends once, all slides preloaded386 const displacement = useTexture(displacementImage ?? images[0]);387 const gl = useThree((state) => state.gl);388 const geometry = useMemo(() => new THREE.PlaneGeometry(2, 2), []);389 const material = useMemo(390 () =>391 new THREE.ShaderMaterial({392 uniforms: {393 uFrom: { value: null },394 uTo: { value: null },395 uDisplacement: { value: null },396 uHasDisplacement: { value: 0 },397 uProgress: { value: 0 },398 uIntensity: { value: 0.35 },399 uDirection: { value: 1 },400 uTime: { value: 0 },401 uResolution: { value: new THREE.Vector2(1, 1) },402 uFromAspect: { value: new THREE.Vector2(1, 0) },403 uToAspect: { value: new THREE.Vector2(1, 0) },404 },405 vertexShader,406 fragmentShader,407 depthTest: false,408 depthWrite: false,409 }),410 []411 );412 const transition = useRef({ shown: index, target: index, progress: 0 });413414 useEffect(() => {415 textures.forEach((t) => {416 t.colorSpace = THREE.SRGBColorSpace;417 t.anisotropy = 8;418 t.needsUpdate = true;419 });420 const u = material.uniforms;421 u.uFrom.value = textures[transition.current.shown] ?? textures[0];422 (u.uFromAspect.value as THREE.Vector2).x = aspectOf(u.uFrom.value);423 u.uTo.value = u.uFrom.value;424 (u.uToAspect.value as THREE.Vector2).x = aspectOf(u.uTo.value);425 }, [textures, material]);426 useEffect(() => {427 material.uniforms.uDisplacement.value = displacement;428 material.uniforms.uHasDisplacement.value = displacementImage ? 1 : 0;429 }, [material, displacement, displacementImage]);430 useEffect(() => {431 material.uniforms.uIntensity.value = intensity;432 }, [material, intensity]);433434 // A new index starts a transition from what is on screen435 useEffect(() => {436 const tr = transition.current;437 if (index === tr.target) return;438 const u = material.uniforms;439 u.uFrom.value = textures[tr.progress > 0.5 ? tr.target : tr.shown];440 (u.uFromAspect.value as THREE.Vector2).x = aspectOf(u.uFrom.value);441 u.uTo.value = textures[index];442 (u.uToAspect.value as THREE.Vector2).x = aspectOf(u.uTo.value);443 u.uDirection.value = direction;444 tr.shown = tr.progress > 0.5 ? tr.target : tr.shown;445 tr.target = index;446 tr.progress = 0;447 }, [index, direction, textures, material]);448449 useEffect(() => () => geometry.dispose(), [geometry]);450 useEffect(() => () => material.dispose(), [material]);451 useEffect(() => () => textures.forEach((t) => t.dispose()), [textures]);452453 useFrame((state, delta) => {454 const u = material.uniforms;455 u.uTime.value = state.clock.elapsedTime;456 gl.getDrawingBufferSize(u.uResolution.value as THREE.Vector2);457 const tr = transition.current;458 if (tr.shown !== tr.target) {459 tr.progress = Math.min(1, tr.progress + Math.min(delta, 1 / 30) * 0.9 * speed);460 // Ease in-out461 const p = tr.progress;462 u.uProgress.value = p < 0.5 ? 4 * p * p * p : 1 - Math.pow(-2 * p + 2, 3) / 2;463 if (tr.progress >= 1) {464 tr.shown = tr.target;465 u.uFrom.value = u.uTo.value;466 (u.uFromAspect.value as THREE.Vector2).x = (u.uToAspect.value as THREE.Vector2).x;467 u.uProgress.value = 0;468 onSettled();469 }470 }471 });472473 return <mesh geometry={geometry} material={material} frustumCulled={false} />;474}475476const ARROW: CSSProperties = {477 width: 44,478 height: 44,479 borderRadius: 999,480 border: "1px solid rgba(255,255,255,0.3)",481 background: "rgba(10,10,14,0.45)",482 color: "white",483 font: "600 18px/1 system-ui, sans-serif",484 cursor: "pointer",485 backdropFilter: "blur(8px)",486};487488/** Self-contained slider: fills its positioned parent. Arrows, dots, swipe, keyboard ← →. */489function LiquidSlider3D({ images, displacementImage, intensity = 0.35, speed = 1, autoplay = 0, pauseWhenOffscreen, lowPowerMode }: LiquidSliderProps) {490 const [index, setIndex] = useState(0);491 const [direction, setDirection] = useState(1);492 const busy = useRef(false);493 const count = images.length;494495 const go = useCallback(496 (step: number) => {497 if (count < 2) return;498 setDirection(step >= 0 ? 1 : -1);499 setIndex((i) => (i + step + count) % count);500 busy.current = true;501 },502 [count]503 );504505 useEffect(() => {506 if (!autoplay) return undefined;507 const timer = window.setInterval(() => go(1), autoplay * 1000);508 return () => window.clearInterval(timer);509 }, [autoplay, go]);510511 // Swipe / drag512 const drag = useRef<{ x: number; id: number } | null>(null);513514 if (!count) return null;515 return (516 <div517 style={{ position: "absolute", inset: 0, touchAction: "pan-y" }}518 role="region"519 aria-roledescription="carousel"520 aria-label="Image slider"521 tabIndex={0}522 onKeyDown={(e) => {523 if (e.key === "ArrowRight") go(1);524 if (e.key === "ArrowLeft") go(-1);525 }}526 onPointerDown={(e) => {527 drag.current = { x: e.clientX, id: e.pointerId };528 }}529 onPointerUp={(e) => {530 const d = drag.current;531 drag.current = null;532 if (!d) return;533 const dx = e.clientX - d.x;534 if (Math.abs(dx) > 40) go(dx < 0 ? 1 : -1);535 }}536 >537 <UICanvas pauseWhenOffscreen={pauseWhenOffscreen} lowPowerMode={lowPowerMode}>538 <Suspense fallback={null}>539 <Slides images={images} displacementImage={displacementImage} intensity={intensity} speed={speed} index={index} direction={direction} onSettled={() => (busy.current = false)} />540 </Suspense>541 </UICanvas>542 <div style={{ position: "absolute", inset: "auto 0 20px 0", display: "flex", alignItems: "center", justifyContent: "center", gap: 14 }}>543 <button type="button" aria-label="Previous image" style={ARROW} onClick={() => go(-1)}>544 ←545 </button>546 <div style={{ display: "flex", gap: 6 }}>547 {images.map((_, i) => (548 <button549 key={i}550 type="button"551 aria-label={`Image ${i + 1} of ${count}`}552 aria-current={i === index}553 onClick={() => i !== index && go(i - index)}554 style={{ width: i === index ? 22 : 8, height: 8, borderRadius: 999, border: 0, padding: 0, background: i === index ? "white" : "rgba(255,255,255,0.4)", cursor: "pointer", transition: "width 0.3s" }}555 />556 ))}557 </div>558 <button type="button" aria-label="Next image" style={ARROW} onClick={() => go(1)}>559 →560 </button>561 </div>562 <span aria-live="polite" style={{ position: "absolute", width: 1, height: 1, overflow: "hidden", clip: "rect(0,0,0,0)" }}>563 Image {index + 1} of {count}564 </span>565 </div>566 );567}568569export default memo(LiquidSlider3D);
03
Drop it in a page
Updated live with the settings above.
app/page.tsx
1import LiquidSlider3D from "@/components/LiquidSlider3D";23export default function Section() {4 return (5 // The component fills this box: give it a position and a height6 <div style={{ position: "relative", width: "100%", height: "100vh" }}>7 <LiquidSlider3D8 images={[9 "/heart.jpg",10 "/cowboy.jpg",11 "/3d3.jpg",12 "/touching.jpg",13 "/blackhole.jpg",14 ]}15 intensity={0.35}16 speed={1}17 />18 </div>19 );20}