注册 登录
编程论坛 JavaScript论坛

HTML5 - 另一个时钟

冰镇柠檬汁儿 发布于 2015-05-28 22:45, 1460 次点击
<!DOCTYPE html>

<html lang="en" xmlns="http://www.
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <canvas width="500" height="500" id="clock"></canvas>
    <script>
        function drawClock() {
            var cxt = document.getElementById('clock').getContext('2d'),
                i = 0,
                now = new Date(),
                sec = now.getSeconds(),
                min = now.getMinutes(),
                hour = (now.getHours() % 12) + min / 60;

            cxt.clearRect(0, 0, 500, 500);

            cxt.lineWidth = 10;
            cxt.strokeStyle = 'rgba(0,0,255,0.6)';
            cxt.beginPath()
            cxt.strokeRect(50, 50, 400, 400);
            //cxt.arc(250, 250, 200, 0, 360, false);
            cxt.closePath();
            cxt.stroke();

            //绘制表盘
            cxt.strokeStyle = 'rgba(0,0,0,0.6)';
            for (i = 1; i <= 60; i++) {
                cxt.save();
                if (i % 5 == 0) {
                    cxt.lineWidth = 7;
                }
                else {
                    cxt.lineWidth = 4;
                }
                cxt.translate(250, 250);
                cxt.rotate(i * Math.PI / 30);

                cxt.beginPath();
                if (i % 5 == 0) {
                    cxt.moveTo(0, -170);
                    cxt.font = '18px 黑体';
                    cxt.lineWidth = 1;
                    cxt.strokeText((i / 5), -6, -150);
                    cxt.lineWidth = 7;
                }
                else {
                    cxt.moveTo(0, -180);
                }
                cxt.lineTo(0, -190);
                cxt.closePath();

                cxt.stroke();

                cxt.restore();
            }
            //绘制时针
            cxt.save();
            cxt.lineWidth = 7;
            cxt.strokeStyle = 'rgb(0,0,0)';
            cxt.translate(250, 250);
            cxt.rotate(hour * Math.PI / 6);
            cxt.beginPath();
            cxt.moveTo(0, -110);
            cxt.lineTo(0, 10);
            cxt.closePath();
            cxt.stroke();
            cxt.restore();
            //绘制分针
            cxt.save();
            cxt.lineWidth = 5;
            cxt.strokeStyle = 'rgb(0,0,0)';
            cxt.translate(250, 250);
            cxt.rotate(min * Math.PI / 30);
            cxt.beginPath();
            cxt.moveTo(0, -130);
            cxt.lineTo(0, 15);
            cxt.closePath();
            cxt.stroke();
            cxt.restore();
            //绘制秒针
            cxt.save();
            cxt.lineWidth = 2;
            cxt.strokeStyle = 'rgb(255,0,0)';
            cxt.translate(250, 250);
            cxt.rotate(sec * Math.PI / 30);
            cxt.beginPath();
            cxt.moveTo(0, -150);
            cxt.lineTo(0, 20);
            cxt.closePath();
            cxt.stroke();
            //绘制时针、分针、秒针的交叉点
            cxt.beginPath();
            cxt.arc(0, 0, 6, 0, 360, false);
            cxt.closePath();
            cxt.fillStyle = 'rgba(150,150,150,1)';
            cxt.fill();
            cxt.stroke();
            //设置秒针的尖端的小原点
            cxt.beginPath();
            cxt.arc(0, -130, 5, 0, 360, false);
            cxt.closePath();
            cxt.fillStyle = 'rgba(150,150,50,1)';
            cxt.fill();
            cxt.stroke();
            cxt.restore();
        }

        drawClock();
        setTimeout(function () {
            drawClock()
            setTimeout(arguments.callee, 1000);
        }, 1000);
    </script>
</body>
</html>
9 回复
#2
hu9jj2015-05-29 06:35
都是宝贝
#3
纳兰伽香2015-05-29 09:34
好贴水一下
#4
wp2319572015-05-29 09:39
方钟不好看  圆的好
#5
wmf20142015-05-29 10:07
把秒针做成匀速转动的,别有一番风味
#6
龙牙2015-05-29 12:31
看看
#7
lxwl20122015-05-30 11:23
好帖,看了要顶哦
#8
冰镇柠檬汁儿2015-05-30 13:29
以下是引用wmf2014在2015-5-29 10:07:16的发言:

把秒针做成匀速转动的,别有一番风味

呵呵,那需要获取毫秒,其实也很简单
#9
诸葛欧阳2015-05-30 22:28
封装的函数确实很好用啊
#10
冰镇柠檬汁儿2015-05-30 22:44
这倒是,HTML5确实很强大呢
1