Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
@lottiefiles/react-lottie-player
Advanced tools
@lottiefiles/react-lottie-player is a React component library that allows you to easily integrate Lottie animations into your React applications. It provides a simple and flexible way to control Lottie animations, including playing, pausing, stopping, and looping animations.
Basic Animation Playback
This feature allows you to play a Lottie animation automatically and loop it. The `autoplay` and `loop` props control the playback behavior, and the `src` prop specifies the URL of the Lottie animation file.
import { Player } from '@lottiefiles/react-lottie-player';
function App() {
return (
<Player
autoplay
loop
src="https://assets7.lottiefiles.com/packages/lf20_5x8LBM.json"
style={{ height: '300px', width: '300px' }}
/>
);
}
Controlling Animation Playback
This feature allows you to control the playback of a Lottie animation using buttons. The `ref` prop is used to get a reference to the Player component, which is then used to call the `play`, `pause`, and `stop` methods.
import { Player } from '@lottiefiles/react-lottie-player';
import { useRef } from 'react';
function App() {
const playerRef = useRef(null);
return (
<div>
<Player
ref={playerRef}
src="https://assets7.lottiefiles.com/packages/lf20_5x8LBM.json"
style={{ height: '300px', width: '300px' }}
/>
<button onClick={() => playerRef.current.play()}>Play</button>
<button onClick={() => playerRef.current.pause()}>Pause</button>
<button onClick={() => playerRef.current.stop()}>Stop</button>
</div>
);
}
Customizing Animation Speed
This feature allows you to customize the playback speed of a Lottie animation. The `setPlayerSpeed` method is used to set the speed of the animation, with 1 being the normal speed.
import { Player } from '@lottiefiles/react-lottie-player';
import { useRef } from 'react';
function App() {
const playerRef = useRef(null);
return (
<div>
<Player
ref={playerRef}
src="https://assets7.lottiefiles.com/packages/lf20_5x8LBM.json"
style={{ height: '300px', width: '300px' }}
/>
<button onClick={() => playerRef.current.setPlayerSpeed(0.5)}>0.5x Speed</button>
<button onClick={() => playerRef.current.setPlayerSpeed(1)}>1x Speed</button>
<button onClick={() => playerRef.current.setPlayerSpeed(2)}>2x Speed</button>
</div>
);
}
react-lottie is another React component library for rendering Lottie animations. It provides similar functionality to @lottiefiles/react-lottie-player, including autoplay, loop, and control methods. However, it may not have as many built-in controls and customization options as @lottiefiles/react-lottie-player.
lottie-react is a lightweight library for integrating Lottie animations into React applications. It offers basic playback controls and customization options. Compared to @lottiefiles/react-lottie-player, it is more minimalistic and may lack some advanced features.
react-lottie-player is a simple and easy-to-use library for adding Lottie animations to React projects. It provides basic playback controls and customization options. While it is similar to @lottiefiles/react-lottie-player, it may not offer as many advanced features and controls.
This is a React component for the Lottie Web Player
npm install --save @lottiefiles/react-lottie-player
import { Player, Controls } from '@lottiefiles/react-lottie-player';
Clone repo
run yarn install
run yarn storybook
yarn storybook
Add the element Player
and set the src
prop to a URL pointing to a valid Lottie JSON.
<Player
autoplay
loop
src="https://assets3.lottiefiles.com/packages/lf20_UJNc2t.json"
style={{ height: '300px', width: '300px' }}
>
<Controls visible={true} buttons={['play', 'repeat', 'frame', 'debug']} />
</Player>
Prop | Description | Type | Default |
---|---|---|---|
lottieRef | Get lottie animation object | function | undefined |
onEvent | Listen for events | function | undefined |
onStateChange | Play state changes | function | undefined |
onBackgroundChange | Listen for bg changes | function | undefined |
autoplay | Autoplay animation on load. | boolean | false |
background | Background color. | string | undefined |
controls | Show controls. | boolean | false |
direction | Direction of animation. | number | 1 |
hover | Whether to play on mouse hover. | boolean | false |
keepLastFrame | Stop animation on the last frame. Has no effect if loop is true. | boolean | false |
loop | Whether to loop animation. | boolean | false |
renderer | Renderer to use. | `"svg" | "canvas"` |
speed | Animation speed. | number | 1 |
style | The style for the container. | object | undefined |
src (required) | Bodymovin JSON data or URL to JSON. | object | string |
To call methods on the instance of the Player component. you may get a reference to the component and call the methods on ref.current. This is esentially reacts way of doing a document.getElementById(); You may then use this ref ie: player in the example below to call methods that are described in this documentation. See ref in react documentation
import React from 'react';
import { Player } from '@lottiefiles/react-lottie-player';
class App extends React.Component {
constructor(props) {
super(props);
this.player = React.createRef(); // initialize your ref
}
render() {
return (
<Player
ref={this.player} // set the ref to your class instance
autoplay={false}
loop={true}
controls={true}
src="https://assets3.lottiefiles.com/packages/lf20_XZ3pkn.json"
style={{ height: '300px', width: '300px' }}
></Player>
);
}
}
export default App;
The lottieRef prop returns the Lottie instance which you can use to set data and call methods as described in the bodymovin documentation.
import React from 'react';
import { Player } from '@lottiefiles/react-lottie-player';
class App extends React.Component {
constructor(props) {
super(props);
this.state = { lottie: null }; // initialize your state
}
render() {
return (
<Player
lottieRef={instance => {
this.setState({ lottie: instance }); // the lottie instance is returned in the argument of this prop. set it to your local state
}}
autoplay={false}
loop={true}
controls={true}
src="https://assets3.lottiefiles.com/packages/lf20_XZ3pkn.json"
style={{ height: '300px', width: '300px' }}
></Player>
);
}
}
export default App;
import React from 'react';
import { Player } from '@lottiefiles/react-lottie-player';
class App extends React.Component {
constructor(props) {
super(props);
this.player = React.createRef();
}
doSomething() {
this.player.current.play(); // make use of the player and call methods
}
render() {
return (
<Player
onEvent={event => {
if (event === 'load') this.doSomething(); // check event type and do something
}}
ref={this.player}
autoplay={false}
loop={true}
controls={true}
src="https://assets3.lottiefiles.com/packages/lf20_XZ3pkn.json"
style={{ height: '300px', width: '300px' }}
></Player>
);
}
}
export default App;
The following events are exposed and can be listened to via addEventListener
calls.
Name | Description |
---|---|
load | Animation data is loaded. |
error | An animation source cannot be parsed, fails to load or has format errors. |
ready | Animation data is loaded and player is ready. |
play | Animation starts playing. |
pause | Animation is paused. |
stop | Animation is stopped. |
freeze | Animation is paused due to player being invisible. |
loop | An animation loop is completed. |
complete | Animation is complete (all loops completed). |
frame | A new frame is entered. |
pause() => void
Pause animation play.
Type: void
play() => void
Start playing animation.
Type: void
setPlayerDirection(direction: 1 | -1 ) => void
Animation play direction.
Name | Type | Description |
---|---|---|
value | number | Direction values. |
Type: void
setPlayerSpeed(speed?: number) => void
Sets animation play speed.
Name | Type | Description |
---|---|---|
value | number | Playback speed. |
Type: void
stop() => void
Stops animation play.
Type: void
setSeeker(frame: number, play: boolean) => void
Seek to a given frame.
Type: void
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, path).
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.
MIT License © LottieFiles.com
FAQs
Lottie web player wrapper for React
We found that @lottiefiles/react-lottie-player demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.