在 React 和 Next.js 中使用动态渐变背景
Ribbonwash 会把动态网格渐变导出为开箱即用的 React 组件 MeshBackground.jsx。渐变铺满组件,内容渲染在它上面。可用于 Vite、Create React App 和 Next.js,唯一的依赖是 React 17 及以上。
4 步加进你的应用
设计渐变
在编辑器里做一个场景,或者从模板(作品 → 新建作品…)开始。
导出 React 组件
选择 导出 → 代码…。React 组件 标签页会显示
MeshBackground.jsx,用 完整 ZIP → 下载 ZIP 可以连同所需文件一起拿到(代码导出是完整版($19,一次买断)功能)。复制 react/ 文件夹
解压 ZIP,把
react/文件夹复制到你的项目里。任何能 import JSON 的打包工具都可以——Vite、Next.js、Create React App。渲染
用
import MeshBackground from './react/MeshBackground'包住你的首屏区块。渐变会铺满组件,children 渲染在它上面。
react/ 文件夹里有什么
| 文件 | 内容 |
|---|---|
MeshBackground.jsx | 组件本身(见下文)。 |
Example.jsx | 全屏使用示例。 |
mesh-runtime-react.js | 运行时和 React 包装合成一个文件,没有其他依赖。 |
scene.json | 你的场景:曲线、颜色、动效。 |
poster.js | 首帧图片,在 WebGL 启动前以及动画无法运行时显示。 |
MeshBackground.jsx
这就是导出生成的文件,原样照录:
// 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>
);
}
还有 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 | 作用 |
|---|---|
children | 渲染在渐变上方的内容。 |
style, className | 作用于外层 <div>。在这里设定高度(例如 minHeight: '100vh')。 |
playing | 默认 true。设为 false 暂停,true 继续。 |
onReady | 以渐变实例(pause()、play()、destroy())为参数调用一次。 |
speed, fit, pixelRatio, mode | 运行时选项:播放速度、'cover' 适配(默认)、设备像素比上限、'embed' 模式(默认)。 |
degradeBelowFps, freezeBelowFps, freezeAfterMs | 性能保护:低于 24 fps 降低分辨率,连续 4000 ms 低于 15 fps 停在静态画面(默认值)。 |
respectReducedMotion | 默认 true:对开启“减少动态效果”的访客显示静态画面。 |
运行时选项只在组件挂载时读取一次;playing 和包装组件的 scene prop 会即时生效。播放 / 暂停切换:
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)与 SSR
渐变需要浏览器(WebGL 和 effect hook),所以要在 Client Component 里渲染。在使用它的文件顶部加上 'use client'。
// 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 />;
}
- 服务端渲染。在服务端 import 运行时是安全的。服务端渲染会输出一个以首帧图为 CSS 背景的容器
<div>,所以第一次绘制就能看到渐变的首帧。 - 在浏览器里。canvas 在水合后于 effect 中创建,组件卸载时销毁。
- Pages Router。不需要指令,像其他组件一样 import 并渲染即可。
体积与性能
- gzip 后约 19 KB。这是合并了运行时和包装的
mesh-runtime-react.js的大小,不含 React 本身。 - scene.json 只包含曲线数据;poster.js 是以 data URL 内联的首帧 JPEG(长边 640 px)。大小因作品而异。
- 照顾弱设备。低于 24 fps 降低分辨率,连续 4 秒低于 15 fps 停在静态画面。
- 无障碍。开启
prefers-reduced-motion时显示静态画面,没有 WebGL2 时显示首帧图。渐变层是pointer-events: none,不会挡住点击。
常见问题
能在 Next.js 的 Server Components 里用吗?
可以,作为 Client Component 使用。在渲染 MeshBackground 的文件顶部写上 'use client',然后在任意服务端页面或布局里使用这个组件。服务端会渲染一个以首帧图为背景的空容器,canvas 在浏览器里启动。
支持哪些 React 版本?
React 和 React DOM 17 及以上。没有其他依赖。
可以在运行时切换场景吗?
包装组件会在不重新挂载的情况下应用新的 scene prop。导出的 MeshBackground.jsx 会 import 自己的 scene.json,所以要切换场景,请把它改成通过 prop 接收 scene,再传入其他导出的 JSON 文件。
上线后需要授权码吗?
不需要。导出的组件和运行时没有授权检查,也不联网,离线也能一直运行。