0.3.23 • Published 9 months ago

@lottiefiles/dotlottie-svelte v0.3.23

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@lottiefiles/dotlottie-svelte

npm npm bundle size npm NPM

Contents

Introduction

A Svelte component for rendering Lottie and dotLottie animations. It acts as a wrapper around the dotLottie web player.

What is dotLottie?

dotLottie is an open-source file format that aggregates one or more Lottie files and their associated resources into a single file. They are ZIP archives compressed with the Deflate compression method and carry the file extension of ".lottie".

Learn more about dotLottie.

Installation

npm install @lottiefiles/dotlottie-svelte

Usage

<script lang="ts">
import { DotLottieSvelte } from '@lottiefiles/dotlottie-svelte';
</script>

<DotLottieSvelte
  src="path/to/animation.lottie"
  loop
  autoplay
/>

Live Examples

APIs

Props

Property nameTypeRequiredDefaultDescription
autoplaybooleanfalseAuto-starts the animation on load.
loopbooleanfalseDetermines if the animation should loop.
srcstringundefinedURL to the animation data (.json or .lottie).
speednumber1Animation playback speed. 1 is regular speed.
datastring | ArrayBufferundefinedAnimation data provided either as a Lottie JSON string or as an ArrayBuffer for .lottie animations.
modestring"forward"Animation play mode. Accepts "forward", "reverse", "bounce", "reverse-bounce".
backgroundColorstringundefinedBackground color of the canvas. Accepts 6-digit or 8-digit hex color string (e.g., "#000000", "#000000FF"),
segment[number, number]undefinedAnimation segment. Accepts an array of two numbers, where the first number is the start frame and the second number is the end frame.
renderConfigRenderConfigundefinedConfiguration for rendering the animation.
dotLottieRefCallback(dotLottie: DotLottie) => voidundefinedCallback function that receives a reference to the dotLottie web player instance once instantiated.
useFrameInterpolationbooleantrueDetermines if the animation should update on subframes. If set to false, the original AE frame rate will be maintained. If set to true, it will refresh at each requestAnimationFrame, including intermediate values. The default setting is true.
autoResizeCanvasbooleantrueDetermines if the canvas should resize automatically to its container

RenderConfig

The renderConfig object accepts the following properties:

Property nameTypeRequiredDefaultDescription
devicePixelRationumberwindow.devicePixelRatio | 1The device pixel ratio.

Custom Playback Controls

DotLottieSvelte component makes it easy to build custom playback controls for the animation. It exposes a dotLottieRefCallback prop that can be used to get a reference to the dotLottie web player instance. This instance can be used to control the playback of the animation using the methods exposed by the dotLottie web player instance.

Here is an example:

<script lang="ts">
  import { DotLottieSvelte } from '@lottiefiles/dotlottie-svelte';
  import type { DotLottie } from '@lottiefiles/dotlottie-svelte';
  
  let dotLottie: DotLottie | null = null;

  function play() {
    dotLottie?.play();
  }

  function pause() {
    dotLottie?.pause();
  }

  function stop() {
    dotLottie?.stop();
  }
</script>

<DotLottieSvelte
  src="path/to/your/animation.lottie"
  loop={true}
  autoplay={true}
  dotLottieRefCallback={(ref) => dotLottie = ref}
/>

<button on:click={play}>Play</button>
<button on:click={pause}>Pause</button>
<button on:click={stop}>Stop</button>

You can find the list of methods that can be used to control the playback of the animation here.

Listening to Events

DotLottieSvelte component can receive a dotLottieRefCallback prop that can be used to get a reference to the dotLottie web player instance. This reference can be used to listen to player events emitted by the dotLottie web instance.

Here is an example:

<script lang="ts">
    import { DotLottieSvelte } from '@lottiefiles/dotlottie-svelte';
    import type { DotLottie } from '@lottiefiles/dotlottie-svelte';

    let dotLottie: DotLottie | null = null;

    function onLoaded() {
        console.log("Animation loaded");
    }

    function onPlay() {
        console.log("Animation started");
    }

    function onPause() {
        console.log("Animation paused");
    }

    function onComplete() {
        console.log("Animation completed");
    }
    
    function setupListeners(dotLottieInstance) {
        dotLottieInstance.addEventListener('load', onLoaded);
        dotLottieInstance.addEventListener('play', onPlay);
        dotLottieInstance.addEventListener('pause', onPause);
        dotLottieInstance.addEventListener('complete', onComplete);
    }

    function removeListeners(dotLottieInstance) {
        dotLottieInstance.removeEventListener('load', onLoaded);
        dotLottieInstance.removeEventListener('play', onPlay);
        dotLottieInstance.removeEventListener('pause', onPause);
        dotLottieInstance.removeEventListener('complete', onComplete);
    }

     onDestroy(() => {
        if (dotLottie) {
            removeListeners(dotLottie);
        }
    });
</script>

<DotLottieSvelte
    src="path/to/your/animation.lottie"
    loop={true}
    autoplay={true}
    dotLottieRefCallback={(ref) => {
        dotLottie = ref;
        setupListeners(dotLottie);
    }}
/>

dotLottie instance exposes multiple events that can be listened to. You can find the list of events here.

Development

Setup

pnpm install

Dev

pnpm dev

Build

pnpm build
0.3.23-beta.3

9 months ago

0.3.23

9 months ago

0.3.23-beta.1

9 months ago

0.3.23-beta.2

9 months ago

0.3.23-beta.0

9 months ago

0.3.22

9 months ago

0.3.21

9 months ago

0.3.20

10 months ago

0.3.19

10 months ago

0.3.18

11 months ago

0.3.9

12 months ago

0.3.17

11 months ago

0.3.16

11 months ago

0.3.15

11 months ago

0.3.14

12 months ago

0.3.13

12 months ago

0.3.12

12 months ago

0.3.11

12 months ago

0.3.10

12 months ago

0.3.0

1 year ago

0.2.0

1 year ago

0.3.6

1 year ago

0.3.5

1 year ago

0.3.8

12 months ago

0.3.7

1 year ago

0.1.9

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.3.4

1 year ago

0.3.3

1 year ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.1

1 year ago

0.0.0

1 year ago