index.js 5.3 KB
Newer Older
1
import React, { PureComponent } from 'react';
N
niko 已提交
2
import autoHeight from '../autoHeight';
3 4 5
import styles from './index.less';

/* eslint no-return-assign: 0 */
N
niko 已提交
6
/* eslint no-mixed-operators: 0 */
7 8
// riddle: https://riddle.alibaba-inc.com/riddles/2d9a4b90

N
niko 已提交
9 10
@autoHeight()
export default class WaterWave extends PureComponent {
11 12
  state = {
    radio: 1,
N
niko 已提交
13
  };
14 15

  componentDidMount() {
J
jim 已提交
16 17 18 19
    requestAnimationFrame(() => {
      this.renderChart();
      this.resize();
    });
J
jim 已提交
20
    window.addEventListener('resize', this.resize, { passive: true });
21 22
  }

N
nikogu 已提交
23 24 25 26 27 28 29 30 31
  componentWillUnmount() {
    cancelAnimationFrame(this.timer);
    if (this.node) {
      this.node.innerHTML = '';
    }
    window.removeEventListener('resize', this.resize);
  }

  resize = () => {
J
jim 已提交
32
    requestAnimationFrame(() => {
J
jim 已提交
33 34 35 36 37 38 39
      if (this.root) {
        const { height } = this.props;
        const { offsetWidth } = this.root.parentNode;
        this.setState({
          radio: offsetWidth < height ? offsetWidth / height : 1,
        });
      }
A
afc163 已提交
40
    });
N
niko 已提交
41
  };
42 43

  renderChart() {
A
afc163 已提交
44
    const { percent, color = '#1890FF' } = this.props;
45
    const data = percent / 100;
N
nikogu 已提交
46
    const self = this;
47 48 49 50 51 52 53 54 55 56 57 58

    if (!this.node || !data) {
      return;
    }

    const canvas = this.node;
    const ctx = canvas.getContext('2d');

    const canvasWidth = canvas.width;
    const canvasHeight = canvas.height;
    const radius = canvasWidth / 2;
    const lineWidth = 2;
N
niko 已提交
59
    const cR = radius - lineWidth;
60 61

    ctx.beginPath();
A
afc163 已提交
62
    ctx.lineWidth = lineWidth * 2;
63

N
niko 已提交
64
    const axisLength = canvasWidth - lineWidth;
65 66 67 68 69 70 71 72 73
    const unit = axisLength / 8;
    const range = 0.2; // 振幅
    let currRange = range;
    const xOffset = lineWidth;
    let sp = 0; // 周期偏移量
    let currData = 0;
    const waveupsp = 0.005; // 水波上涨速度

    let arcStack = [];
N
niko 已提交
74
    const bR = radius - lineWidth;
75 76 77
    const circleOffset = -(Math.PI / 2);
    let circleLock = true;

N
niko 已提交
78 79
    for (let i = circleOffset; i < circleOffset + 2 * Math.PI; i += 1 / (8 * Math.PI)) {
      arcStack.push([radius + bR * Math.cos(i), radius + bR * Math.sin(i)]);
80 81 82 83 84 85 86 87 88 89 90 91
    }

    const cStartPoint = arcStack.shift();
    ctx.strokeStyle = color;
    ctx.moveTo(cStartPoint[0], cStartPoint[1]);

    function drawSin() {
      ctx.beginPath();
      ctx.save();

      const sinStack = [];
      for (let i = xOffset; i <= xOffset + axisLength; i += 20 / axisLength) {
N
niko 已提交
92
        const x = sp + (xOffset + i) / unit;
93 94
        const y = Math.sin(x) * currRange;
        const dx = i;
N
niko 已提交
95
        const dy = 2 * cR * (1 - currData) + (radius - cR) - unit * y;
96 97 98 99 100 101 102 103 104 105

        ctx.lineTo(dx, dy);
        sinStack.push([dx, dy]);
      }

      const startPoint = sinStack.shift();

      ctx.lineTo(xOffset + axisLength, canvasHeight);
      ctx.lineTo(xOffset, canvasHeight);
      ctx.lineTo(startPoint[0], startPoint[1]);
A
afc163 已提交
106

A
afc163 已提交
107
      const gradient = ctx.createLinearGradient(0, 0, 0, canvasHeight);
A
afc163 已提交
108 109 110
      gradient.addColorStop(0, '#ffffff');
      gradient.addColorStop(1, '#1890FF');
      ctx.fillStyle = gradient;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
      ctx.fill();
      ctx.restore();
    }

    function render() {
      ctx.clearRect(0, 0, canvasWidth, canvasHeight);
      if (circleLock) {
        if (arcStack.length) {
          const temp = arcStack.shift();
          ctx.lineTo(temp[0], temp[1]);
          ctx.stroke();
        } else {
          circleLock = false;
          ctx.lineTo(cStartPoint[0], cStartPoint[1]);
          ctx.stroke();
          arcStack = null;

          ctx.globalCompositeOperation = 'destination-over';
          ctx.beginPath();
          ctx.lineWidth = lineWidth;
          ctx.arc(radius, radius, bR, 0, 2 * Math.PI, 1);

          ctx.beginPath();
          ctx.save();
N
niko 已提交
135
          ctx.arc(radius, radius, radius - 3 * lineWidth, 0, 2 * Math.PI, 1);
136 137 138

          ctx.restore();
          ctx.clip();
A
afc163 已提交
139
          ctx.fillStyle = '#1890FF';
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        }
      } else {
        if (data >= 0.85) {
          if (currRange > range / 4) {
            const t = range * 0.01;
            currRange -= t;
          }
        } else if (data <= 0.1) {
          if (currRange < range * 1.5) {
            const t = range * 0.01;
            currRange += t;
          }
        } else {
          if (currRange <= range) {
            const t = range * 0.01;
            currRange += t;
          }
          if (currRange >= range) {
            const t = range * 0.01;
            currRange -= t;
          }
        }
N
niko 已提交
162
        if (data - currData > 0) {
163 164
          currData += waveupsp;
        }
N
niko 已提交
165
        if (data - currData < 0) {
166 167 168 169 170 171
          currData -= waveupsp;
        }

        sp += 0.07;
        drawSin();
      }
N
nikogu 已提交
172
      self.timer = requestAnimationFrame(render);
173 174 175 176 177 178 179 180 181
    }

    render();
  }

  render() {
    const { radio } = this.state;
    const { percent, title, height } = this.props;
    return (
N
niko 已提交
182 183 184 185 186
      <div
        className={styles.waterWave}
        ref={n => (this.root = n)}
        style={{ transform: `scale(${radio})` }}
      >
A
afc163 已提交
187 188 189 190 191 192 193 194
        <div style={{ width: height, height, overflow: 'hidden' }}>
          <canvas
            className={styles.waterWaveCanvasWrapper}
            ref={n => (this.node = n)}
            width={height * 2}
            height={height * 2}
          />
        </div>
195
        <div className={styles.text} style={{ width: height }}>
N
niko 已提交
196
          {title && <span>{title}</span>}
197 198 199 200 201 202
          <h4>{percent}%</h4>
        </div>
      </div>
    );
  }
}