canvas画圆环%显示

我:
JS代码: 
function circleProgress(id,value,average){ 
var canvas = document.getElementById(id); 
var context = canvas.getContext(‘2d’); 
var _this = $(canvas), 
value= Number(44),// 当前百分比,数值 
average = Number(33),// 平均百分比 
color = “”,// 进度条、文字样式 
maxpercent = 100,//最大百分比,可设置 
c_width = _this.width(),// canvas,宽度 
c_height =_this.height();// canvas,高度 
// 判断设置当前显示颜色 
if( value== maxpercent ){ 
color=”#29c9ad”; 
}else if( value> average ){ 
color=”#27b5ff”; 
}else{ 
color=”#ff6100”; 
} 
// 清空画布 
context.clearRect(0, 0, c_width, c_height); 
// 画初始圆 
context.beginPath(); 
// 将起始点移到canvas中心 
context.moveTo(c_width/2, c_height/2); 
// 绘制一个中心点为(c_width/2, c_height/2),半径为c_height/2,起始点0,终止点为Math.PI * 2的 整圆
context.arc(c_width/2, c_height/2, c_height/2, 0, Math.PI * 2, false); 
context.closePath(); 
context.fillStyle = ‘#ddd’; //填充颜色 
context.fill(); 
// 绘制内圆 
context.beginPath(); 
context.strokeStyle = color; 
context.lineCap = ‘square’; 
context.closePath(); 
context.fill(); 
context.lineWidth = 10.0;//绘制内圆的线宽度
function draw(cur){
    // 画内部空白  
    context.beginPath();  
    context.moveTo(24, 24);  
    context.arc(c_width/2, c_height/2, c_height/2-10, 0, Math.PI * 2, true);  
    context.closePath();  
    context.fillStyle = 'rgba(255,255,255,1)';  // 填充内部颜色
    context.fill();
    // 画内圆
    context.beginPath();
    // 绘制一个中心点为(c_width/2, c_height/2),半径为c_height/2-5不与外圆重叠,
    // 起始点-(Math.PI/2),终止点为((Math.PI*2)*cur)-Math.PI/2的 整圆cur为每一次绘制的距离
    context.arc(c_width/2, c_height/2, c_height/2-5, -(Math.PI / 2), ((Math.PI * 2) * cur ) - Math.PI / 2, false);
    context.stroke();
    //在中间写字  
    context.font = "bold 18pt Arial";  // 字体大小,样式
    context.fillStyle = color;  // 颜色
    context.textAlign = 'center';  // 位置
    context.textBaseline = 'middle';  
    context.moveTo(c_width/2, c_height/2);  // 文字填充位置
    context.fillText(value+"%", c_width/2, c_height/2-20);
    context.fillText("正确率", c_width/2, c_height/2+20);
}
我:
// 调用定时器实现动态效果
var timer=null,n=0;
function loadCanvas(nowT){
    timer = setInterval(function(){
        if(n>nowT){
            clearInterval(timer);
        }else{
            draw(n);
            n += 0.01;
        }
    },15);
}
loadCanvas(value/100);
timer=null;
}; 
最后需用调用circleProgress这个方法,并把相应的参数穿进去。

  

原文地址:https://www.cnblogs.com/shixingwen/p/6049109.html