Package detail

@lottiefiles/lottie-interactivity

LottieFiles39.3kMIT1.6.2

This is a small effects and interactivity library written to be paired with the Lottie Web Player

readme

Lottie Interactivity

npm (scoped) NPM npm bundle size (scoped)

This is a small library to add scrolling and cursor interactivity to your Lottie Animations. This can be used with the Lottie Web-Player Component or the Lottie Player.

Documentation

For full documentation, visit docs.lottiefiles.com/lottie-interactivity

Installation

via yarn

yarn add @lottiefiles/lottie-interactivity

via npm

npm install --save @lottiefiles/lottie-interactivity

via cdn

<script src="https://unpkg.com/@lottiefiles/lottie-interactivity@latest/dist/lottie-interactivity.min.js"></script>

Demo

To see examples of all these features, please visit the LottieFiles interactivity page.


Getting started

1. Import lottie player script to DOM

<script src="https://unpkg.com/@lottiefiles/lottie-player@1/dist/lottie-player.js"></script> // place this in your body element

2. Add a Lottie to html dom with an ID set to the div

<lottie-player
  id="firstLottie"
  src="https://assets2.lottiefiles.com/packages/lf20_i9mxcD.json"
  style="width:400px; height: 400px;"
>
</lottie-player>

3. Setup configuration

The name of the player ie: 'firstLottie' in this example is the ID set to the lottie web component on the html page. Configration will contain an actions object. This object takes an array named actions which consists of an array of objects. Multiple objects can be added into this array and therefore multiple actions such as "seek","play", "stop" and "loop", can be set.

Each object has a start and end which is essentially a percentage for the height of the lottie container and is a value between 0 and 1. The visibility arrays first value is the start and the second value is the end. This refers to the percentage of the viewport.

Ensure that the ending frame is the frame you want the interactivity to end at. This could be the last frame or a frame of your choosing. In this case it is set to 100.

Configuration modes include "scroll" where the animation will be synced to the scrolling of the window, "cursor" where the scrolling of the animation will be synced to the cursor position within the container. And "chain" allowing you to interact with multiple Lottie animations one after the other.

The configuration can include a container field as shown in the next example. If a container is not provided the parent div will be taken as a container.

ensure that the interactivity library is imported to the body of your html DOM

LottieInteractivity.create({
  mode: 'scroll',
  player: '#firstLottie',
  actions: [
    {
      visibility: [0, 1],
      type: 'seek',
      frames: [0, 100],
    },
  ],
});
3.1 React config

The configuration for the library remains the same for react apps. However usage and initialization is as follows. Import the create function from the lottie interactivity library and call the create function. With frameworks like react it is ideal to add an event listener that waits for the lottie player to load before calling the interactivity library. An example is as follows for a very basic react class component.

import React from 'react';
import '@lottiefiles/lottie-player';
import { create } from '@lottiefiles/lottie-interactivity';

class App extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef(); // 1. create a reference for the lottie player
  }
  componentDidMount() {
    // 3. listen for player load. see lottie player repo for other events
    this.myRef.current.addEventListener('load', function (e) {
      // 4. configure the interactivity library
      create({
        mode: 'scroll',
        player: '#firstLottie',
        actions: [
          {
            visibility: [0, 1],
            type: 'seek',
            frames: [0, 100],
          },
        ],
      });
    });
  }
  render() {
    return (
      <div className="App">
        <div style={{ height: '400px' }}></div>
        <lottie-player
          ref={this.myRef} // 2. set the reference for the player
          id="firstLottie"
          controls
          mode="normal"
          src="https://assets3.lottiefiles.com/packages/lf20_UJNc2t.json"
          style={{ width: '320px' }}
        ></lottie-player>
      </div>
    );
  }
}

export default App;
3.2 Vue config

The configuration for the library remains the same for vue apps. However usage and initialization is as follows. Import the create function from the lottie interactivity library and call the create function. With frameworks like vue it is ideal to add an event listener that waits for the lottie player to load before calling the interactivity library. An example is as follows for a very basic vue class component.

<template>
  <!--  1. Create a lottie player with a reference -->
  <lottie-player id="firstLottie"
                 ref="lottie"
                 controls
                 mode="normal"
                 src="https://assets3.lottiefiles.com/packages/lf20_UJNc2t.json"
                 style="width: 320px;">
  </lottie-player>
</template>

<script>
  import '@lottiefiles/lottie-player';
  import { create } from '@lottiefiles/lottie-interactivity';

  export default {
  name: 'App',

  mounted() {
    // 2. listen for player load. See lottie player repo for other events
    this.$refs.lottie.addEventListener('load', function() {
      // 3. configure the interactivity library
      create({
        mode: 'scroll',
        player: '#firstLottie',
        actions: [
          {
            visibility: [0, 1],
            type: 'seek',
            frames: [0, 100],
          },
        ],
      });
    })
  }
}
</script>

Lottie-Interactivity has a wide variety of interactions and modes possible, allowing you to easily add interactions to your Lottie animations. For the full documentation on what's possible and how to use this library click here.

