1.5.23 • Published 8 months ago

@johanaarstein/dotlottie-player v1.5.23

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

Lottie Player Web Component

Awesome Vector Animations

This started as a fork of @dotlottie/player-component, mainly made to address issues with render settings and aspect ratio. Since then we've added some functionalities here and tweaked some configurations there, and now this is the most versatile Lottie Player Web Component out there!

The component is built with Lit and compiled with Rust. It's compatible with server side rendering, and like any good web component it's framework agnostic.

If you only need to render animations as SVG we've made light version: @johanaarstein/dotlottie-player-light.

Demo

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

Installation

In HTML

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

In JavaScript or TypeScript

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

Usage

Add the element dotlottie-player to your markup and point src to a Lottie animation of your choice. If you're working in SSR environments like Next.js or Nuxt.js, it might be a good idea to set reflective booleans (like autoplay, controls and loop) to an empty string instead of true – to mimic how modern browsers treats these values in markup, as opposed to how Node.js treats them. This way you avoid hydration errors.

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

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

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

Angular

  1. Import the component in app.component.ts.
import { Component } from '@angular/core'
import '@johanaarstein/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 class note that you need to use the class namespace, and not className.

import '@johanaarstein/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 '@johanaarstein/dotlottie-player'
import type { DotLottiePlayer } from '@johanaarstein/dotlottie-player'

function App() {
  const animation = useRef<DotLottiePlayer | null>(null)
  return (
    <dotlottie-player
      ref={animation}
      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 '@johanaarstein/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 '@johanaarstein/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=""
    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
segmentPlay only part of an animation. E. g. from frame 10 to frame 60 would be [10, 60][number, number]undefined
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
load(src: string) => voidLoad
pause() => voidPause
play() => voidPlay
reload() => voidReload
seek(value: number \| string) => voidGo to frame. Can be a number or a percentage string (e. g. 50%).
setDirection(value: 1 \| -1) => voidSet Direction
setLooping(value: boolean) => voidSet Looping
setSpeed(value?: number) => voidSet Speed
setSubframe(value: boolean) => voidSet subframe
snapshot(download?: boolean) => stringSnapshot the current frame as SVG. If 'download' is set to true, a download is triggered 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 Plugin

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.

License

GPL-2.0-or-later

1.5.16

10 months ago

1.5.15

10 months ago

1.5.18

9 months ago

1.5.17

9 months ago

1.5.19

9 months ago

1.5.21

9 months ago

1.5.20

9 months ago

1.5.23

8 months ago

1.5.22

9 months ago

1.5.12

11 months ago

1.5.11

12 months ago

1.5.14

11 months ago

1.5.13

11 months ago

1.4.20

1 year ago

1.4.22

1 year ago

1.4.21

1 year ago

1.4.24

1 year ago

1.4.23

1 year ago

1.4.26

1 year ago

1.4.25

1 year ago

1.4.28

1 year ago

1.4.27

1 year ago

1.4.29

1 year ago

1.4.31

1 year ago

1.4.30

1 year ago

1.4.33

1 year ago

1.4.32

1 year ago

1.4.35

1 year ago

1.4.34

1 year ago

1.4.37

1 year ago

1.4.36

1 year ago

1.4.38

1 year ago

1.5.5

1 year ago

1.5.4

1 year ago

1.5.3

1 year ago

1.5.2

1 year ago

1.5.1

1 year ago

1.5.0

1 year ago

1.4.11

1 year ago

1.4.13

1 year ago

1.4.12

1 year ago

1.4.15

1 year ago

1.4.14

1 year ago

1.4.17

1 year ago

1.4.16

1 year ago

1.4.19

1 year ago

1.4.18

1 year ago

1.5.9

1 year ago

1.5.8

1 year ago

1.5.7

1 year ago

1.5.6

1 year ago

1.5.10

1 year ago

1.4.6

1 year ago

1.4.5

1 year ago

1.4.4

1 year ago

1.4.3

1 year ago

1.4.2

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.10

1 year ago

1.3.13

1 year ago

1.3.14

1 year ago

1.3.11

1 year ago

1.3.12

1 year ago

1.3.17

1 year ago

1.3.18

1 year ago

1.3.15

1 year ago

1.3.16

1 year ago

1.3.9

1 year ago

1.3.8

1 year ago

1.3.7

1 year ago

1.4.9

1 year ago

1.4.8

1 year ago

1.4.10

1 year ago

1.4.7

1 year ago

1.2.18

1 year ago

1.2.19

1 year ago

1.3.6

1 year ago

1.3.5

1 year ago

1.3.4

1 year ago

1.3.3

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.20

1 year ago

1.2.23

1 year ago

1.2.24

1 year ago

1.2.21

1 year ago

1.2.22

1 year ago

1.2.27

1 year ago

1.2.28

1 year ago

1.2.25

1 year ago

1.2.26

1 year ago

1.2.29

1 year ago

1.2.30

1 year ago

1.2.31

1 year ago

1.2.34

1 year ago

1.2.12

1 year ago

1.2.35

1 year ago

1.2.13

1 year ago

1.2.32

1 year ago

1.2.33

1 year ago

1.2.11

1 year ago

1.2.38

1 year ago

1.2.16

1 year ago

1.2.17

1 year ago

1.2.36

1 year ago

1.2.37

1 year ago

1.2.15

1 year ago

1.2.10

2 years ago

1.2.9

2 years ago

1.2.8

2 years ago