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 vs Remotion
| Motion Canvas | Remotion | |
|---|---|---|
| 最適合 | 教育、解說 | 產品、行銷 |
| 動畫風格 | 命令式(生成器) | 宣告式(React) |
| 編輯器 | 內建即時編輯器 | Remotion Studio |
| 程式碼動畫 | 一級支援 | 需手動實作 |
| 生態系統 | 成長中 | 龐大(npm 套件) |
| 語音同步 | 內建音訊同步 | 需手動同步 |
來源
- 倉庫:github.com/motion-canvas/motion-canvas
- 文件:motioncanvas.io/docs
- 範例:motioncanvas.io/docs/examples