@1hive/lottie-react-web 中文文档教程

发布于 3年前 浏览 30 项目主页 更新于 3年前

Lottie for React, React Native, iOS, and Android

npm 版本许可证



用于 React 的 Lottie 组件 具有运行时动画控制。

Introduction

Lottie 是一个用于 Web、Android 和 iOS 的库,它解析 Adobe After Effects 动画导出为 JSON 和 bodymovin 并在每个平台上本地呈现它们!

设计师第一次可以创建和发布精美的动画,而无需工程师费力地手工重新创建。

这个库是一个 react-lottie 分支,添加了运行时动画控制的功能并修复了很多错误。

Getting Started

通过使用 yarn 或 npm 安装节点模块开始使用 Lottie:

yarn add lottie-react-web

或者

npm i --save lottie-react-web

Usage

组件可以以声明的方式使用:

import React from 'react';
import Lottie from 'lottie-react-web'
import animation from './animation.json'

const App = () => (
  <Lottie
    options={{
      animationData: animation
    }}
  />
)

export default App

默认情况下它会自动循环播放动画。

Lottie 的动画控制可以通过道具来设置。 这是一个对点击做出反应的切换动画的例子:

import React, { Component } from 'react';
import Lottie from 'lottie-react-web'
import toggleAnimation from './toggleAnimation.json'

export default class App extends Component {
  this.state = { isToggled: false }

  render() (
    <div
      onClick={() => {
        this.setState(state => { isToggled: !state.isToggled})
      }}
    >
      <Lottie
        direction={this.state.isToggled ? 1 : -1}
        options={{
          animationData: toggleAnimation,
          loop: false,
        }}
      />
    </div>
  )
)

export default App

API

这些都是可用的道具:

Props

PropDescriptionDefault
optionsMandatory - The object representing the animation settings that will be instantiated by bodymovin. Defines the source of animation (animationData), loop, autoplay, a few others. See details in the section below.{ autoplay: true, loop: true }
animationControlThis is where you can change the animation at runtime. A key value pair of a After Effects property path and the a custom value to apply to it. See details below.
widthSets the width of the animation container.100%
heightSets the heigth of the animation container.100%
isStoppedA boolean flag indicating whether or not the animation is stopped.false
isPausedA boolean flag indicating whether or not the animation is paused.false
speedAn integer indicating the speed of the animation ( 1 is 100%.)1
segmentsAn array of two integers indicating the beginning and ending frame of the animationDefaults to play entire animation
forceSegmentsA boolean indicating wether the segments should play immediately or sequentiallyfalse
directionAn integer indicating wether the animation progresses in the usual (1) or reverse (-1) direction1
ariaRoleA string indicating the animation container ariaRole property"button"
ariaLabelA string indicating the animation container ariaLabel property"animation"
titleA string indicating the animation container title property""

Options object

定义将由 bodymovin 实例化的动画设置。 目前支持 bodymovin 选项的一个子集。

必须指定 animationData 或路径。

PropDescriptionDefault
animationDataMandatory - The source of the animation.
pathMandatory - The path to the animation.
assetsPathMandatory - The root path for external assets.images
loopPlay animation non-stop in a loop.true
autoplayAutomatically play animation when it is instantiated.true
rendererThe method for rendering the animation.svg
rendererSettingsCustomize bodymovin aspect ratio configurations.

Changing animation at runtime

您可以针对特定的 After Effects 图层属性并将其更改为 通过在 属性上设置一个 property 对象来运行时。 示例:

import React from 'react';
import Lottie from 'lottie-react-web'
import animation from './animation.json'

const Animation = ({ x, y }) => (
  <Lottie
    options={{
      animationData: animation,
    }}
    animationControl={{
      'Square,Transform,Position': [x, y],
    }}
  />
)

export default Animation

这将在运行时覆盖 JoyStkCtrl01 层的 Position 值。

Lottie 与 Joystick 'n Sliders After Effects 插件兼容,因此您可以轻松创建令人惊叹的动画。

Lottie for React, React Native, iOS, and Android

npm VersionLicense



Lottie component for React with runtime animation control.

Introduction

Lottie is a library for the Web, Android and iOS that parses Adobe After Effects animations exported as JSON with bodymovin and renders them natively on each platform!

For the first time, designers can create and ship beautiful animations without an engineer painstakingly recreating it by hand.

This library is a react-lottie fork that adds the capability for runtime animation control and fixes lots of bugs.

Getting Started

Get started with Lottie by installing the node module with yarn or npm:

yarn add lottie-react-web

or

npm i --save lottie-react-web

Usage

<Lottie> component can be used in a declarative way:

import React from 'react';
import Lottie from 'lottie-react-web'
import animation from './animation.json'

const App = () => (
  <Lottie
    options={{
      animationData: animation
    }}
  />
)

export default App

By default it will automatically play the animation in loop.

Lottie's animation control can be set via props. Here is an example of a toggle animation that reacts on click:

import React, { Component } from 'react';
import Lottie from 'lottie-react-web'
import toggleAnimation from './toggleAnimation.json'

export default class App extends Component {
  this.state = { isToggled: false }

  render() (
    <div
      onClick={() => {
        this.setState(state => { isToggled: !state.isToggled})
      }}
    >
      <Lottie
        direction={this.state.isToggled ? 1 : -1}
        options={{
          animationData: toggleAnimation,
          loop: false,
        }}
      />
    </div>
  )
)

export default App

API

These are all props available:

Props

PropDescriptionDefault
optionsMandatory - The object representing the animation settings that will be instantiated by bodymovin. Defines the source of animation (animationData), loop, autoplay, a few others. See details in the section below.{ autoplay: true, loop: true }
animationControlThis is where you can change the animation at runtime. A key value pair of a After Effects property path and the a custom value to apply to it. See details below.
widthSets the width of the animation container.100%
heightSets the heigth of the animation container.100%
isStoppedA boolean flag indicating whether or not the animation is stopped.false
isPausedA boolean flag indicating whether or not the animation is paused.false
speedAn integer indicating the speed of the animation ( 1 is 100%.)1
segmentsAn array of two integers indicating the beginning and ending frame of the animationDefaults to play entire animation
forceSegmentsA boolean indicating wether the segments should play immediately or sequentiallyfalse
directionAn integer indicating wether the animation progresses in the usual (1) or reverse (-1) direction1
ariaRoleA string indicating the animation container ariaRole property"button"
ariaLabelA string indicating the animation container ariaLabel property"animation"
titleA string indicating the animation container title property""

Options object

Defines the animation settings that will be instantiated by bodymovin. Currently a subset of the bodymovin options are supported.

Either the animationData OR path must be specified.

PropDescriptionDefault
animationDataMandatory - The source of the animation.
pathMandatory - The path to the animation.
assetsPathMandatory - The root path for external assets.images
loopPlay animation non-stop in a loop.true
autoplayAutomatically play animation when it is instantiated.true
rendererThe method for rendering the animation.svg
rendererSettingsCustomize bodymovin aspect ratio configurations.

Changing animation at runtime

You can target an specific After Effects layer property and change it at runtime by passing setting a property object on the <Lottie> prop. Example:

import React from 'react';
import Lottie from 'lottie-react-web'
import animation from './animation.json'

const Animation = ({ x, y }) => (
  <Lottie
    options={{
      animationData: animation,
    }}
    animationControl={{
      'Square,Transform,Position': [x, y],
    }}
  />
)

export default Animation

This will override the Position value of the layer JoyStkCtrl01 at runtime.

Lottie is compatible with Joystick 'n Sliders After Effects plugin, so you can create amazing animations easily.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文