Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@lottiefiles/lottie-player

Package Overview
Dependencies
Maintainers
3
Versions
62
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lottiefiles/lottie-player

Lottie animation and Telegram Sticker player web components.

  • 1.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
111K
increased by13.37%
Maintainers
3
Weekly downloads
 
Created
Source

lottie-player Web Component

This is a Web Component for easily embedding and playing Lottie animations and the Lottie-based Telegram Sticker (tgs) animations in websites.

npm webcomponents.org

Demo

screencast

Documentation

Installation

In HTML, import from CDN or from the local Installation:
Lottie Player:
  • Import from CDN.
<script src="https://unpkg.com/@lottiefiles/lottie-player@0.4.0/dist/lottie-player.js"></script>
  • Import from local node_modules directory.
<script src="/node_modules/@lottiefiles/lottie-player/dist/lottie-player.js"></script>
Telegram Sticker (TGS) Player:
  • Import from CDN.
<script src="https://unpkg.com/@lottiefiles/lottie-player@0.4.0/dist/tgs-player.js"></script>
  • Import from local node_modules directory.
<script src="/node_modules/@lottiefiles/lottie-player/dist/tgs-player.js"></script>
In Javascript or TypeScript:
  1. Install package using npm or yarn.
npm install --save @lottiefiles/lottie-player
  1. Import package in your code.
import "@lottiefiles/lottie-player";

Usage

Lottie-Player

Add the element lottie-player and set the src property to a URL pointing to a valid Bodymovin JSON.

<lottie-player
  autoplay
  controls
  loop
  mode="normal"
  src="https://assets3.lottiefiles.com/packages/lf20_UJNc2t.json"
  style="width: 320px"
>
</lottie-player>

You may set and load animations programatically as well.

<lottie-player autoplay controls loop mode="normal" style="width: 320px">
</lottie-player>
const player = document.querySelector("lottie-player");
player.load("https://assets3.lottiefiles.com/packages/lf20_UJNc2t.json");

// or load via a Bodymovin JSON string/object
player.load(
  '{"v":"5.3.4","fr":30,"ip":0,"op":38,"w":315,"h":600,"nm":"new", ... }'
);

TGS-Player

Add the element tgs-player and set the src property to a URL pointing to a valid TGS JSON.

<tgs-player autoplay loop mode="normal" src="https//domain/example.tgs">
</tgs-player>

ReactJS & VueJS

Import the player either as

import * as LottiePlayer from "@lottiefiles/lottie-player";

or

require("@lottiefiles/lottie-player");

Use as follows

<lottie-player
  autoplay
  controls
  loop
  mode="normal"
  src="https://assets3.lottiefiles.com/packages/lf20_UJNc2t.json"
  style="width: 320px"
></lottie-player>

Typescript ReactJS

Import the player either as

import * as LottiePlayer from "@lottiefiles/lottie-player";

or

require("@lottiefiles/lottie-player");

Use as follows

<lottie-player
  autoplay
  controls
  loop
  mode="normal"
  src="https://assets3.lottiefiles.com/packages/lf20_UJNc2t.json"
  style="width: 320px"
></lottie-player>

For typescript projects an added step is required. The component must be declared as a JSX intrinsic element. Create a file 'declarations.d.ts' in the root of your project and add the code below to the file.

declare namespace JSX {
  interface IntrinsicElements {
    "lottie-player": any;
  }
}

NuxtJS

The process for NuxtJS is slightly different. Create a lottie-player.js file in project root inside a folder named 'plugins'. Add the code below to the file

import * as LottiePlayer from "@lottiefiles/lottie-player";

Open nuxt.config.js file and adjust the plugins array as shown below

plugins: [{ src: "~/plugins/lottie-player.js", mode: "client" }],

You would then be able to use the player as follows inside any component

<lottie-player
  autoplay
  controls
  loop
  style="width:400px"
  src="https://assets3.lottiefiles.com/packages/lf20_RItkEz.json"
  speed="1"
  debug
></lottie-player>

This is because the player script needs to be rendered on the browser/client side and we must configure nuxtjs to load the script on the client side only.

NextJS

The process to import in NextJS is similar to NuxtJS in the sense that on SSR mode, the library must be declared as a client side module. To do this, import the library within a react useEffect hook.

import React, { useRef } from "react";

