canvas绘制时钟

方法一:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<style>
    *{margin:0;padding: 0}
</style>
<body>
    <canvas id="cav" width="800" height="500" style="border:1px solid red"></canvas>
</body>
  <script>
       var cvs = document.getElementById("cav")
       var bi = cvs.getContext("2d")

       function biao() { 
            var x=400,y=250,r=200;

            var date = new Date()
            var shi = date.getHours();
            var fen = date.getMinutes();
            var miao = date.getSeconds();
            var hours = (-90+shi*30 + fen/2)*Math.PI/180
            var minutes = (-90+fen*6)*Math.PI/180
            var scends = (-90+miao*6)*Math.PI/180
    
         //清空表盘
          bi.clearRect(0,0,cvs.width,cvs.height)
        //分针刻盘
            bi.beginPath()
           bi.strokeStyle="blue"

            for(i= 0;i<60;i++){
                bi.moveTo(x,y)
                bi.arc(x,y,r,6*i*Math.PI/180,6*(i+1)*Math.PI/180,false)
            }
            bi.stroke()
            bi.closePath()
        //(第二层覆盖第一层中心区域)
                bi.fillStyle="#fff"
                bi.beginPath()      
                bi.moveTo(x,y)
                bi.arc(x,y,r*19/20,0,360*Math.PI/180,false)
                bi.fill()
                bi.closePath()
        //时针刻盘
           bi.beginPath()
           bi.lineWidth=3;
           bi.strokeStyle="black"
            for(i= 0;i<12;i++){
                bi.moveTo(x,y)
                bi.arc(x,y,r,30*i*Math.PI/180,30*(i+1)*Math.PI/180,false)
            }
           
            bi.stroke()
             bi.closePath()
         //(第3层覆盖第2层中心区域)
                bi.beginPath()   
                bi.fillStyle="#fff"
                bi.moveTo(x,y)
                bi.arc(x,y,r*18/20,0,360*Math.PI/180,false)
                   bi.fill()
                bi.closePath()

             
           //绘制时针 
          
              bi.beginPath()
              bi.lineWidth=5  
              bi.strokeStyle="green"                    
              bi.moveTo(x,y)
              bi.arc(x,y,r*10/20,hours,hours,false)
              bi.stroke()
              bi.closePath()
             
               //绘制分针 
           
              bi.beginPath() 
                  bi.lineWidth=3 
              bi.strokeStyle="orange"                 
              bi.moveTo(x,y)
              bi.arc(x,y,r*14/20,minutes,minutes,false)
              
              bi.stroke()
              bi.closePath()
              //绘制秒针
           bi.beginPath()
            bi.lineWidth=1
            bi.strokeStyle="red"   
              bi.moveTo(x,y)
              bi.arc(x,y,r*18/20,scends,scends,false)
              bi.stroke()
              bi.closePath()
              //绘制圆心
              bi.beginPath()
              bi.fillStyle="#000"
              bi.moveTo(x,y)
              bi.arc(x,y,r*1/30,2*Math.PI,false)
              bi.fill()
              //bi.stroke()
              bi.closePath()

         }

        biao() 
       
        setInterval(biao,1000)
        

   
  
  </script>
</html>

 

 

