H5动画,canvas绘制圆环百分比进度的动态效果


<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
<input οnclick="circleProgress(20,20)" value="画圆" type="button">  <!--circleProgress(20,20) 第一个参数是正确率,第二个参数如果比第一个大,显示颜色不同-->
<canvas id="yuan"></canvas>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script>
    function circleProgress(value,average){
        var canvas = document.getElementById("yuan");
        var context = canvas.getContext('2d');
        var _this = $(canvas),
            value= Number(value),// 当前百分比,数值
            average = Number(average),// 平均百分比
            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;
    };
</script>
</body>
</html>



js代码 [removed] var wave = (function () { var ctx; var waveImage; var canvasWidth; var canvasHeight; var needAnimate = false; function init (callback) { var wave = document.getElementById('wave'); var canvas = document.createElement('canvas'); if (!canvas.getContext) return; ctx = canvas.getContext('2d'); canvasWidth = wave.offsetWidth; canvasHeight = wave.offsetHeight; canvas.setAttribute('width', canvasWidth); canvas.setAttribute('height', canvasHeight); wave.appendChild(canvas); waveImage = new Image(); waveImage.onload = function () { waveImage.onload = null; callback(); } waveImage.src = 'images/wave.png'; } function animate () { var waveX = 0; var waveY = 0; var waveX_min = -203; var waveY_max = canvasHeight * 0.7; var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) { window.setTimeout(callback, 1000 / 60); }; function loop () { ctx.clearRect(0, 0, canvasWidth, canvasHeight); if (!needAnimate) return; if (waveY < waveY_max) waveY = 1.5; if (waveX < waveX_min) waveX = 0; else waveX -= 3; ctx.globalCompositeOperation = 'source-over'; ctx.beginPath(); ctx.arc(canvasWidth/2, canvasHeight/2, canvasHeight/2, 0, Math.PI*2, true); ctx.closePath(); ctx.fill(); ctx.globalCompositeOperation = 'source-in'; ctx.drawImage(waveImage, waveX, canvasHeight - waveY); requestAnimationFrame(loop); } loop(); } function start () { if (!ctx) return init(start); needAnimate = true; setTimeout(function () { if (needAnimate) animate(); }, 500); } function stop () { needAnimate = false; } return {start: start, stop: stop}; }()); wave.start(); [removed]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值