html5 canvas progressbar 进度条

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>html5实现的简单进度条效果 - www.codefans.net</title>
<script type="text/javascript">
        var i = 0;
        var res = 0;
        var context = null;
        var total_width = 500;
        var total_height = 34;
        var initial_x = 20;
        var initial_y = 600;
        var radius = total_height/2;
        window.onload = function() {
            var elem = document.getElementById('myCanvas');
            if (!elem || !elem.getContext) {
                return;
            }
            context = elem.getContext('2d');
            if (!context) {
                return;
            }
            context.font = "16px Verdana";
            var progress_lingrad = context.createLinearGradient(0,initial_y+total_height,0,0);
            progress_lingrad.addColorStop(0, '#4DA4F3');
            progress_lingrad.addColorStop(0.4, '#ADD9FF');
            progress_lingrad.addColorStop(1, '#9ED1FF');
            context.fillStyle = progress_lingrad;
            res = setInterval(draw, 300);
        }

        function draw() {
            i+=5;
            context.clearRect(initial_x-5,initial_y-5,total_width+15,total_height+15);
            progressLayerRect(context, initial_x, initial_y, total_width, total_height, radius);
            progressBarRect(context, initial_x, initial_y, i, total_height, radius, total_width);
            progressText(context, initial_x, initial_y, i, total_height, radius, total_width );
            if (i>=total_width) {
                clearInterval(res);
            }
        }
        function roundRect(ctx, x, y, width, height, radius) {
            ctx.beginPath();
            ctx.moveTo(x + radius, y);
            ctx.lineTo(x + width - radius, y);
            ctx.arc(x+width-radius, y+radius, radius, -Math.PI/2, Math.PI/2, false);
            ctx.lineTo(x + radius, y + height);
            ctx.arc(x+radius, y+radius, radius, Math.PI/2, 3*Math.PI/2, false);
            ctx.closePath();
            ctx.fill();
        }
        function roundInsetRect(ctx, x, y, width, height, radius) {
            ctx.beginPath();
            ctx.moveTo(1000, 1000);
            ctx.lineTo(1000, -1000);
            ctx.lineTo(-1000, -1000);
            ctx.lineTo(-1000, 1000);
            ctx.lineTo(1000, 1000);
            ctx.moveTo(x + radius, y);
            ctx.lineTo(x + width - radius, y);
            ctx.arc(x+width-radius, y+radius, radius, -Math.PI/2, Math.PI/2, false);
            ctx.lineTo(x + radius, y + height);
            ctx.arc(x+radius, y+radius, radius, Math.PI/2, 3*Math.PI/2, false);
            ctx.closePath();
            ctx.fill();
        }
        function progressLayerRect(ctx, x, y, width, height, radius) {
            ctx.save();
            ctx.shadowOffsetX = 2;
            ctx.shadowOffsetY = 2;
            ctx.shadowBlur = 5;
            ctx.shadowColor = '#666';
            ctx.fillStyle = 'rgba(189,189,189,1)';
            roundRect(ctx, x, y, width, height, radius);
            ctx.shadowColor = 'rgba(0,0,0,0)'
            var lingrad = ctx.createLinearGradient(0,y+height,0,0);
            lingrad.addColorStop(0, 'rgba(255,255,255, 0.1)');
            lingrad.addColorStop(0.4, 'rgba(255,255,255, 0.7)');
            lingrad.addColorStop(1, 'rgba(255,255,255,0.4)');
            ctx.fillStyle = lingrad;
            roundRect(ctx, x, y, width, height, radius);
            ctx.fillStyle = 'white';
            ctx.restore();
        }
        function progressBarRect(ctx, x, y, width, height, radius, max) {
            var offset = 0;
            ctx.beginPath();
            if (width<radius) {
                offset = radius - Math.sqrt(Math.pow(radius,2)-Math.pow((radius-width),2));
                ctx.moveTo(x + width, y+offset);
                ctx.lineTo(x + width, y+height-offset);
                ctx.arc(x + radius, y + radius, radius, Math.PI - Math.acos((radius - width) / radius), Math.PI + Math.acos((radius - width) / radius), false);
            }
            else if (width+radius>max) {
                offset = radius - Math.sqrt(Math.pow(radius,2)-Math.pow((radius - (max-width)),2));
                ctx.moveTo(x + radius, y);
                ctx.lineTo(x + width, y);
                ctx.arc(x+max-radius, y + radius, radius, -Math.PI/2, -Math.acos((radius - (max-width)) / radius), false);
                ctx.lineTo(x + width, y+height-offset);
                ctx.arc(x+max-radius, y + radius, radius, Math.acos((radius - (max-width)) / radius), Math.PI/2, false);
                ctx.lineTo(x + radius, y + height);
                ctx.arc(x+radius, y+radius, radius, Math.PI/2, 3*Math.PI/2, false);
            }
            else {
                ctx.moveTo(x + radius, y);
                ctx.lineTo(x + width, y);
                ctx.lineTo(x + width, y + height);
                ctx.lineTo(x + radius, y + height);
                ctx.arc(x+radius, y+radius, radius, Math.PI/2, 3*Math.PI/2, false);
            }
            ctx.closePath();
            ctx.fill();
            if (width<max-1) {
                ctx.save();
                ctx.shadowOffsetX = 1;
                ctx.shadowBlur = 1;
                ctx.shadowColor = '#666';
                if (width+radius>max)
                  offset = offset+1;
                ctx.fillRect(x+width,y+offset,1,total_height-offset*2);
                ctx.restore();
            }
        }
        function progressText(ctx, x, y, width, height, radius, max) {
            ctx.save();
            ctx.fillStyle = 'white';
            var text = Math.floor(width/max*100)+"%";
            var text_width = ctx.measureText(text).width;
            var text_x = x+width-text_width-radius/2;
            if (width<=radius+text_width) {
                text_x = x+radius/2;
            }
            ctx.fillText(text, text_x, y+22);
            ctx.restore();
        }
</script>
</head>
<body>
<p><canvas id="myCanvas" width="1000" height="1500">您的浏览器不支持canvas.</canvas></p>
</body>
</html>

posted @ 2015-11-15 20:49  alxe_yu  阅读(349)  评论(0)    收藏  举报