zoukankan      html  css  js  c++  java
  • react canvas圆环动态百分比

    import React from 'react';
    import Tools from '../../tools/index'
    export default class PercentageRing extends React.Component {
    constructor(props){
    super(props);
    }
    componentDidMount() {
    let canvas = this.refs.ring;
    let width = canvas.offsetWidth;
    let height = canvas.offsetHeight;
    let context = canvas.getContext('2d');
    // this.rings();
    let i = 1;
    let percent = 30;
    this.timerId = setInterval(()=>{
    i >= percent && clearInterval(this.timerId);
    context.clearRect(0,0,width+10,height);
    this.draw(i++);
    },50)
    }
    draw(i){
    let canvas = this.refs.ring;
    let width = canvas.offsetWidth;
    let height = canvas.offsetHeight;
    let context = canvas.getContext('2d');
    let rad = Math.PI*2/100;
    canvas.width = width;
    canvas.height = height;
    // 绘制一个圆
    context.beginPath(); // 开始创建路径
    context.arc(width/2,height/2,height/2-10,0,2*Math.PI,false);
    context.lineWidth = 10;
    context.strokeStyle="#12365a"; // 轮廓颜色
    context.lineCap = "round"; // 绘制圆帽
    context.stroke(); // 通过线条来绘制轮廓
    context.closePath(); // 关闭路径
    // this.draw();
    context.beginPath();
    context.font = `${72/320}rem PingFang SC`;
    context.textAlign = 'right';
    context.textBaseline = 'bottom';
    context.fillStyle="#00a8ff";
    context.fillText('%',width/2+110,height/2+25);
    context.stroke();
    context.closePath();
    // 绘制半圆
    context.beginPath();
    context.arc(width/2,height/2,height/2-10,-Math.PI/2,-Math.PI/2+i*rad,false);
    context.lineWidth = 10;
    // 创建渐变颜色
    let linearGrad = context.createLinearGradient(0,0,width,height);
    linearGrad.addColorStop(0.0, '#02a7ff');
    linearGrad.addColorStop(0.25, '#1da1fb');
    linearGrad.addColorStop(0.5, '#5893f4');
    linearGrad.addColorStop(0.75, '#9484ec');
    context.strokeStyle = linearGrad;
    context.stroke();
    context.closePath();
    // 绘制文本信息
    context.beginPath();
    context.font = `${236/302}rem PingFang SC`;
    context.textAlign = 'center';
    context.textBaseline = 'middle';
    context.fillStyle = "#00a8ff";
    context.fillText(i.toFixed(0),width/2,height/2); // 绘制文本最大宽度
    context.stroke();
    context.closePath();
    }
    componentWillUnmount(){
      this.timerId && clearInterval(this.timerId);
    }
    render() {
    return (
    <div className="perent-ring-box">
    <canvas className="perent-canvas" ref='ring'>
    <p>请使用最新的谷歌、火狐、IE浏览器</p>
    <p>您的浏览器不支持</p>
    </canvas>
    </div>
    )
     
    }
    }
  • 相关阅读:
    推荐一波好的代码托管
    二十一、如何导入svg图片
    二十、滑动开关css
    十九、CSS如何引入字体
    十八、移动端rem布局
    十五、css3 Filter--滤镜
    十四、css动画基础知识
    十三、初始化标签默认样式
    十二、移动端头部声明
    十一、使用a标签打电话、发短信、发邮件
  • 原文地址:https://www.cnblogs.com/BlogRegisterAspx/p/10396033.html
Copyright © 2011-2022 走看看