Motion Canvas — 用于解说视频的 TypeScript 动画引擎
一个使用生成器编程动画并带有实时编辑器的 TypeScript 库,非常适合制作解说视频、代码演示和教学内容。
概述
- 仓库: github.com/motion-canvas/motion-canvas (18K+ 星标)
- 语言: TypeScript
- 许可证: MIT
- 安装:
npm create @motion-canvas@latest - 文档: motioncanvas.io/docs
核心概念
项目设置
npm create @motion-canvas@latest my-animation
cd my-animation
npm install
npm start # 在 localhost:9000 打开编辑器
场景
每个动画由多个场景组成。场景是一个生成器函数:
import { makeScene2D } from "@motion-canvas/2d";
import { createRef } from "@motion-canvas/core";
import { Circle } from "@motion-canvas/2d/lib/components";
export default makeScene2D(function* (view) {
const circle = createRef<Circle>();
view.add(<Circle ref={circle} size={100} fill="#e13238" />);
// 动画播放圆形
yield* circle().scale(2, 0.6);
yield* circle().opacity(0, 0.3);
});
使用生成器驱动动画
yield* 关键字逐帧驱动动画:
// 顺序动画 (一个接一个)
yield* rect().position.x(300, 1); // 在 1 秒内向右移动
yield* rect().fill("#00ff00", 0.5); // 在 0.5 秒内改变颜色
// 并行动画 (同时进行)
yield* all(
rect().position.x(300, 1),
rect().rotation(360, 1),
);
// 链式动画 (带延迟)
yield* chain(
rect().opacity(1, 0.3),
waitFor(0.5),
rect().scale(2, 0.6),
);
// 等待指定时间
yield* waitFor(2); // 等待 2 秒
节点 (视觉元素)
import { Rect, Circle, Line, Txt, Img, Video, Layout } from "@motion-canvas/2d/lib/components";
// 矩形
<Rect width={200} height={100} fill="#333" radius={10} />
// 文本
<Txt text="Hello" fontSize={48} fill="white" fontFamily="monospace" />
// 圆形
<Circle size={80} fill="#e13238" />
// 图片
<Img src={myImage} width={400} />
// 布局 (Flexbox)
<Layout direction="column" gap={20} alignItems="center">
<Txt text="Title" />
<Rect width={200} height={50} />
</Layout>
信号 (响应式值)
import { createSignal } from "@motion-canvas/core";
const progress = createSignal(0);
// 在组件中使用
<Rect width={() => progress() * 400} height={20} fill="blue" />
// 驱动信号动画
yield* progress(1, 2); // 在 2 秒内从 0 动画到 1
缓动
import { easeInOutCubic, easeOutBounce, linear } from "@motion-canvas/core";
yield* rect().position.x(300, 1, easeInOutCubic);
yield* circle().scale(2, 0.8, easeOutBounce);
代码高亮
import { Code, lines, word } from "@motion-canvas/2d/lib/components";
const code = createRef<Code>();
view.add(
<Code
ref={code}
fontSize={24}
code={`function hello() {\n console.log("world");\n}`}
/>
);
// 高亮特定行
yield* code().selection(lines(1, 2), 0.3);
// 动画切换代码内容
yield* code().code(
`function hello() {\n return "world";\n}`,
0.6,
);
命令
# 启动编辑器 (实时预览)
npm start
# 渲染为视频
npx motion-canvas render
# 渲染特定场景
npx motion-canvas render --scene=myScene
最佳实践
- 使用 ref 作为动画目标: 对需要动画的节点使用
createRef<Circle>() - 基于生成器的流程: 按顺序思考 ——
yield*就是你的时间线 - 使用
all()实现并行: 将同时进行的动画分组 - 信号用于响应式数据: 多个节点依赖的数值使用信号
- 使用 Layout 组件实现 Flexbox: 自动定位子元素
- 保持场景简洁: 将长动画拆分为多个场景
- 使用编辑器: 实时预览和拖动时间轴非常有用
何时使用 Motion Canvas 与 Remotion
| Motion Canvas | Remotion | |
|---|---|---|
| 最佳用途 | 教育、解说 | 产品、营销 |
| 动画风格 | 命令式 (生成器) | 声明式 (React) |
| 编辑器 | 内置实时编辑器 | Remotion Studio |
| 代码动画 | 一等支持 | 手动实现 |
| 生态系统 | 正在成长 | 庞大 (npm 包) |
| 语音同步 | 内置音频同步 | 手动同步 |
来源
- 仓库: github.com/motion-canvas/motion-canvas
- 文档: motioncanvas.io/docs
- 示例: motioncanvas.io/docs/examples