5.2.3 • Published 5 months ago

@aarsteinmedia/dotlottie-player v5.2.3

Weekly downloads
-
License
GPL-2.0-or-later
Repository
github
Last release
5 months ago

AM LottiePlayer

Awesome Vector Animations

We proudly claim this to be the most versatile, lightweight and efficient Lottie Player Web Component available. It's compatible with server side rendering, and completely framework agnostic.

PS: If you only need to render animations as SVG, and don't need to convert or combine animations on the fly, we've made light version: @aarsteinmedia/dotlottie-player-light.

Demo

Here is a demo, running on Next.js 15 using TypeScript.

Installation

In HTML

  • Import from CDN:
<script src="https://unpkg.com/@aarsteinmedia/dotlottie-player@latest/dist/index.js"></script>
  • Import from node_modules directory:
<script src="/node_modules/@aarsteinmedia/dotlottie-player/dist/index.js"></script>

In JavaScript or TypeScript

  1. Install using npm or yarn:
npm install --save @aarsteinmedia/dotlottie-player
  1. Import in your app:
import '@aarsteinmedia/dotlottie-player'

Usage

Add the element dotlottie-player to your markup and point src to a Lottie animation of your choice.

<dotlottie-player
  id="find-me"
  autoplay
  controls
  subframe
  loop
  src="https://storage.googleapis.com/aarsteinmedia/am.lottie"
  style="width: 320px; margin: auto;"
>
</dotlottie-player>

Load animation

To set animations programmatically, use the load() method.

const lottiePlayer = document.querySelector('#find-me')
player?.load('https://storage.googleapis.com/aarsteinmedia/am.lottie')

Convert to dotLottie

If you have a Lottie JSON animation and want to convert it to a dotLottie – to leverage compression, combine multiple animations in one file and keep your file library tidy with a discrete file extension – you can do so with the convert() method. This will trigger a download in the browser. If you have controls set to visible there's a convert button in the context menu on the right hand side.

Convert to JSON

If you're debugging a dotLottie animation – for instance if expressions aren't working as expected, you can convert it to JSON, either by usin the convert() method, or — if controls are set to visible – a button in the context menu on the right hand side.

Combine animations

If you want to combine multiple animations in one single dotLottie file you can use the addAnimation method. This will trigger a download in the browser. The source files can be either dotLottie or JSON, and the output file will will always be a dotLottie.

const lottiePlayer = document.querySelector('#find-me')
(async () => {
  await lottiePlayer?.addAnimation([
    { id: 'animation_1', url: '/url/to/animation_1.lottie' },
    { id: 'animation_2', url: '/url/to/animation_2.json', direction: -1, speed: 2 }
  ])
}()) 

You can also use this method independent of any Lottie player on the page, as long as the script is loaded, of course.

(async () => {
  await dotLottiePlayer().addAnimation([
    { id: 'animation_1', url: '/path/to/animation_1.lottie' },
    { id: 'animation_2', url: '/path/to/animation_2.json', direction: -1, speed: 2 }
  ])
}())

The new file wil automatically load the first animation when initialized. You can toggle between animations with the next() and prev() methods, or you can use the navigation buttons in the controls.

Control the playback of multiple animations in a single file. In the example below the first animation will play once, and then the next animation will loop:

<dotlottie-player
  id="find-me"
  subframe
  src="/path/to/combined-animations.lottie"
>
</dotlottie-player>  
  const player = document.querySelector('#find-me')
  player?.setMultiAnimationSettings(
    [
      {
        autplay: true
      },
      {
        autoplay: true,
        loop: true
      }
    ]
  )

Angular

  1. Import the component in app.component.ts.
import { Component } from '@angular/core'
import '@aarsteinmedia/dotlottie-player'

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'your-app-name';
}
  1. Add the player to your html template.

React.js / Next.js

If you've already imported the library in a parent component, you don't need to import it again in children of that component. If you want to assign the element a CSS class note that you need to use the class namespace, and not className.

import '@aarsteinmedia/dotlottie-player'

function App() {
  return (
    <dotlottie-player
      class="your-class-name"
      src="https://storage.googleapis.com/aarsteinmedia/am.lottie"
      autoplay
      controls
      loop
      style={{
        width: '320px',
        margin: 'auto'
      }}
    />
  )
}

