dotLottie Web Documentation

repository·main·Indexed 21 days ago

https://github.com/lottiefiles/dotlottie-web

A high-performance Lottie and dotLottie animation player powered by a Rust + WASM core. It supports Software, WebGL2, and WebGPU rendering backends, state machines, runtime theming, and audio. The library provides official wrappers for React (@lottiefiles/dotlottie-react), Svelte (@lottiefiles/dotlottie-svelte), Vue (@lottiefiles/dotlottie-vue), and Web Components (@lottiefiles/dotlottie-wc), as well as Node.js support for server-side playback and GIF conversion.

Tokens
41.7K
Snippets
140
Records
169
Agent score
73%

What's inside dotLottie Web

  1. Introduction to @lottiefiles/dotlottie-web

    main

    @lottiefiles/dotlottie-web is an isomorphic JavaScript library used for rendering Lottie and dotLottie animations in both web browsers and Node.js environments. It provides an API for loading, playing, and controlling animations, with support for advanced features like interactivity and theming.

    What is dotLottie?

    dotLottie is an open-source file format that bundles one or more Lottie animations and their assets into a single, compressed .lottie file using ZIP compression. This format is more efficient for storage and distribution and supports advanced features like interactivity and theming.

  2. Use DotLottieWorker for high performance

    main

    For complex animations, multiple simultaneous animations, or to keep the main thread free for UI interactions, use DotLottieWorker. This offloads rendering to a Web Worker.

    • Performance: Recommended for mobile devices and heavy JS environments.
    • Worker Grouping: Use the workerId option to group animations into separate workers. By default, all instances share the same worker.
    • React Support: Use DotLottieWorkerReact from @lottiefiles/dotlottie-react for a component-based approach with workers.
    import { DotLottieWorker } from '@lottiefiles/dotlottie-web';
    
    // Dedicated worker for hero animation
    const heroAnimation = new DotLottieWorker({
      canvas: heroCanvas,
      src: 'hero.lottie',
      workerId: 'hero-worker',
    });
  3. Work with multi-animation .lottie files

    main

    A single .lottie file can contain multiple animations. You can access the list of animations via the manifest property and switch between them using loadAnimation(id).

    // Get all animation IDs from the manifest
    const animations = dotLottie.manifest?.animations;
    // Returns: [{ id: 'animation-1' }, { id: 'animation-2' }]
    
    // Load a specific animation by its ID
    dotLottie.loadAnimation('animation-2');
  4. Switch between Software, WebGL2, and WebGPU backends

    main

    The player supports three rendering backends. While the default is Software (Canvas2D), you can opt into hardware-accelerated rendering by changing your import path. The DotLottie class and its API remain identical across all backends.

    // WebGL2 — broadly supported
    import { DotLottie } from '@lottiefiles/dotlottie-web/webgl';
    
    // WebGPU — experimental, modern Chromium / Safari TP
    import { DotLottie } from '@lottiefiles/dotlottie-web/webgpu';
  5. Use Web Workers for high-performance rendering

    main

    Use DotLottieWorker to offload animation rendering to a Web Worker. This keeps the main thread free for UI interactions, which is critical for complex animations, multiple simultaneous animations, or mobile devices.

    By default, all DotLottieWorker instances share the same worker. You can group animations into separate workers by providing a unique workerId.

    import { DotLottieWorker } from '@lottiefiles/dotlottie-web';
    
    // Basic Worker Usage
    const dotLottie = new DotLottieWorker({
      canvas: document.getElementById('canvas') as HTMLCanvasElement,
      src: 'https://example.com/animation.lottie',
      autoplay: true,
      loop: true,
    });
    
    // Worker Grouping
    const heroAnimation = new DotLottieWorker({
      canvas: heroCanvas,
      src: 'hero.lottie',
      workerId: 'hero-worker',
    });
  6. How Interactive State Machines work

    main

    dotLottie v2 supports state machines defined directly within the .lottie file. You can drive these animations by specifying a stateMachineId during initialization. The player emits typed events like stateMachineStart, stateMachineTransition, and stateMachineStateEntered to allow you to sync your application logic with the animation state.

    const dotLottie = new DotLottie({
      canvas: document.getElementById('canvas'),
      src: 'interactive-button.lottie',
      stateMachineId: 'main',
      autoplay: true,
    });
  7. Choose the right dotLottie package

    main

    Select a package based on your project requirements:

    • @lottiefiles/dotlottie-web: Use for framework-agnostic code, direct canvas control, maximum performance, or when you need the smallest bundle size.
    • @lottiefiles/dotlottie-react: Use for React applications where you want a declarative component API and React lifecycle integration.
    # Web (vanilla JS, Vue, Svelte, etc.)
    npm install @lottiefiles/dotlottie-web
    
    # React
    npm install @lottiefiles/dotlottie-react
  8. Optimize performance with Faster First Frame

    main

    The player uses a WASM engine (~500 KB compressed) that is fetched from a CDN when the first player is constructed. To prevent this download from blocking your first animation's critical path, you can preload the WASM engine.

    Option 1: Programmatic Preload

    Call DotLottie.preload() at the app or route load level, before any player instance is created.

    Option 2: HTML Preload Tag

    You can use a <link rel="preload"> tag to start the download even earlier.

    Important Requirements:

    1. crossorigin attribute is required: Without it, the browser cannot reuse the preloaded response, causing the file to download twice.
    2. Version Matching: The version in the URL must exactly match your installed package version. If you use setWasmUrl(), ensure the preload tag points to the same URL.
    import { DotLottie } from '@lottiefiles/dotlottie-web';
    
    // At app or route load, before any player is constructed:
    DotLottie.preload();
    <link rel="preconnect" href="https://cdn.jsdelivr.net" />
    <link
      rel="preload"
      as="fetch"
      crossorigin
      href="https://cdn.jsdelivr.net/npm/@lottiefiles/dotlottie-web@0.77.1/dist/dotlottie-player.wasm"
    />
  9. Install dotLottie guidelines for AI coding assistants

    main

    You can install dotLottie implementation guidelines as a skill or command for your AI coding assistant to improve its ability to write code using this library. This provides the assistant with context regarding package selection, state machines, theming, and performance best practices.

    curl -fsSL https://raw.githubusercontent.com/LottieFiles/dotlottie-web/main/guidelines/install.sh | bash
  10. Run local examples

    main

    The repository contains several runnable example applications in the examples/ directory.

    To run them locally:

    1. Install dependencies and build the project.
    2. Navigate to the desired example folder.
    3. Start the development server.

    Example highlights:

    • web: State machines, themes, segments, layout.
    • web-node: Server-side rendering to a buffer.
    • react: Renderer selection (canvas/webgl/webgpu).
    • vue: Theme toggling, ref/event patterns.
    • solid: Signal-based state, theme/animation switching.
    • wc: Web Component usage and event delegation.
    • next: SSR-safe rendering with the App Router.
    pnpm install && pnpm run build
    cd examples/web
    pnpm run dev