Lottie for Web
Lottie-web is a JavaScript library designed to render animations exported from Adobe After Effects as JSON files, leveraging the Bodymovin plugin. It enables designers to deliver complex, vector-based animations across web platforms without the need for manual re-coding by engineers, significantly streamlining the animation pipeline. The package, as of the provided context, is at version 5.13.0, which includes critical bug fixes and improvements, notably around Node.js compatibility for SSR. Lottie-web is actively maintained, with new features and bug fixes released periodically, though major versions with breaking changes (like the recent v6) are less frequent. Key differentiators include its tight integration with After Effects, robust performance across SVG and Canvas renderers, and a comprehensive API for controlling animation playback, speed, and segments. It is a core component of the broader Lottie ecosystem, alongside its counterparts for Android, iOS, and React Native, providing a unified animation solution.
Common errors
-
TypeError: lottie.loadAnimation is not a function
cause This error typically occurs if `lottie-web` was not imported correctly, or if a v6 API call is attempted in an environment where an older version (e.g., 5.x) is loaded, or vice-versa. It can also happen if attempting to call `loadAnimation` before the `lottie` object is fully available.fixVerify the import statement (`import lottie from 'lottie-web';` or `const lottie = require('lottie-web');`). Ensure the correct version of `lottie-web` is installed for your code's API usage. If using a `<script>` tag, ensure it's loaded before your animation logic. -
Cannot read properties of undefined (reading 'play') / TypeError: anim.play is not a function
cause This usually means the `animationInstance` (returned by `lottie.loadAnimation`) was not stored, or the animation failed to load, resulting in `undefined`. It can also occur if attempting to call global `lottie.play()` in v6.fixAlways store the return value of `lottie.loadAnimation()` into a variable (e.g., `const anim = lottie.loadAnimation(...)`). Add checks (`if (anim) { anim.play(); }`) to ensure the instance exists before interacting with it. For v6, ensure you're calling `play()` on the instance, not the global `lottie` object. -
Animation not visible or not playing after loading.
cause Common causes include an incorrect `container` DOM element reference, an invalid or unreachable `path` to the animation JSON, `autoplay` being set to `false`, or issues with CSS on the container element (e.g., `display: none`, zero height/width).fixDouble-check the `container` element's ID and existence, the `path` to your `.json` file, and ensure `autoplay` is `true` if immediate playback is desired. Verify the container has sufficient `width` and `height` and is visible in the DOM. Check browser console for network errors loading the JSON. -
Images or other assets within the Lottie animation are not loading or appearing.
cause This indicates that `lottie-web` cannot find the associated image files referenced in the animation JSON. This is usually due to incorrect `assetsPath` configuration or incorrect relative paths in the JSON itself.fixEnsure that the `assetsPath` option in `lottie.loadAnimation` correctly points to the directory containing your animation's images. If no `assetsPath` is specified, images are expected to be relative to the animation JSON file or the page's base URL.
Warnings
- breaking Lottie-web v6.0.0 introduced significant breaking changes. The `lottie.loadAnimation` signature changed from accepting an options object to positional arguments (e.g., `container` is now the first argument). Many global methods like `lottie.destroy()` or `lottie.setSpeed()` were removed, and their functionality moved to methods on the individual `AnimationItem` instances.
- breaking Starting with v6.0.0, the UMD (Universal Module Definition) bundle was dropped. This means direct inclusion via a `<script>` tag might require different assets or build configurations. The library now primarily supports ES Modules and CommonJS.
- gotcha Animations, especially those loaded dynamically or in single-page applications, can cause memory leaks if not properly destroyed when no longer needed. This can lead to performance degradation over time.
- gotcha If your Lottie animation JSON includes external assets like images, their paths must be correctly configured using the `assetsPath` option in `loadAnimation` or by ensuring relative paths are accurate. Incorrect paths will result in missing images.
- gotcha Choosing the correct renderer (`'svg'`, `'canvas'`, or `'html'`) is crucial for performance and fidelity. SVG offers vector scalability but can be heavy for complex animations with many layers. Canvas can be faster for raster-heavy or very complex animations but lacks the DOM manipulability of SVG. HTML renderer is less common but can be useful for certain effects.
- deprecated While still actively maintained by Airbnb, some community discussions suggest a potential shift or deprecation of `lottie-web` in favor of `@lottiefiles/dotlottie-web` for certain use cases, especially for `.lottie` file format support and WebAssembly/Web Worker benefits, though this is not an official Airbnb deprecation.
Install
-
npm install lottie-web -
yarn add lottie-web -
pnpm add lottie-web
Imports
- lottie
const lottie = require('lottie-web');import lottie from 'lottie-web';
- lottie.loadAnimation
lottie.loadAnimation(document.getElementById('anim'), { path: '/data.json' });lottie.loadAnimation({ container: document.getElementById('anim'), path: '/data.json' }); - AnimationItem (instance)
lottie.play('myAnim'); lottie.destroy('myAnim');const anim = lottie.loadAnimation(...); anim.play(); anim.destroy();
Quickstart
import lottie from 'lottie-web';
const animationData = {
v: '5.10.0',
fr: 60,
ip: 0,
op: 180,
w: 500,
h: 500,
nm: 'Simple Animation',
ddd: 0,
assets: [],
layers: [
{
ddd: 0,
ind: 1,
ty: 4,
nm: 'Square',
sr: 1,
ks: {
o: { a: 0, k: 100, ix: 11 },
r: { a: 0, k: 0, ix: 10 },
p: { a: 1, k: [{t:0, s:[250,250,0]}, {t:90, s:[100,250,0]}, {t:180, s:[250,250,0]}], ix: 2},
a: { a: 0, k: [25, 25, 0], ix: 1},
s: { a: 0, k: [100, 100, 100], ix: 6}
},
ao: 0,
shapes: [
{
ty: 'gr',
it: [
{ ind: 0, ty: 'sh', ix: 1, ks: { a: 0, k: { i: [[-25,-25],[25,-25],[-25,25],[25,25]], o: [[25,-25],[-25,-25],[25,25],[-25,25]], v: [[-25,25],[25,25],[25,-25],[-25,-25]] }, ix: 2},
{ ind: 1, ty: 'fl', ix: 2, c: { a: 0, k: [0.7,0.2,0.1,1], ix: 3}, o: { a: 0, k: 100, ix: 4}},
{ ind: 2, ty: 'st', ix: 3, c: { a: 0, k: [0,0,0,1], ix: 5}, o: { a: 0, k: 100, ix: 6}, w: { a: 0, k: 2, ix: 7}, lc: 1, lj: 1, ml: 4}
],
nm: 'Group 1', mn: 'ADBE Vector Group', hd: false
}
],
ip: 0, op: 180, st: 0, bm: 0
}
],
markers: []
};
function initLottieAnimation() {
const container = document.getElementById('lottie-container');
if (!container) {
console.error('Lottie container not found!');
return;
}
const anim = lottie.loadAnimation({
container: container,
renderer: 'svg', // Can be 'svg', 'canvas', or 'html'
loop: true,
autoplay: true,
animationData: animationData // Or use 'path: "/path/to/animation.json"'
});
// Optional: Control animation playback
container.addEventListener('mouseenter', () => anim.pause());
container.addEventListener('mouseleave', () => anim.play());
}
document.addEventListener('DOMContentLoaded', initLottieAnimation);
// To make it runnable in a browser, you'd typically have this in an HTML file:
/*
<div id="lottie-container" style="width: 300px; height: 300px; background-color: #f0f0f0; border: 1px solid #ccc;"></div>
<script type="module" src="./main.js"></script>
*/