Animated gradient background in React and Next.js
Ribbonwash exports your animated mesh gradient as a ready-made React component, MeshBackground.jsx. The gradient fills the component and your content renders on top. It works with Vite, Create React App and Next.js, and React 17 or newer is its only dependency.
Add it to your app in 4 steps
Design the gradient
Make your scene in the editor, or start from a template (Artworks → New artwork…).
Export the React component
Choose Export → Code…. The React component tab shows
MeshBackground.jsx; Complete ZIP → Download ZIP gives you it together with the files it needs (code export is part of Pro ($19, one-time)).Copy the react/ folder
Unzip and copy the
react/folder into your project. It works with any bundler that can import JSON — Vite, Next.js, Create React App.Render it
import MeshBackground from './react/MeshBackground'and wrap your hero in it. The gradient fills the component; children render on top.
What's in the react/ folder
| File | What it is |
|---|---|
MeshBackground.jsx | The component (below). |
Example.jsx | A full-screen usage example. |
mesh-runtime-react.js | The runtime and the React wrapper in one file, with no other dependency. |
scene.json | Your scene: curves, colors and motion. |
poster.js | The first frame as an image. It shows before WebGL starts and whenever the animation can't run. |
MeshBackground.jsx
This is the file the export writes, unchanged:
// Ribbonwash · mesh gradient background — React 17+
// 用法 / Usage:
// import MeshBackground from './MeshBackground';
// <MeshBackground style={{ minHeight: '100vh' }}><h1>Hello</h1></MeshBackground>
// 渐变铺满组件作为背景,children 显示在上层。 The gradient fills the component; children render on top.
import { MeshGradientView } from './mesh-runtime-react.js';
import scene from './scene.json';
import poster from './poster.js';
export default function MeshBackground({ children, className, style, playing = true, ...options }) {
return (
<div className={className} style={{ position: 'relative', overflow: 'hidden', ...style }}>
<MeshGradientView
scene={scene}
playing={playing}
poster={poster}
{...options}
style={{ position: 'absolute', inset: 0, zIndex: 0, pointerEvents: 'none',
background: `${scene.background} url(${poster}) center / cover no-repeat` }}
/>
<div style={{ position: 'relative', zIndex: 1 }}>{children}</div>
</div>
);
}
And Example.jsx:
import MeshBackground from './MeshBackground';
export default function Example() {
return (
<MeshBackground style={{ minHeight: '100vh', display: 'grid', placeItems: 'center', color: '#fff' }}>
<h1>Hello, gradient</h1>
</MeshBackground>
);
}Props
| Prop | What it does |
|---|---|
children | Your content, rendered above the gradient. |
style, className | Applied to the outer <div>. Give it a height here (for example minHeight: '100vh'). |
playing | true by default. Pass false to pause; true resumes. |
onReady | Called once with the gradient instance (pause(), play(), destroy()). |
speed, fit, pixelRatio, mode | Runtime options: playback speed, 'cover' fit (default), the device-pixel-ratio cap, and 'embed' mode (default). |
degradeBelowFps, freezeBelowFps, freezeAfterMs | Performance guard: lower resolution below 24 fps, hold a still frame below 15 fps for 4000 ms (the defaults). |
respectReducedMotion | true by default: visitors who prefer reduced motion see a still frame. |
Runtime options are read once, when the component mounts. playing and the wrapper's scene prop are applied live. A play/pause toggle:
import { useState } from 'react';
import MeshBackground from './react/MeshBackground';
export default function Hero() {
const [playing, setPlaying] = useState(true);
return (
<MeshBackground playing={playing} style={{ minHeight: '70vh' }}>
<button onClick={() => setPlaying(p => !p)}>{playing ? 'Pause' : 'Play'}</button>
</MeshBackground>
);
}Next.js (App Router) and SSR
The gradient needs the browser (WebGL and an effect hook), so render it from a Client Component: add 'use client' to the top of the file that uses it.
// app/Hero.jsx
'use client';
import MeshBackground from './react/MeshBackground';
export default function Hero() {
return (
<MeshBackground style={{ minHeight: '100vh', display: 'grid', placeItems: 'center', color: '#fff' }}>
<h1>Hello, gradient</h1>
</MeshBackground>
);
}
// app/page.jsx (a Server Component)
import Hero from './Hero';
export default function Page() {
return <Hero />;
}
- Server render. Importing the runtime on the server is safe. Server rendering produces the container
<div>s with the poster as a CSS background, so the first paint already shows the gradient's first frame. - In the browser. The canvas is created after hydration, inside an effect, and is destroyed when the component unmounts.
- Pages Router. No directive is needed; import and render it like any other component.
Size and performance
- About 19 KB gzipped. That covers
mesh-runtime-react.js, the runtime plus the wrapper. React itself is not included. - scene.json holds only curve data, and poster.js is your first frame as a JPEG (640 px on the long side), inlined as a data URL. Its size depends on the artwork.
- Kind to weak devices. Below 24 fps it lowers its resolution; below 15 fps for 4 seconds it holds a still frame.
- Accessible. It shows a still frame under
prefers-reduced-motion, and the poster without WebGL2. The gradient layer haspointer-events: none, so it never blocks clicks.
FAQ
Does it work with Next.js Server Components?
Yes, as a Client Component. Put 'use client' at the top of the file that renders MeshBackground, then use that component from any server page or layout. The server renders the empty container with the poster as its background, and the canvas starts in the browser.
Which React versions are supported?
React and React DOM 17 or newer. There are no other dependencies.
Can I swap the scene at runtime?
The wrapper applies a new scene prop without remounting. The exported MeshBackground.jsx imports its own scene.json, so to switch scenes, edit it to take scene as a prop and pass in other exported JSON files.
Does production need a license key?
No. The exported component and runtime have no license check and never call home. They keep working offline, forever.