export default App

If you're using TypeScript and want to assign the component a ref, you can do it like this:

import { useRef } from 'react'
import '@aarsteinmedia/dotlottie-player'
import type DotLottiePlayer from '@aarsteinmedia/dotlottie-player'

function App() {
  const animation = useRef<DotLottiePlayer | null>(null)
  return (
    <dotlottie-player
      ref={animation}
      subframe
      src="https://storage.googleapis.com/aarsteinmedia/am.lottie"
    />
  )
}

export default App

Vue.js / Nuxt.js (using Vite.js)

Compared to React and Angular there's a couple of extra steps, but surely nothing too daunting.

  1. Declare the dotlottie-player tag as a custom element, to prevent Vue from attempting to resolve it.

In Vue.js

vite.config.ts:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
  plugins: [
    vue({
      template: {
        compilerOptions: {
          isCustomElement: (tag: string) => ['dotlottie-player'].includes(tag),
        }
      }
    })
  ]
})

In Nuxt.js

nuxt.config.ts:

export default defineNuxtConfig({
  vue: {
    compilerOptions: {
      isCustomElement: (tag: string) => ['dotlottie-player'].includes(tag),
    }
  }
})
  1. Import/initiate the component.

In Vue.js

main.ts:

import { createApp } from 'vue'
import DotLottiePlayer from '@aarsteinmedia/dotlottie-player'
import App from './App.vue'

const app = createApp(App)
app.component('DotLottiePlayer', DotLottiePlayer)

In Nuxt.js

Create a plugins folder in your root if it doesn't exist already, add a file named dotlottie-player.js:

import DotLottiePlayer from '@aarsteinmedia/dotlottie-player'

export default defineNuxtPlugin(({ vueApp }) => {
  vueApp.component('DotLottiePlayer', DotLottiePlayer)
})
  1. The component can now be used in your pages or components template tags.
<template>
  <dotlottie-player
    src="https://storage.googleapis.com/aarsteinmedia/am.lottie"
    autoplay
    controls
    subframe
    loop
    style="width: 320px; margin: auto;"
  />
</template>

Properties

Property / AttributeDescriptionTypeDefault
autoplayPlay animation on loadbooleanfalse
backgroundBackground colorstringundefined
controlsShow controlsbooleanfalse
countNumber of times to loop animationnumberundefined
directionDirection of animation1 | -11
hoverWhether to play on mouse hoverbooleanfalse
loopWhether to loop animationbooleanfalse
modePlay modenormal | bouncenormal
objectfitResizing of animation in containercontain | cover | fill | nonecontain
rendererRenderer to usesvg | canvas | htmlsvg
speedAnimation speednumber1
src (required)URL to LottieJSON or dotLottiestringundefined
subframeWhen enabled this can help to reduce flicker on some animations, especially on Safari and iOS devices.booleanfalse

Methods

MethodFunction
addAnimation(config: Config[]) => voidAdd animation. Triggers download of new dotLottie file.
convert() => voidIf the current animation is in JSON format – convert it to dotLottie. Triggers a download in the browser.
destroy() => voidNullify animation and remove element from the DOM.
getLottie() => AnimationItem \| nullReturns the lottie-web instance used in the component
load(src: string) => voidLoad animation by URL or JSON object
next() => voidNext animation (if several in file)
pause() => voidPause
prev() => voidPrevious animation (if several in file)
play() => voidPlay
reload() => voidReload
seek(value: number \| string) => voidGo to frame. Can be a number or a percentage string (e. g. 50%).
setCount(value: number) => voidDynamically set number of loops
setDirection(value: 1 \| -1) => voidSet Direction
setLooping(value: boolean) => voidSet Looping
setMultiAnimationSettings(value: AnimationSettings[]) => voidSet Multi-animation settings
setSegment(value: AnimationSegment) => voidPlay only part of an animation. E. g. from frame 10 to frame 60 would be [10, 60]
setSpeed(value?: number) => voidSet Speed
setSubframe(value: boolean) => voidSet subframe
snapshot() => stringSnapshot the current frame as SVG. Triggers a download in the browser.
stop() => voidStop
toggleBoomerang() => voidToggle between bounce and normal
toggleLooping() => voidToggle looping
togglePlay() => voidToggle play

Events

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

