初学H5写时钟

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <style>

        #can{

            border: 1px solid black;

        }

    </style>

</head>

<body>

    <canvas id="can" width="600" height="600"></canvas>

</body>

<script>

    let can=document.getElementById('can')

    let c = can.getContext('2d')

    console.log(c)

    function shi(){

    let deg=Math.PI/180;

   

    let dt=new Date()

    let h =dt.getHours();

    h=h>12?h-12:h;

    let m=dt.getMinutes();

    let s=dt.getSeconds()


 

    c.clearRect(0,0,600,600)


 

    c.beginPath()

    c.strokeStyle='green'

    c.lineWidth='5'

    c.arc(300,300,250,0,360*deg);

    c.stroke()

    c.closePath()

    // c.translate(300,300)

    c.beginPath()

    c.strokeStyle='silver'

    c.arc(300,300,230,0,360*deg);

    c.stroke()

    c.closePath()

    c.beginPath()

    c.strokeStyle='silver'

    c.arc(300,300,10,0,360*deg);

    c.stroke()

    c.closePath()

    for( var i=0;i<60;i++){

        c.save()

        c.translate(300,300)

        c.rotate(deg*6*i)

        c.beginPath()

        c.strokeStyle='silver'

        c.lineWidth='5'

        c.moveTo(0,240);

        c.lineTo(0,248)

        c.stroke()

        c.closePath()

        c.restore()

    }

    for( var i=0;i<12;i++){

        c.save()

        c.translate(300,300)

        c.rotate(deg*30*i)

        c.beginPath()

        c.strokeStyle='green'

        c.lineWidth='8'

        c.moveTo(0,230);

        c.lineTo(0,248)

        c.stroke()

        c.closePath()

        c.restore()

    }

//时

    // c.rotate(-30*deg)

    c.save()

    c.translate(300,300)

    c.beginPath()

    c.rotate(30*deg*h+(30/60)*m*deg)

    c.strokeStyle='green'

    c.lineWidth='8'

    c.moveTo(0,-100);

    c.lineTo(0,0)

    c.stroke()

    c.closePath()

    c.restore();


 

//分

    c.save()

    c.translate(300,300)

    c.rotate(50*deg)

    c.beginPath()

    c.rotate(6*deg*m+(6/60)*s*deg-60*deg)

    c.strokeStyle='silver'

    c.lineWidth='6'

    c.moveTo(0,-150);

    c.lineTo(0,0)

    c.stroke()

    c.closePath()

    c.restore();

//秒

    c.save()

    c.translate(300,300)

    c.beginPath()

    c.rotate(6*deg*s)

    c.strokeStyle='green'

    c.lineWidth='3'

    c.moveTo(0,-200);

   

    c.lineTo(0,0)

    c.stroke()

    c.closePath()

    c.restore()

}

    shi(0)

    setInterval(function(){

        shi(1)

    },1000)


 

</script>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值