JavaScript练习—时钟练习

    <canvas id="clock" width="500" height="500" style="border:1px solid #d3d3d3;">
        您的浏览器不支持 HTML5 canvas 标签。</canvas>
</div>

<script type="text/javascript">
    var clock=document.getElementById("clock");
    var c=clock.getContext("2d");

    draw();
    //自定义一个画图的函数
    function draw(){
        //清理画布
        c.clearRect(0,0,500,500);

        //获取当前时间
        var now=new Date();

        //获取当前秒钟数
        var sec=now.getSeconds();

        //获取当前分钟数
        var min=now.getMinutes();

        //获取小时数
        var hours=now.getHours();


        //当前的分钟占整个60分钟的小数是多少。

        hours=hours+min/60;

        // console.log(hours);
        //把二十四小时转成十二小时制
        hours=(hours>12?hours-12:hours);

        //画表盘
        c.beginPath();
        c.lineWidth=10;
        c.strokeStyle="#abc";
        c.arc(250,250,200,0,2*Math.PI);
        c.closePath();
        c.stroke();




        //小时
        for(var i=0;i<12;i++){
            c.save();
            c.lineWidth=7;
            c.strokeStyle="black";
            //把中心点移动画布中间
            c.translate(250,250);
            c.rotate(i*30*Math.PI/180);
            c.beginPath();
            c.moveTo(0,-170);
            c.lineTo(0,-190);
            c.stroke();
            c.closePath();
            c.restore();
        }

        //  分钟
        for(var i=0;i<60;i++){
            c.save();
            c.lineWidth=5;
            c.strokeStyle="black";
            c.translate(250,250);
            c.rotate(i*6*Math.PI/180);
            c.beginPath();
            c.moveTo(0,-180);
            c.lineTo(0,-190);

            c.stroke();
            c.closePath();
            c.restore();

        }

        //时针
        c.save();
        c.lineWidth=10;
        c.strokeStyle="black";
        c.translate(250,250);
        c.rotate(hours*30*Math.PI/180);
        c.beginPath();
          c.moveTo(0,-120);
           c.lineTo(0,10);
        c.stroke();
        c.closePath();
        c.restore();

        //分针
        c.save();
        c.lineWidth=5;
        c.strokeStyle="black";
        c.translate(250,250);
        c.rotate(min*6*Math.PI/180);
        c.beginPath();
          c.moveTo(0,-160);
        c.lineTo(0,10);
        c.stroke();
        c.closePath();
        c.restore();


        //秒针
        c.save();
        c.lineWidth=3;
        c.strokeStyle="red";
        c.translate(250,250);
        c.rotate(sec*6*Math.PI/180);
        c.beginPath();
        c.moveTo(0,-160);
        c.lineTo(0,10);
        c.stroke();
        c.closePath();
        c.restore();
        //中间小圆点
        c.arc(250,250,5,0,2*Math.PI);

        c.fillStyle="red";
        c.fill();



    }

      setInterval(draw,1000);

   //   alert(draw);


</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值