Lottie 動畫 — 以程式碼建立的 JSON 動畫
以程式碼建立動畫 Logo、圖示與微互動,輸出輕量的 Lottie JSON 檔。無需 After Effects,全在程式碼中完成。
概覽
- 格式:Lottie JSON(開放標準)
- 播放器儲存庫:github.com/LottieFiles/lottie-player
- 編輯器儲存庫:github.com/marciogranzotto/lottie-tools(開放原始碼網頁編輯器)
- 授權:MIT
- 安裝方式:
npm install lottie-web或npm install @lottiefiles/lottie-player
什麼是 Lottie?
Lottie 是以 JSON 為基礎的動畫格式。動畫以關鍵幀屬性的結構化 JSON 檔案描述。它能以任何解析度渲染(基於向量)、檔案體積小,並可原生在網頁、iOS、Android 與 React Native 上播放。
Lottie JSON 結構
{
"v": "5.7.1", // Lottie 版本
"fr": 30, // 幀率
"ip": 0, // 起始幀
"op": 60, // 結束幀 — 30fps 下為 2 秒
"w": 400, // 寬度
"h": 400, // 高度
"assets": [], // 外部資源(圖片、預合成)
"layers": [ // 動畫圖層(由下至上)
{
"ty": 4, // 形狀圖層
"nm": "Circle", // 圖層名稱
"ip": 0,
"op": 60,
"st": 0, // 開始時間
"ks": { // 變形屬性
"o": { "a": 0, "k": 100 }, // 不透明度(靜態)
"r": { "a": 0, "k": 0 }, // 旋轉
"p": { // 位置(已動畫化)
"a": 1, // a=1 表示已動畫化
"k": [
{ "t": 0, "s": [200, 300], "e": [200, 100], "i": {"x": 0.5, "y": 1}, "o": {"x": 0.5, "y": 0} },
{ "t": 30, "s": [200, 100] }
]
},
"s": { "a": 0, "k": [100, 100] } // 縮放
},
"shapes": [...] // 形狀定義
}
]
}
圖層類型
| ty | 類型 | 說明 |
|---|---|---|
| 0 | Precomp | 巢狀合成 |
| 1 | Solid | 純色圖層 |
| 2 | Image | 圖片資源圖層 |
| 3 | Null | 變形父層(空白) |
| 4 | Shape | 向量形狀(最常見) |
| 5 | Text | 文字圖層 |
形狀類型
| ty | 形狀 |
|---|---|
| "rc" | 矩形 |
| "el" | 橢圓 |
| "sr" | 星形/多邊形 |
| "sh" | 路徑(貝茲曲線) |
| "fl" | 填色 |
| "st" | 描邊 |
| "gf" | 漸層填色 |
| "gs" | 漸層描邊 |
| "tr" | 變形 |
| "gr" | 群組 |
以程式碼建立動畫
簡單的淡入圓形
{
"v": "5.7.1",
"fr": 30,
"ip": 0,
"op": 30,
"w": 200,
"h": 200,
"layers": [{
"ty": 4,
"nm": "circle",
"ip": 0,
"op": 30,
"st": 0,
"ks": {
"o": {
"a": 1,
"k": [
{"t": 0, "s": [0], "e": [100], "i": {"x": [0.4], "y": [1]}, "o": {"x": [0.6], "y": [0]}},
{"t": 20, "s": [100]}
]
},
"r": {"a": 0, "k": 0},
"p": {"a": 0, "k": [100, 100]},
"s": {
"a": 1,
"k": [
{"t": 0, "s": [0, 0], "e": [100, 100], "i": {"x": [0.2, 0.2], "y": [1, 1]}, "o": {"x": [0.8, 0.8], "y": [0, 0]}},
{"t": 25, "s": [100, 100]}
]
}
},
"shapes": [
{
"ty": "gr",
"it": [
{"ty": "el", "p": {"a": 0, "k": [0, 0]}, "s": {"a": 0, "k": [80, 80]}},
{"ty": "fl", "c": {"a": 0, "k": [0.2, 0.5, 1, 1]}, "o": {"a": 0, "k": 100}},
{"ty": "tr", "p": {"a": 0, "k": [0, 0]}, "s": {"a": 0, "k": [100, 100]}}
]
}
]
}]
}
關鍵幀緩動
// 淡入淡出(平滑)
"i": {"x": [0.42], "y": [1]},
"o": {"x": [0.58], "y": [0]}
// 淡出(減速)
"i": {"x": [0.0], "y": [1]},
"o": {"x": [0.6], "y": [0]}
// 淡入(加速)
"i": {"x": [0.4], "y": [1]},
"o": {"x": [1.0], "y": [0]}
// 線性
"i": {"x": [1], "y": [1]},
"o": {"x": [0], "y": [0]}
以 JavaScript 程式化生成
function createLottieAnimation({ width, height, fps, durationFrames, layers }) {
return {
v: "5.7.1",
fr: fps,
ip: 0,
op: durationFrames,
w: width,
h: height,
assets: [],
layers,
};
}
function createShapeLayer({ name, startFrame, endFrame, shapes, transform }) {
return {
ty: 4,
nm: name,
ip: startFrame,
op: endFrame,
st: startFrame,
ks: transform,
shapes,
};
}
// 建立並儲存
const animation = createLottieAnimation({
width: 400, height: 400, fps: 30, durationFrames: 60,
layers: [/* ... */],
});
const fs = require("fs");
fs.writeFileSync("animation.json", JSON.stringify(animation, null, 2));
在網頁中播放 Lottie
<!-- 透過 CDN -->
<script src="https://unpkg.com/@lottiefiles/lottie-player@latest/dist/lottie-player.js"></script>
<lottie-player src="animation.json" background="transparent" speed="1" loop autoplay></lottie-player>
// 透過 npm
import lottie from "lottie-web";
const anim = lottie.loadAnimation({
container: document.getElementById("lottie"),
renderer: "svg",
loop: true,
autoplay: true,
animationData: require("./animation.json"),
});
在 Remotion 中使用 Lottie
import { Lottie } from "@remotion/lottie";
import animationData from "./animation.json";
export const MyScene: React.FC = () => {
return <Lottie animationData={animationData} />;
};
安裝方式:npm install @remotion/lottie
最佳實踐
- 保持簡單 — 圖層越少 = 檔案越小、效能越好
- 盡量使用形狀圖層而非圖片圖層(向量在任何尺寸下都清晰)
- 善用關鍵幀緩動 — 自然動態請勿使用線性緩動
- 30fps 是網頁 Lottie 動畫的標準
- 低於 2 秒適用於微互動,3-5 秒適用於 Logo
- 使用 lottie-player 測試 — 嵌入前先預覽
- 最佳化 — 移除未使用的屬性,數值精確度降至最低
Agent 技能來源
- Wiggle(Logo 動畫技能):github.com/talknerdytome-labs/wiggle-claude-skill
- Lottie Tools(編輯器):github.com/marciogranzotto/lottie-tools
- OmniLottie(AI 生成):github.com/OpenVGLab/OmniLottie