NameDescription
completeAnimation is complete – including all loops
destroyedAnimation is destroyed
errorThe source cannot be parsed, fails to load or has format errors
frameA new frame is entered
freezeAnimation is paused due to player being out of view
loadAnimation is loaded
loopA loop is completed
playAnimation has started playing
pauseAnimation has paused
readyAnimation is loaded and player is ready
stopAnimation has stopped

WordPress Plugins

We've made a free WordPress plugin that works with Gutenberg Blocks, Elementor, Divi Builder and Flatsome UX Builder: AM LottiePlayer. It has all the functionality of this package, with a helpful user interface.

It's super lightweight – and only loads on pages where animations are used.

We've also made a premium WordPress plugin for purchase: AM LottiePlayer PRO. It has an easy-to-use GUI for combining and controlling multiple Lottie animations in a single file, converting JSON to dotLottie with drag-and-drop, and many more exclusive features.

License

GPL-2.0-or-later

4.0.5

12 months ago

4.0.7

12 months ago

4.0.6

12 months ago

5.0.4

5 months ago

5.0.3

5 months ago

5.0.2

5 months ago

5.0.1

5 months ago

5.0.0

8 months ago

4.0.9

12 months ago

4.0.8

12 months ago

5.1.9

5 months ago

5.1.8

5 months ago

5.1.7

5 months ago

5.1.6

5 months ago

5.1.5

5 months ago

5.1.4

5 months ago

5.1.3

5 months ago

5.1.2

5 months ago

5.1.1

5 months ago

5.1.0

5 months ago

4.0.10

11 months ago

4.0.12

9 months ago

4.0.11

11 months ago

4.0.14

8 months ago

4.0.13

9 months ago

5.2.3

5 months ago

5.2.2

5 months ago

5.2.1

5 months ago

5.2.0

5 months ago

5.1.13

5 months ago

5.1.12

5 months ago

5.1.11

5 months ago

5.1.10

5 months ago

4.0.4

12 months ago

4.0.1

12 months ago

4.0.0

1 year ago

4.0.3

12 months ago

4.0.2

12 months ago

3.2.5

1 year ago

3.2.4

1 year ago

3.2.2

1 year ago

3.2.3

1 year ago

3.2.1

1 year ago

3.2.0

1 year ago

3.0.8

1 year ago

3.0.7

1 year ago

3.0.9

1 year ago

3.1.0

1 year ago

3.0.4

1 year ago

3.0.3

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

3.0.6

1 year ago

3.0.5

1 year ago

3.0.0

1 year ago

2.5.14

1 year ago

2.5.15

1 year ago

2.5.10

1 year ago

2.5.11

1 year ago

2.5.12

1 year ago

2.5.13

1 year ago

3.0.0-canary.5

1 year ago

3.0.0-canary.4

1 year ago

3.0.0-canary.3

1 year ago

3.0.0-canary.2

1 year ago

3.0.0-canary.1

1 year ago

2.5.9

1 year ago

2.5.8

1 year ago

2.5.7

1 year ago

2.5.6

2 years ago

2.5.5

2 years ago

2.5.4

2 years ago

2.5.0

2 years ago

2.5.2

2 years ago

2.5.1

2 years ago

2.5.3

2 years ago

2.4.4

2 years ago

2.4.3

2 years ago

2.4.1

2 years ago

2.4.2

2 years ago

2.4.0

2 years ago

2.3.6

2 years ago

2.3.5

2 years ago

2.3.2

2 years ago

2.3.1

2 years ago

2.3.4

2 years ago

2.3.3

2 years ago

2.3.0

2 years ago

2.2.11

2 years ago

2.2.10

2 years ago

2.2.9

2 years ago

2.2.8

2 years ago

2.2.1

2 years ago

2.2.0

2 years ago

2.2.3

2 years ago

2.2.2

2 years ago

2.2.5

2 years ago

2.2.4

2 years ago

2.2.7

2 years ago

2.1.8

2 years ago

2.2.6

2 years ago

2.1.7

2 years ago

2.1.6

2 years ago

2.1.5

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.16

2 years ago

2.0.15

2 years ago

2.0.14

2 years ago

2.0.13

2 years ago

2.0.12

2 years ago

2.0.11

2 years ago

2.0.10

2 years ago

2.0.9

2 years ago

2.0.8

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago