繁体   English   中英

React 组件中 SVG 的 JSON 格式

[英]JSON format of SVG inside React Component

我使用 AfterEffects 的 bodymovin 扩展来创建动画并将其导出为与网络兼容的 JSON。 我试着按照这个演练来弄清楚如何将它包含在一个组件中: https ://gist.github.com/prettyhandsome/caa7386dcda76fde28cf5d2a07a12082#file-devmyndshapes-jsx

但是,我没有运气。 是否有关于如何将 JSON 作为图像包含在 React 组件中的良好的、循序渐进的在线资源? 这是我失败的尝试:

import React, { Component } from 'react';
import Plx from 'react-plx';
import PropTypes from 'prop-types';
import bodymovin from 'bodymovin';
import animationData from '../../animations/test.json';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { skyChange } from './redux/actions';



export class Home extends Component {
  static propTypes = {
    home: PropTypes.object.isRequired,
    actions: PropTypes.object.isRequired,
  };

  componentDidMount() {
    this.attachAnimation();
  }

  animationIsAttached = false;
  const animationProperties = {
    //the is the DOM element that you would like to attach your animation to
    container: this.animationContainer,
    //the renderer options are  svg, canvas, or html
    renderer: 'svg',
    //true plays the animation in a continuous loop
    loop: true,
    //true plays the animation when the view loads
    autoplay: true,
    //the JSON data, which we imported above
    animationData: animationData
  }
  attachAnimation = () => {
    if (this.animationContainer !== undefined && !this.animationIsAttached) {
      const animationProperties = {
        container: this.animationContainer,
        renderer: 'svg',
        loop: true,
        autoplay: true,
        animationData: animationData
      }

      bodymovin.loadAnimation(animationProperties);
    }
  }


  render() {

    const { skyState, splashState } = this.props.home;
    const { skyChange } = this.props.actions;



    return (
      <div className="testing-animation">
         <h1> Animation Attempt </h1>
         <div style={{width: 50, height: 50}} ref={(animationDiv) => { this.animationContainer = animationDiv; }}/>
         </div>
       </div>
    );
  }
}

/* istanbul ignore next */
function mapStateToProps(state) {
  return {
    home: state.home,
  };
}

/* istanbul ignore next */
function mapDispatchToProps(dispatch) {
  return {
    actions: bindActionCreators({ skyChange }, dispatch)
  };
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(Home);

替换代码 -

animationData: animationData

animationData: animationData.default

暂无
暂无

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

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