export default function Home() {
  const ref = useRef(null);
  React.useEffect(() => {
    import("@lottiefiles/lottie-player");
  });
  return (
    <div className={styles.container}>
      <main className={styles.main}>
        <lottie-player
          id="firstLottie"
          ref={ref}
          autoplay
          controls
          loop
          mode="normal"
          src="https://assets4.lottiefiles.com/packages/lf20_gb5bmwlm.json"
          style={{ width: "300px", height: "300px" }}
        ></lottie-player>
      </main>
    </div>
  );
}

Do add a declaration file named declaration.d.ts to the root of the project as well

declare namespace JSX {
  interface IntrinsicElements {
    "lottie-player": any;
  }
}

Properties

| Property | Attribute | Description | Type | Default | | --------------------- | --------------------- | ----------------------------------- | ------------------------------------ | ----------------- | ------- | | autoplay | autoplay | Autoplay animation on load. | boolean | false | | background | background | Background color. | string | undefined | | controls | controls | Show controls. | boolean | false | | count | count | Number of times to loop animation. | number | undefined | | direction | direction | Direction of animation. | number | 1 | | hover | hover | Whether to play on mouse hover. | boolean | false | | loop | loop | Whether to loop animation. | boolean | false | | mode | mode | Play mode. | PlayMode.Bounce \| PlayMode.Normal | PlayMode.Normal | | preserveAspectRatio | preserveAspectRatio | Valid preserve aspect ratio value. | string | 'xMidYMid meet' | | renderer | renderer | Renderer to use. | "svg" | "canvas" | 'svg' | | speed | speed | Animation speed. | number | 1 | | src (required) | src | Bodymovin JSON data or URL to JSON. | string | undefined |

Methods

getLottie() => Promise<any>

Returns the instance of lottie player used in the component.

Returns

Type: Promise<any>

load(src: string | object) => void

Load (and play) a given Bodymovin animation.

Parameters
NameTypeDescription
srcstring or objectURL, or a JSON string or object representing a Bodymovin JSON.
Returns

Type: void

pause() => void

Pause animation play.

Returns

Type: void

play() => void

Start playing animation.

Returns

Type: void

setDirection(value: number) => void

Animation play direction.

Parameters
NameTypeDescription
valuenumberDirection values.
Returns

Type: void

setLooping(value: boolean) => void

Sets the looping of the animation.

Parameters
NameTypeDescription
valuebooleanWhether to enable looping. Boolean true enables looping.
Returns

Type: void

setSpeed(value?: number) => void

Sets animation play speed.

Parameters
NameTypeDescription
valuenumberPlayback speed.
Returns

Type: void

stop() => void

Stops animation play.

Returns

Type: void

seek(value: number | string) => void

Seek to a given frame. Frame value can be a number or a percent string (e.g. 50%).

Returns

Type: void

snapshot(download?: boolean) => string

Snapshot the current frame as SVG. If 'download' argument is boolean true, then a download is triggered in browser.

Returns

Type: string

toggleLooping() => void

Toggles animation looping.

Returns

Type: void

togglePlay() => void

Toggle playing state.

Returns

Type: void

resize() => void

Resize animation stage and elements in response to changes in component.

Returns

Type: void

Events

The following events are exposed and can be listened to via addEventListener calls.

NameDescription
loadAnimation data is loaded.
errorAn animation source cannot be parsed, fails to load or has format errors.
readyAnimation data is loaded and player is ready.
playAnimation starts playing.
pauseAnimation is paused.
stopAnimation is stopped.
freezeAnimation is paused due to player being invisible.
loopAn animation loop is completed.
completeAnimation is complete (all loops completed).
frameA new frame is entered.

Styling

Custom propertyDescriptionDefault
--lottie-player-toolbar-heightToolbar height35px
--lottie-player-toolbar-background-colorToolbar background colortransparent
--lottie-player-toolbar-icon-colorToolbar icon color#999
--lottie-player-toolbar-icon-hover-colorToolbar icon hover color#222
--lottie-player-toolbar-icon-active-colorToolbar icon active color#555
--lottie-player-seeker-track-colorSeeker track color#CCC
--lottie-player-seeker-thumb-colorSeeker thumb colorrgba(0, 107, 120, 0.8)

License

MIT License © LottieFiles.com

Keywords

FAQs

Package last updated on 08 Apr 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc