mcpField
T
theatre-js

theatre-js

github.com/jbmanllr设计与 UI199 次安装39 周前更新
代码仓库

关于 theatre-js

theatre-js 是 Theatre.js 动画库与实践指南,面向制作 Web 动效的开发者。它介绍可视化时间线编辑器 Studio 与程序化控制,并提供 React 和 React Three Fiber 集成及生产部署参考。

主要功能

  1. 01Studio 可视化时间线编辑
  2. 02项目、对象与序列管理
  3. 03关键帧动画播放控制
  4. 04React 与 R3F 集成模式
  5. 05状态导出和生产部署指南

适用场景

  1. 01为 DOM 元素制作时间线动画
  2. 02在 React 组件中读取动画值
  3. 03编辑 React Three Fiber 场景
  4. 04导出动画状态用于生产环境

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/jbmanllr/agent-skills --skill theatre-js

安装后在 Claude Code 中输入/theatre-js确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

README.md
references/
01-core.md
02-prop-types.md
03-studio.md
04-react-integration.md
05-r3f-integration.md
06-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.mdStudio UI、键盘快捷键、扩展、面板
references/04-react-integration.mduseVal、usePrism、@theatre/react hooks
references/05-r3f-integration.mdReact 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 值 hookuseVal(obj.props.x)
导出状态Studio → Project → Export (JSON)

评论

支持 Markdown