canvas实现时钟

使用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>

        #canvas {

            border: 1px solid;

            margin: 0 auto;

        }

    </style>

</head>

<body>

    <canvas width="150" height="150" id="canvas"></canvas>

    <script>

        var ctx = document.getElementById("canvas").getContext('2d');

        ctx.translate(75, 75);

        ctx.rotate(-Math.PI / 2); //逆时针旋转九十度

        ctx.lineCap = "round";

        ctx.save();

        clock();

        //绘制小时刻度

        function clock() {

            ctx.clearRect(-75, -75, 150, 150);

            ctx.fillStyle = 'pink';

            ctx.fill();

            ctx.fillRect(-75, -75, 150, 150);

            var date = new Date();

            ctx.lineWidth = 3 //设置线条宽度

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

                ctx.beginPath()

                ctx.rotate(Math.PI / 6)

                ctx.moveTo(68, 0)

                ctx.lineTo(75, 0)

                ctx.stroke()

            }

            //绘制分钟

            ctx.lineWidth = 2 //设置线条宽度

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

                ctx.beginPath();

                if (i % 5 != 0) {

                    ctx.moveTo(72, 0)

                    ctx.lineTo(75, 0)

                    ctx.stroke()

                }

                ctx.rotate(Math.PI / 30);

            }

            var hour = date.getHours();

            var min = date.getMinutes();

            var sec = date.getSeconds();

            console.log(hour, min, sec);

            //绘制时针

            ctx.restore();

            ctx.save();

            ctx.lineWidth = 5;

            hour > 12 ? hour - 12 : hour;

            ctx.rotate((Math.PI / 6) * (hour + min / 60 + sec / 3600));

            ctx.beginPath();

            ctx.moveTo(-10, 0);

            ctx.lineTo(40, 0);

            ctx.stroke();

            //绘制分针

            ctx.restore();

            ctx.save();

            ctx.lineWidth = 3;

            ctx.rotate((Math.PI / 30) * (min + sec / 3600));

            ctx.beginPath();

            ctx.moveTo(-10, 0);

            ctx.lineTo(60, 0);

            ctx.stroke();

            //绘制秒针

            ctx.restore();

            ctx.save();

            ctx.lineWidth = 3;

            ctx.rotate((Math.PI / 30) * (sec));

            ctx.beginPath();

            ctx.moveTo(-10, 0);

            ctx.lineTo(60, 0);

            ctx.strokeStyle = 'red';

            ctx.stroke();

            //在原点出绘制小红原点

            ctx.beginPath();

            ctx.arc(0, 0, 5, 0, 2 * (Math.PI));

            ctx.fillStyle = "red";

            ctx.fill();

            //在秒针末端绘制小红圆圈

            ctx.beginPath();

            ctx.arc(65, 0, 5, 0, 2 * (Math.PI));

            ctx.fillStyle = 'red';

            ctx.fill();

            ctx.restore();

        }

        setInterval(function() {

            clock();

        }, 1000);

    </script>

</body>

</html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值