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文件描述关键帧属性。它们以矢量方式在任何分辨率下渲染,文件体积小巧,并能原生在Web、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 | 预合成 | 嵌套合成 |
| 1 | 纯色 | 纯色图层 |
| 2 | 图片 | 图片资源图层 |
| 3 | 空对象 | 空变换父级 |
| 4 | 形状 | 矢量形状(最常见) |
| 5 | 文本 | 文本图层 |
形状类型
| 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));
在Web中播放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是Web Lottie动画的标准帧率
- 微交互控制在2秒以内,Logo动画控制在3-5秒
- 用lottie-player测试 — 在嵌入之前预览效果
- 优化 — 移除未使用的属性,精简数字精度
技能来源
- Wiggle(Logo动画技能):github.com/talknerdytome-labs/wiggle-claude-skill
- Lottie Tools(编辑器):github.com/marciogranzotto/lottie-tools
- OmniLottie(AI生成):github.com/OpenVGLab/OmniLottie