lottie is a cross-platform animation solution released by Airbnb around 2017. It can be applied to iOS, Android, React Native, and web. It parses AE animations through the Bodymovin plugin and exports json files that can render animations on mobile and web. Designers create animations using AE and export the corresponding json files with Bodymovin for the frontend, which can use these json files to generate 100% accurate animations.
Users can easily handle Lottie assets and add components in Galacean.
It is recommended that designers use base64 format for images when exporting Lottie files in AE, embedding them into the Lottie json file.
After obtaining the .json
file, developers need to upload the .json
file to the Galacean Editor. Use the upload button in the asset panel to select the "lottie" asset, choose a local lottie json file, and then upload it:
Select an entity, add the Lottie component, and choose the resource as the asset uploaded in the previous step to display and play the Lottie effect:
Developers can adjust various parameters in the properties panel to configure the Lottie:
Property | Description |
---|---|
resource | Select Lottie asset |
autoPlay | Auto play, default is auto |
isLooping | Loop play, default is loop |
speed | Play speed, 1 is the original speed, the larger the value, the faster the play |
priority | Render priority, the smaller the value, the higher the render priority |
Sometimes developers may need to dynamically set Lottie at runtime. Add the following code in the script component:
// 先找到 Lottie 所在的实体 lottieEntity,然后获取 Lottie 组件
const lottie = lottieEntity.getComponent(LottieAnimation);
// 设置 lottie 属性
lottie.speed = 2;
Sometimes developers only upload Lottie resources in the editor and dynamically create Lottie components when needed. Use the following method:
// 动态加载编辑器中的 Lottie 资源
const lottieResource = await engine.resourceManager.load({url: '/光球.json', type: 'EditorLottie'});
// 给一个实体添加 Lottie 组件
const lottie = entity.addComponent(LottieAnimation);
// 给 Lottie 组件设置 Lottie 资源
lottie.resource = lottieResource;
Additionally, the Lottie component provides 2 APIs to control animation play and pause:
Method | Description |
---|---|
play | Play animation, passing in the animation segment name will play a specific segment |
pause | Pause animation |
Often, we need to listen for the end of a Lottie animation to run some business logic. The play
method of LottieAnimation
returns a Promise
, making it easy to listen for the end of the animation:
const lottie = lottieEntity.getComponent(LottieAnimation);
await lottie.play();
// do something next..
The editor provides an animation slicing function, allowing you to cut the entire segment provided by the designer into multiple segments. Each segment needs to define three fields: segment name, start frame, and end frame.
This operation will add the lolitaAnimations
field in the Lottie protocol to implement animation slicing:
"lolitaAnimations": [
{
"name": "clip1",
"start": 0,
"end": 30
},
{
"name": "clip2",
"start": 50,
"end": 100
},
]
@galacean/engine-lottie is a secondary package of the Galacean Engine. When using Lottie in the project, ensure that this package is installed:
npm i @galacean/engine-lottie --save
When developing in Pro Code
mode, you need a json
file and an atlas
file to implement lottie
animations. Usually, the art team exports only the json
file through AE
. In this case, you need to use the tools-atlas-lottie CLI
tool to generate the atlas
file.
import { LottieAnimation } from "@galacean/engine-lottie";
// Load lottie json、atlas file with engine's `resourceManager`
engine.resourceManager.load({
urls: [
"https://gw.alipayobjects.com/os/bmw-prod/b46be138-e48b-4957-8071-7229661aba53.json",
"https://gw.alipayobjects.com/os/bmw-prod/6447fc36-db32-4834-9579-24fe33534f55.atlas"
],
type: 'lottie'
}).then((lottieEntity) => {
// Add lottie entity created to scene
root.addChild(lottieEntity);
// Get `LottieAnimation` component and play the animation
const lottie = lottieEntity.getComponent(LottieAnimation);
lottie.isLooping = true;
lottie.speed = 1;
lottie.play();
});
In business scenarios, there is often a need for 3D transformations, such as entrance animations for pop-ups. For example, with rotation, traditional lottie-web solutions can only rotate along the Z-axis (i.e., perpendicular to the screen's normal direction). Even if we achieve rotation along the X-axis or Y-axis in AE, it will be ignored when played with lottie-web.
Thanks to the unified architecture of the Galacean Engine 2D/3D engine, 3D transformation functions can be easily implemented.
Engine Version | Lottie Version |
---|---|
1.2.x | 1.1.0-beta.0 |
1.3.x | engine-1.3 |
1.4.x | engine-1.4 |