0.14.1 • Published 6 months ago

@lottiefiles/dotlottie-react v0.14.1

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

@lottiefiles/dotlottie-react

npm npm bundle size npm NPM

Contents

Introduction

A React library for rendering lottie and dotLottie animations in the browser.

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-react

Usage

import React from 'react';
import { DotLottieReact } from '@lottiefiles/dotlottie-react';

const App = () => {
  return (
    <DotLottieReact
      src="path/to/animation.lottie"
      loop
      autoplay
    />
  );
};

Live Examples

  • Getting Started
  • Custom Playback Controls

APIs

DotLottieReactProps

The DotLottieReactProps extends the HTMLCanvasElement Props and accepts all the props that the HTMLCanvasElement accepts. In addition to that, it also accepts the following 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][0, totalFrames - 1]Animation segment. Accepts an array of two numbers, where the first number is the start frame and the second number is the end frame.
renderConfigRenderConfig{}Configuration for rendering the animation.
playOnHoverbooleanfalseDetermines if the animation should play on mouse hover and pause on mouse out.
dotLottieRefCallbackReact.RefCallback\<DotLottie | null>undefinedCallback function that receives a reference to the dotLottie web player instance.
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.
markerstringundefinedThe Lottie named marker to play.
animationIdstringundefinedThe ID of the animation to play.

RenderConfig

The renderConfig object accepts the following properties:

Property nameTypeRequiredDefaultDescription
devicePixelRationumberwindow.devicePixelRatio | 1The device pixel ratio.
renderConfig.autoResizebooleantrueDetermines if the canvas should resize automatically to its container

Custom Playback Controls

DotLottieReact 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:

import React from 'react';
import { DotLottieReact } from '@lottiefiles/dotlottie-react';

const App = () => {
  const dotLottieRef = React.useRef(null);

  return (
    <div>
      <DotLottieReact
        src="path/to/animation.lottie"
        loop
        autoplay
        dotLottieRefCallback={(dotLottie) => {
          dotLottieRef.current = dotLottie;
        }}
      />
      <div style={{ display: 'flex', gap: '8px', marginTop: '16px' }}>
        <button onClick={() => dotLottieRef.current?.play()}>Play</button>
        <button onClick={() => dotLottieRef.current?.pause()}>Pause</button>
        <button onClick={() => dotLottieRef.current?.stop()}>Stop</button>
        <button onClick={() => dotLottieRef.current?.setFrame(30)}>Seek to frame 30</button>
      </div>
    </div>
  );
};

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

Listening to Events

DotLottieReact 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:

import React from 'react';
import { DotLottieReact } from '@lottiefiles/dotlottie-react';

const App = () => {

  const onPlay = () => {
    console.log('Animation start playing');
  }

  const onPause = () => {
    console.log('Animation paused');
  }

  const onComplete = () => {
    console.log('Animation completed');
  }

  const onFrameChange = ({currentFrame}) => {
    console.log('Current frame: ', currentFrame);
  }

  return (
    <DotLottieReact
      src="path/to/animation.lottie"
      loop
      autoplay
      dotLottieRefCallback={(dotLottie) => {
        dotLottie.addEventListener('play', onPlay);
        dotLottie.addEventListener('pause', onPause);
        dotLottie.addEventListener('complete', onComplete);
        dotLottie.addEventListener('frame', onFrameChange);
      }}
    />
  );
};

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.13.0

10 months ago

0.13.1

9 months ago

0.13.2

9 months ago

0.13.3

9 months ago

0.13.4

8 months ago

0.13.5

8 months ago

0.10.1

1 year ago

0.14.0

7 months ago

0.14.1

6 months ago

0.10.0

1 year ago

0.12.2-pre.0

11 months ago

0.13.1-pre.2

10 months ago

0.13.1-pre.1

10 months ago

0.13.1-pre.0

10 months ago

0.13.0-beta.9

10 months ago

0.11.0

1 year ago

0.11.1

1 year ago

0.13.0-beta.7

10 months ago

0.13.0-beta.8

10 months ago

0.13.0-beta.5

11 months ago

0.13.0-beta.6

10 months ago

0.13.0-beta.3

11 months ago

0.13.0-beta.4

11 months ago

0.13.0-dev.0

10 months ago

0.14.0-pre.3

10 months ago

0.9.3-beta.9

1 year ago

0.12.1-beta.1

11 months ago

0.9.3-beta.7

1 year ago

0.12.1-beta.2

11 months ago

0.9.3-beta.8

1 year ago

0.9.3-beta.5

1 year ago

0.9.3-beta.6

1 year ago

0.9.3-beta.4

1 year ago

0.10.1-alpha.0

1 year ago

0.13.0-beta.12

9 months ago

0.13.0-beta.11

10 months ago

0.13.0-beta.10

10 months ago

0.12.0

12 months ago

0.12.1

12 months ago

0.12.2

11 months ago

0.12.3

11 months ago

0.13.4-pre.0

8 months ago

0.9.3-beta.3

1 year ago

0.9.3

1 year ago

0.9.3-beta.0

1 year ago

0.9.3-beta.1

1 year ago

0.9.3-beta.2

1 year ago

0.9.2

1 year ago

0.9.1

1 year ago

0.9.0

1 year ago

0.8.12

1 year ago

0.8.11

1 year ago

0.8.9

1 year ago

0.8.8

1 year ago

0.8.5

1 year ago

0.8.4

1 year ago

0.8.7

1 year ago

0.8.6

1 year ago

0.7.2

1 year ago

0.7.1

1 year ago

0.7.0

1 year ago

0.5.9

2 years ago

0.8.10

1 year ago

0.8.1

1 year ago

0.6.3

2 years ago

0.8.0

1 year ago

0.6.2

2 years ago

0.8.3

1 year ago

0.6.5

1 year ago

0.8.2

1 year ago

0.6.4

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.8

2 years ago

0.5.7

2 years ago

0.5.6

2 years ago

0.5.5

2 years ago

0.5.4

2 years ago

0.5.3

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.5.0

2 years ago

0.4.0

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.6

2 years ago

0.2.5-beta.4

2 years ago

0.2.5-beta.3

2 years ago

0.2.5-beta.2

2 years ago

0.2.5

2 years ago

0.2.5-beta.1

2 years ago

0.2.4-beta.1

2 years ago

0.2.5-beta.0

2 years ago

0.2.4

2 years ago

0.2.4-beta.0

2 years ago

0.2.3

2 years ago

0.2.1

2 years ago

0.2.2

2 years ago

0.2.0

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.0

2 years ago