To see examples of all these features, please visit the LottieFiles interactivity page.

Our other Lottie related libraries


Project Description
lottie-player A Web Component for easily embedding and playing Lottie animations and the Lottie-based Telegram Sticker (tgs) animations in websites.
lottie-react A React component for the Lottie Web player.
lottie-vue A Vue component for the Lottie player.
svelte-lottie-player Lottie player component for use with Svelte.
jLottie jLottie is suitable as a general purpose lottie player, though implements a subset of the features in the core player - this approach leads to a tiny footprint and great performance.
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".
lottie-js The library consists of methods to map the Lottie JSON to the object model and interact with properties as well as manipulate them.
lottie-theming A library to extract themable properties and apply different themes to a given Lottie

Community & Support

  • Github issues. For bugs and errors you encounter using this player.
  • Discord. For hanging out with the community and sharing your awesome Lottie animations!

Contributing

We use changesets to maintain a changelog for this repository. When making any change to the codebase that impacts functionality or performance we require a changeset to be present.

To add a changeset run:

yarn run changeset

And select the type of version bump you'd like (major, minor, patch).

You can document the change in detail and format it properly using Markdown by opening the ".md" file that the "yarn changeset" command created in the ".changeset" folder. Open the file, it should look something like this:

---
"@lottiefiles/pkg1": minor
"@lottiefiles/pkg2": major
---
This is where you document your **changes** using Markdown.
- You can write
- However you'd like
- In as much detail as you'd like
Aim to provide enough details so that team mates and future you can understand the changes and the context of the change.

You can commit your changes and the changeset to your branch and then create a pull request on the develop branch.

License

MIT License © LottieFiles.com

changelog

Changelog

1.6.2

Patch Changes

  • added types

1.6.1

Patch Changes

  • fixed stop error, added playOnce to scroll

1.6.0

Minor Changes

  • 7b255ff: added pause and pauseHold state

1.5.2

Patch Changes

  • adding changesets and dual deployment to npm and gpr

1.5.1

Patch Changes

1.5.1 (2022-04-26)

Bug Fixes

  • fixed autoplay not being removed after onComplete transition (c2ba40d)
  • fixed forceflag not working on click (fc4f818)

1.5.0 (2022-04-12)

Features

  • added jumptToInteraction method (8def8b5)

1.4.0 (2022-04-12)

Features

  • made nextInteraction method public (642a64d)

1.3.8 (2022-03-16)

Bug Fixes

  • added fixes to click and hold transitions (43eb8c5)

1.3.7 (2022-03-07)

Bug Fixes

  • fixed container null issue on wordpress (fa0e5e7)

1.3.6 (2022-02-21)

Bug Fixes

  • fixed error when player is destroyed and redefineOptions is called (7d2c4e3)

1.3.5 (2022-02-17)

Bug Fixes

  • added usecapture to scroll listener (763cc37)
  • changed way window is used (9a6e7b9)

1.3.4 (2022-02-01)

Bug Fixes

  • scrolling: fixed segments not playing on scroll with 'play' action type (9d360c7)

1.3.3 (2022-01-25)

Bug Fixes

  • added init methods when domloaded event has already fires (a6f4c89)

1.3.2 (2022-01-10)

Bug Fixes

  • interactions: added additional comments (5a77a9c)
  • interactions: added mobile support for interactions (b8e3a9e)

1.3.1 (2022-01-05)

Bug Fixes

  • stop method: corrected remove event listener (7c148ad)
  • sync to cursor: added touchmove listener to sync animation for touch screens (88cf1b3)

1.3.0 (2021-12-08)

Features

  • cursor mode: added toggle interaction (e66b74c)

1.2.0 (2021-11-10)

Bug Fixes

  • click handler: fixed faulty call to click handler (69d5b67)

Features

  • additional chain properties: added speed and delay (b1f5db3)
  • animation loading: added animation loading for when the library is passed an animation object (e04600d)
  • global: first draft of interaction chaining (672b0e8)
  • interactions: added ability to load lotties in interaction chaining from 'path' attribute (db3af11)
  • interactions: added click and hover states, updated example page (853eff2)
  • interactions: Added click, hover and visible interactions (d97fbad)
  • interactions: added jumpTo property to get to a specific interaction index after transition (b3a82f4)
  • interactions: added sync to cursor to interaction chaining (67331c3)
  • interactions chaining: added dynamic loading of animations, finished examples page (60003f4)

1.1.0 (2021-09-28)

Bug Fixes

  • scroll: use start/end frames in seeking (71f2197)

Features

  • scroll: reset frame count from loop to seek (270b621)

1.0.0 (2021-08-10)

Bug Fixes

  • examples: fix a missing and misplaced comma in examples (a922b79)
  • scroll: with 'scroll' mode and type as 'play' plays animation once and pauses at end (e9ff742)

1.0.0 (2021-06-29)

Bug Fixes

  • scroll: with 'scroll' mode and type as 'play' plays animation once and pauses at end (e9ff742)