关于 theatre-js
theatre-js 是面向 Web 动效设计与动画的库,提供可视化时间轴编辑器和程序化控制。本技能面向开发者,介绍其核心模型、React 与 React Three Fiber 用法及生产环境配置。
主要功能
- 01使用 Studio 可视化编辑时间轴
- 02项目、画布、对象与序列模型
- 03类型化动画属性与关键帧
- 04集成 React 和 React Three Fiber
- 05播放控制与状态导出
适用场景
- 01制作动画 DOM 界面
- 02为 React Three Fiber 场景编排时间轴
- 03通过代码控制动画播放
- 04导出动画状态用于生产环境
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ noklip-io/ agent-skills --skill theatre-js安装后在 Claude Code 中输入/theatre-js确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
README.mdreferences/01-core.md02-prop-types.md03-studio.md04-react-integration.md05-r3f-integration.md06-production.md详细介绍
Theatre.js 最佳实践
面向 Web 的动作设计编辑器和动画库。提供可视化时间轴编辑器(Studio),并支持通过代码控制。
安装
# Core + Studio (development)
npm install @theatre/[email protected] @theatre/[email protected]
# React Three Fiber integration
npm install @theatre/[email protected]
# React utilities
npm install @theatre/react
# Theatric (simplified controls)
npm install theatric
快速入门
import * as core from '@theatre/core'
import studio from '@theatre/studio'
// Initialize Studio (dev only)
if (import.meta.env.DEV) {
studio.initialize()
}
// Create project → sheet → object
const project = core.getProject('My Project')
const sheet = project.sheet('Main')
const obj = sheet.object('Box', {
position: { x: 0, y: 0 },
opacity: 1
})
// Read values
console.log(obj.value.position.x)
// Listen to changes
obj.onValuesChange((values) => {
element.style.opacity = values.opacity
element.style.transform = `translate(${values.position.x}px, ${values.position.y}px)`
})
// Play animation
sheet.sequence.play({ iterationCount: Infinity })
核心概念
| 概念 | 说明 |
|---|---|
| Project | 所有动画数据的容器;对应导出的 JSON 状态 |
| Sheet | 一个场景或组件;包含对象和一个序列 |
| Object | 具有类型化属性的可动画实体 |
| Sequence | 包含关键帧的时间轴;控制播放 |
| Props | 类型化值(number、compound、rgba 等) |
参考索引
| 参考 | 适用场景 |
|---|---|
references/01-core.md | 项目设置、工作表、对象、序列、播放控制 |
references/02-prop-types.md | 定义属性、自定义类型、复合属性、约束 |
references/03-studio.md | Studio UI、键盘快捷键、扩展、面板 |
references/04-react-integration.md | useVal、usePrism、@theatre/react hooks |
references/05-r3f-integration.md | React Three Fiber、可编辑组件、SheetProvider |
references/06-production.md | 导出状态、资源、部署、tree-shaking |
常见模式
为 DOM 元素添加动画
const obj = sheet.object('Card', {
x: 0,
y: 0,
rotation: 0,
scale: 1,
opacity: 1
})
obj.onValuesChange(({ x, y, rotation, scale, opacity }) => {
element.style.transform = `translate(${x}px, ${y}px) rotate(${rotation}deg) scale(${scale})`
element.style.opacity = opacity
})
序列播放控制
const seq = sheet.sequence
// Play once
seq.play()
// Play with options
seq.play({
iterationCount: Infinity, // loop forever
range: [0, 2], // play seconds 0-2
rate: 1.5, // 1.5x speed
direction: 'alternate' // ping-pong
})
// Pause and seek
seq.pause()
seq.position = 1.5 // jump to 1.5s
// Await completion
await seq.play({ iterationCount: 1 })
console.log('Animation complete')
React Three Fiber 场景
import { Canvas } from '@react-three/fiber'
import { editable as e, SheetProvider } from '@theatre/r3f'
import { getProject } from '@theatre/core'
import studio from '@theatre/studio'
import extension from '@theatre/r3f/dist/extension'
// Dev setup
if (import.meta.env.DEV) {
studio.initialize()
studio.extend(extension)
}
const sheet = getProject('R3F Demo').sheet('Scene')
function App() {
return (
<Canvas>
<SheetProvider sheet={sheet}>
<e.mesh theatreKey="Cube">
<boxGeometry />
<meshStandardMaterial color="orange" />
</e.mesh>
<e.pointLight theatreKey="Light" position={[10, 10, 10]} />
</SheetProvider>
</Canvas>
)
}
Theatric 控件(简单)
import { useControls, types } from 'theatric'
function Component() {
const { color, intensity, position } = useControls({
color: '#ff0000',
intensity: types.number(1, { range: [0, 2] }),
position: { x: 0, y: 0, z: 0 }
})
return <mesh position={[position.x, position.y, position.z]} />
}
需要避免的关键错误
1. 在生产环境中启用 Studio
// ❌ Includes studio in bundle
import studio from '@theatre/studio'
studio.initialize()
// ✅ Dev-only initialization
if (import.meta.env.DEV) {
studio.initialize()
}
2. 生产环境缺少状态
// ❌ No animations without state
const project = core.getProject('My Project')
// ✅ Load exported state
import state from './state.json'
const project = core.getProject('My Project', { state })
3. 对象键冲突
// ❌ Same key = same object (shared state)
sheet.object('Box', { x: 0 })
sheet.object('Box', { y: 0 }) // Overwrites!
// ✅ Unique keys per object
sheet.object('Box1', { x: 0 })
sheet.object('Box2', { y: 0 })
4. 缺少 R3F 扩展
// ❌ No 3D controls in Studio
studio.initialize()
// ✅ Extend with R3F extension
import extension from '@theatre/r3f/dist/extension'
studio.initialize()
studio.extend(extension)
5. 忘记 theatreKey
// ❌ Not editable
<e.mesh>
// ✅ Requires theatreKey
<e.mesh theatreKey="MyCube">
快速参考
| 任务 | 解决方案 |
|---|---|
| 创建项目 | getProject('Name', { state? }) |
| 创建工作表 | project.sheet('Name') |
| 创建对象 | sheet.object('Key', { props }) |
| 监听值 | obj.onValuesChange(cb) |
| 读取值 | obj.value.propName |
| 播放动画 | sheet.sequence.play(opts?) |
| 暂停动画 | sheet.sequence.pause() |
| 跳转到指定位置 | sheet.sequence.position = 1.5 |
| 可编辑的 R3F | <e.mesh theatreKey="Key"> |
| React 值 hook | useVal(obj.props.x) |
| 导出状态 | Studio → Project → Export (JSON) |
评论
支持 Markdown