1. 源代码
import React from 'react';
export default class Index extends React.Component {
componentDidMount() {
let x = 45;
let ctx = this.refs.canvas.getContext('2d');
ctx.beginPath();
ctx.lineWidth = 5;
ctx.strokeStyle = '#F0F8FF';
ctx.arc(100, 100, 90, 0, 2 * Math.PI);
ctx.restore();
ctx.stroke();
ctx.beginPath();
ctx.lineWidth = 5;
ctx.strokeStyle = '#87CEFA';
ctx.arc(100, 100, 80, 0, 2 * Math.PI);
ctx.restore();
ctx.stroke();
ctx.beginPath();
ctx.lineWidth = 10;
ctx.strokeStyle = '#1E90FF';
ctx.arc(100, 100, 80, -90 * Math.PI / 180, (x * 3.6 - 90) * Math.PI / 180);
ctx.stroke();
ctx.font = '40px Arial';
ctx.fillStyle = '#1E90FF';
ctx.textBaseline = 'middle';
ctx.textAlign = 'center';
ctx.fillText(`${x.toFixed(2)}%`, 100, 100);
}
render() {
return (
<div>
app.js
<canvas ref="canvas" width="400" height="400"></canvas>
</div>
);
}
};
2. 效果图