方法二:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>canvas 时钟</title>
    <style>
        *{margin: 0; padding: 0;}
        body{
            background: radial-gradient(#777, #000);
            background: -webkit-radial-gradient(#777, #000);
            background: -moz-radial-gradient(#777, #000);
        }
        div.dd{
            margin: 0 auto;
            width: 400px;
            padding-top: 100px;
        }
    </style>
</head>
<body>
<div class="dd">
    <canvas id="clock" height="400px" width="400px">你的浏览器不支持canvas</canvas>
</div>
<script type="text/javascript">
    (function(doc){
        var can = doc.getElementById("clock");
        var con = can.getContext("2d");//创建一个2d上下文
        con.translate(200, 200);//重新确定坐标原点(确定圆心位置)
 
        //外圆
        con.beginPath();
        con.fillStyle = '#fff';
        con.arc(0, 0, 200, 0, Math.PI*2, false);
        con.fill();
 
        //内圆
        con.beginPath();
        con.fillStyle = '#aaa';
        con.arc(0, 0, 195, 0, Math.PI*2, false);
        con.fill();
 
        //绘制刻度
        con.beginPath();
        con.font = "bold 20px sans-serif";
        con.textAlign = "center";
        con.textBaseline = "middle"
        con.fillStyle = '#000000';
        con.fillText("12", 0, -170);
        con.fillText("3",170,0);
        con.fillText("6",0,170);
        con.fillText("9",-170,0);
        con.fillText("1",84,-147.224);
        con.fillText("2",147.224,-84);
        con.fillText("4",147.224,84);
        con.fillText("5",84,147.224);
        con.fillText("7",-84,147.224);
        con.fillText("8",-147.224,84);
        con.fillText("10",-147.224,-84);
        con.fillText("11",-84,-147.224);
 
        //获取当前时间
        var d = new Date(), time ={};
        time.H = d.getHours()%12;
        time.M = d.getMinutes();
        time.S = d.getSeconds();
 
        function getTime(){
            time.S++;
            if(time.S > 59){
                time.S = 0;
                time.M++;
                if(time.M > 60){
                    time.M = 0;
                    time.H++;
                    if(time.H > 11){
                        time.H = 0;
                    }
                }
            }
            canvasTimeLine();
        }
 
        //将角度转为弧度
        function numToDeg(num){
            return ((num*6 - 90)*0.0174444444444444);
 
        }
        //确定刻度位置
        function computePositionByLenDeg(len, deg){
            return {
                x: len*Math.cos(deg),
                y: len*Math.sin(deg)
            }
        }
        //绘制刻度
        function canvasLineMintus(){
            for(var i = 0;i<60; i++){
                var rec = 180;
                con.beginPath();
                con.lineWidth = 4;
                if(i%5 != 0){
                    con.lineWidth = 1;
                    rec = 184
                }
 
                var beinDeg = numToDeg(i),
                        beginPot = computePositionByLenDeg(rec, beinDeg),
                        endPot = computePositionByLenDeg(190, beinDeg);
                con.moveTo(beginPot.x, beginPot.y);
                con.lineTo(endPot.x, endPot.y);
                con.stroke();
            }
        }
 
        function canvasTimeLine(){
            var sDeg = numToDeg(time.S),
                    mDeg = numToDeg(time.M),
                    hDeg = numToDeg(time.H*5 + Math.floor(time.M/12)),
                    sPot = computePositionByLenDeg(150, sDeg),
                    mPot = computePositionByLenDeg(135, mDeg),
                    hPot = computePositionByLenDeg(110, hDeg);
 
            //时钟表面
            con.beginPath();
            con.fillStyle = '#ddd';
            con.arc(0, 0, 156, 0, Math.PI*2, false);
            con.fill();
 
 
 
            //时针
            con.beginPath();
            con.moveTo(0, 0);
            con.lineTo(hPot.x, hPot.y);
            con.lineWidth = 6;
            con.strokeStyle = "#333";
            con.stroke();
 
            //分针
            con.beginPath();
            con.moveTo(0, 0);
            con.lineTo(mPot.x, mPot.y);
            con.lineWidth = 4;
            con.strokeStyle = "#333";
            con.stroke();
 
            //秒针
            con.beginPath();
            con.moveTo(0, 0);
            con.lineTo(sPot.x, sPot.y);
            con.lineWidth = 2;
            con.strokeStyle = "#ff0000";
            con.stroke();
 
            //针心
            con.beginPath();
            con.fillStyle = '#aaaaaa';
            con.arc(0, 0, 8, 0, Math.PI*2, false);
            con.fill();
            con.beginPath();
            con.fillStyle = '#f00';
            con.arc(0, 0, 4, 0, Math.PI*2, false);
            con.fill();
 
        }
        canvasLineMintus();
        getTime();
 
        setInterval(getTime, 1000);
 
    })(document);
</script>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值