All files / src/components/GLChart GLChart.jsx

0% Statements 0/27
0% Branches 0/12
0% Functions 0/8
0% Lines 0/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100                                                                                                                                                                                                       
import React, { Component } from 'react';
import PropTypes from 'prop-types';
 
import Loader from '../Loader';
import GL from './GL';
 
import styles from './GLChart.scss';
 
class GLChart extends Component {
  static detectGL() {
    try {
      const canvas = document.createElement('canvas');
      const gl = (canvas.getContext('webgl')
                  || canvas.getContext('experimental-webgl'));
 
      return !!(window.WebGLRenderingContext && gl);
    } catch (e) {
      return false;
    }
  }
 
  static propTypes = {
    data: PropTypes.arrayOf(Object),
    className: PropTypes.string,
    style: PropTypes.shape({}),
  };
 
  static defaultProps = {
    data: [],
    className: '',
    style: {},
  };
 
  constructor(props) {
    super(props);
 
    this.canvas = React.createRef();
    this.state = {
      hasGL: this.constructor.detectGL(),
      GLContext: new GL(),
      baseColor: 0x666666,
    };
  }
 
  componentDidMount() {
    this.updateDataset();
  }
 
  componentDidUpdate(prevProps) {
    const { data } = this.props;
    if (data.toString() !== prevProps.data.toString()) {
      this.updateDataset();
    }
  }
 
  componentWillUnmount() {
    const { GLContext } = this.state;
    GLContext.teardown();
  }
 
  updateDataset = () => {
    const { data } = this.props;
 
    if (data.length) {
      const { GLContext, baseColor } = this.state;
 
      window.requestAnimationFrame(() => {
        GLContext.init(this.canvas.current, data, baseColor);
      });
    }
  }
 
  render() {
    const { className, style, data } = this.props;
    const { hasGL } = this.state;
 
    if (!data.length) {
      return (<Loader />);
    }
 
    return (
      hasGL
        ? (
          <div
            className={`${styles.viewport} ${className}`}
            style={style}
          >
            <canvas
              ref={this.canvas}
              className={styles.gl}
            />
          </div>
        )
        : <div />
    );
  }
}
 
export default GLChart;