Communitygithub.com

phido0410/rive

在网页上实现 Rive 交互式动画(.riv 文件)——包括 @rive-app/webgl2 / canvas 运行时、React 钩子(useRive、数据绑定钩子)、状态机、数据绑定(视图模型)以及用于视频的 @remotion/rive。当用户提供 .riv 文件或要求使用 Rive、交互式吉祥物/图标或状态机驱动的动画时使用。

rive 是什么?

该技能提供了一套完整的工具链,用于在网页、React 和 Remotion 视频项目中加载、播放和控制 Rive 动画(.riv 文件)。它基于 Rive 官方运行时,支持 WebGL2 和 Canvas2D 渲染,并包含数据绑定(视图模型)来驱动状态机输入,取代了已弃用的 stateMachineInputs API。对于 React 开发者,技能提供了 useRive、useViewModel 和 useViewModelInstance 等钩子,以及便捷的 useViewModelInstanceNumber 和 useViewModelInstanceTrigger 等专用钩子,可直接从 .riv 文件读取属性并触发触发器。在 Remotion 中,使用 <RemotionRiveCanvas> 组件可以使动画与视频帧同步播放,但需要注意交互逻辑(如点击)在渲染时不会生效,因此应预先设计好时间线动画。技能适用于任何需要高性能、可编程动画的 Web 项目,特别是当设计团队使用 Rive 编辑器创建了包含状态机或视图模型的复杂交互式资产时。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/phido0410/claude-video-skills/tree/HEAD/skills/rive

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

rive 是做什么的?

检查日期 2026-10:@rive-app/canvas 2.45,@rive-app/react-canvas 4.37,@remotion/rive 4.0。

.riv 文件通过 Rive 编辑器(rive.app)制作。代码可以播放和控制它们,但无法创作它们。请向用户索取文件,或使用来自 Rive 市场的社区文件。

包

需求包
原生 JavaScript,推荐@rive-app/webgl2
原生 JavaScript,Canvas2D@rive-app/canvas(若无文本、布局、脚本、音频,用 @rive-app/canvas-lite)
React@rive-app/react-webgl2 或 @rive-app/react-canvas
Remotion@remotion/rive

原生

import { Rive } from '@rive-app/webgl2'

const r = new Rive({
  src: '/hero.riv',
  canvas: document.getElementById('canvas'),
  stateMachines: 'State Machine 1',  // 编辑器中的名称
  autoplay: true,
  autoBind: true,                     // 绑定默认视图模型实例
  onLoad: () => r.resizeDrawingSurfaceToCanvas(),
})
window.addEventListener('resize', () => r.resizeDrawingSurfaceToCanvas())
// 稍后:r.cleanup()

数据绑定(驱动 .riv 的当前方式)

状态机输入(r.stateMachineInputs(name),.value,.fire())在运行时类型中已弃用。仅对没有视图模型的旧文件使用它们。

const vmi = r.viewModelInstance          // 需要 autoBind: true
vmi.number('Progress').value = 0.75
vmi.string('Title').value = 'Hello'
vmi.boolean('Active').value = true
vmi.color('Accent').value = 0xFFFF5500    // ARGB
vmi.enum('Mood').value = 'Happy'
vmi.trigger('Jump').trigger()
vmi.number('Progress').on((e) => console.log(e.data))

React

import { useRive, useViewModel, useViewModelInstance,
  useViewModelInstanceNumber, useViewModelInstanceTrigger } from '@rive-app/react-canvas'

const { rive, RiveComponent } = useRive({ src: '/hero.riv', stateMachines: 'State Machine 1', autoplay: true })
const vm = useViewModel(rive)
const vmi = useViewModelInstance(vm, { useDefault: true, rive })
const { setValue: setProgress } = useViewModelInstanceNumber('Progress', vmi)
const { trigger: jump } = useViewModelInstanceTrigger('Jump', vmi)

return <RiveComponent style={{ width: 400, height: 400 }} onClick={jump} />

容器需要有显式大小;RiveComponent 会填充其父元素。

用于视频

在 Remotion 中使用来自 @remotion/rive 的 <RemotionRiveCanvas src="..." />——它会与当前帧同步播放文件(请检查 remotion-docs 了解其属性)。等待点击/悬停的交互式状态机逻辑在渲染时不会运行;请设计一个带有时间线动画的 .riv 文件用于视频用途。

Individual skills in this repo

This repo contains 4 individual skills — each has its own dedicated page.

相关技能