packages/public/@babylonjs/lottiePlayer/readme.md
- This is a highly experimental feature, use it at your own risk :)
Play Lottie JSON animations on a lightweight Babylon ThinEngine using an OffscreenCanvas + Worker. Artwork is rendered as vectors on the GPU with a stencil-then-cover pipeline, so shapes stay crisp at any scale.
import { Player } from "@babylonjs/lottie-player";
const container = document.getElementById("myDiv") as HTMLDivElement;
const player = new Player();
await player.playAnimationAsync({
container,
animationSource: "/animations/hero.json", // or a parsed RawLottieAnimation object
variables: null,
configuration: { loopAnimation: true },
});
The public API of this package is formed by Player, LocalPlayer, AnimationConfiguration and the RawLottieAnimation type. All other files are internal implementation details.
Future updates could move or rename files and require you to update your references if you take dependencies on those files. Do not depend on the paths of those files either as they could be moved or renamed as part of the internal implementation.
You can pass a variables map through AnimationInput.variables. These variables will be used in:
You can use AnimationConfiguration to change certain parameters of the player. The most commonly used options are:
loopAnimation: when true, the animation restarts from the beginning after the last frame.backgroundColor: RGBA color used to clear the canvas before each frame. Defaults to opaque black; use an alpha of 0 for a transparent canvas.devicePixelRatio: rendering scale; set to 0 (the default) to follow the system device pixel ratio.supportDeviceLost: enable WebGL context-lost recovery.stopAtFrame: stop playback at a specific frame number (useful for visual testing).The following options are deprecated. They belonged to the previous sprite-atlas renderer and are still accepted so existing code keeps compiling, but they no longer have any effect: spriteAtlasWidth, spriteAtlasHeight, gapSize, spritesCapacity, scaleMultiplier, easingSteps, debug, and compatibility.
Player and LocalPlayer select renderer chunks automatically after parsing the animation:
No separate player class or configuration is required. Pre-warming Player loads the worker and base renderer code; animation-specific text or image chunks load after the animation data is known.
The player renders vectors on the GPU instead of rasterizing a sprite atlas. Two behavior changes are worth knowing about when upgrading:
devicePixelRatio: 0 the canvas previously used 2x-4x supersampling to keep atlas sprites crisp. The vector renderer uses multisampling instead, so it now follows the system device pixel ratio. Expect a smaller backing store and lower memory use; pass an explicit devicePixelRatio to override.For this player to work, if you are applying CSP to your website, you need the following headers: worker-src blob: script-src thedomainservingyourjs
worker-src is used to load the worker. To simplify configuration, we have a small wrapper over the real worker that loads it as a blob. script-src is used by the scripts the worker references, like the classes it needs from babylon.js to render. If your CSP is blocking the domain of those scripts, then the worker will fail. You can use 'self' if you are serving your .js from the same domain you are serving your site, or your domain if your .js is served from a separate domain like a CDN.
Prefer to use the Player class that uses an OffscreenCanvas and a worker thread. If for some reason that is not available to you (for example in browsers that do not support OffscreenCanvas), you can use LocalPlayer instead and call playAnimationAsync, which renders on the main JS thread with the same AnimationInput shape.
Only certain features of the Lottie format are currently supported:
Notable Lottie features that are not currently supported include precomp and audio layers, layer effects, expressions, subtract/intersect/inverted masks, luma/inverted/cross-renderer track mattes, trim/repeater/merge-paths and other shape modifiers, and per-character text animators.
More features may be added in the future but there is no timeline for them.
- This is a highly experimental feature, use it at your own risk :)