简体   繁体   English

在 React 中加载带有图像的 Lottie 动画

[英]Loading a Lottie animation with images in React

I'm trying to include a Lottie animation in my React project.我正在尝试在我的 React 项目中包含 Lottie 动画。 It successfully loads the Lottie component, but without images.它成功加载了 Lottie 组件,但没有图像。 It can't resolve the assets.它无法解决资产。 However, it is delivered to me as a json (animation data) and an image directory containing the images.但是,它作为 json(动画数据)和包含图像的图像目录提供给我。

The way I'm trying to solve this is by placing the json data in a javascript file, this js file imports all images and uses these imports in the json data.我试图解决这个问题的方法是将 json 数据放在一个 javascript 文件中,这个 js 文件导入所有图像并在 json 数据中使用这些导入。 This way React will resolve the correct path for these images.这样 React 将解析这些图像的正确路径。 The issue I'm facing now is that I can't seem to correctly import/serve this javascript file to the animationData property of the Lottie component.我现在面临的问题是我似乎无法正确地将此 javascript 文件导入/提供给 Lottie 组件的 animationData 属性。

Here's what I've got so far.这是我到目前为止所得到的。 And it results in Uncaught Error: Cannot find module '../../assets/animations/background-animation/animation.js'它导致Uncaught Error: Cannot find module '../../assets/animations/background-animation/animation.js'

BackgroundAnimation.tsx背景动画.tsx

import * as React from 'react';
import Lottie from 'react-lottie';
import animationData from '../../assets/animations/background-animation/animation.js';

export default class BackgroundAnimation extends React.Component {

    constructor(props) {
      super(props);
      this.state = {isStopped: false, isPaused: false};
    }

    render() {  
      const defaultOptions = {
        loop: true,
        autoplay: true, 
        animationData: animationData,
        rendererSettings: {
          preserveAspectRatio: 'xMidYMid slice'
        }
      };

      return <Lottie options={defaultOptions}/>
    }
  }

animation.js动画.js

import img_0 from './images/img_0.png'
import img_1 from './images/img_1.png'
...

export default {
    "v": "5.4.4",
    "fr": 30,
    "ip": 0,
    "op": 930,
    "w": 1920,
    "h": 1080,
    "nm": "TV - landscape",
    "ddd": 0,
    "assets": [
        {
            "id": "image_0",
            "w": 2349,
            "h": 1134,
            "u": "",
            "p": img_0,
            "e": 0
        },
        {
            "id": "image_1",
            "w": 1138,
            "h": 1139,
            "u": "",
            "p": img_1,
            "e": 0
        },
        ...
     ]
}

Just export it like you did above, and then create a component (I assume you are in React) like so:只需像上面一样导出它,然后像这样创建一个组件(我假设你在 React 中):

import React from 'react';
import Lottie from 'react-lottie';

import animation from './animation';

const Animation = ({ animePaused }) => {
  return (
    <Lottie
      options={{
        animationData: animation,
        autoplay: true
      }}
      height={200}
      isStopped={animePaused}
    />
  );
};

export default Animation;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM