xiangjiejie
on the load

导航

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta name="viewport" content="initial-scale=1, width=device-width, maximum-scale=1, user-scalable=no">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>

<body>
<style>
    #canvas{display:block;margin:100px auto; background:#fff;}
</style>
<canvas id="canvas" width="300" height="300"></canvas>
    <script type="text/javascript">
        window.onload = function(){
            var canvas = document.getElementById("canvas");
            var ctx = canvas.getContext("2d");
            var W = canvas.width;
            var H = canvas.height;
            var deg=0,new_deg=0,dif=0;
            var loop,re_loop;
            var text,text_w;

            function init(){
                ctx.clearRect(0,0,W,H);
                ctx.beginPath();
                ctx.strokeStyle="#ebebeb";
                ctx.lineWidth=5;
                ctx.arc(W/2,H/2,70,0,Math.PI*2,false);
                ctx.stroke();

                var r = deg*Math.PI/180;
                ctx.beginPath();
                ctx.strokeStyle = "#f31437";
                ctx.lineWidth=5;
                ctx.arc(W/2,H/2,70,0-90*Math.PI/180,r-90*Math.PI/180,false);
                ctx.stroke();

                ctx.fillStyle="#f00";
                ctx.font="50px abc";
                text = Math.floor(deg/360*100)+"%";
                text_w = ctx.measureText(text).width;
                ctx.fillText(text,W/2 - text_w/2,H/2+15);
            }
            function draw(){
                new_deg = 250;//设置角度
                loop = setInterval(to,1000/dif);
            }
            function to(){
                if(deg == new_deg){
                    clearInterval(loop);
                }
                if(deg<new_deg){
                    deg++;
                }else{
                    deg--;
                }
                init();
            }
            draw();
            re_loop = setInterval(draw,2000);
        }
    </script>
</body>
</html>

 

posted on 2015-07-07 10:30  xiangjiejie  阅读(349)  评论(0编辑  收藏  举报