canvas画时钟

<!DOCTYPE html>
<html>
<head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
        <script>
        window.onload = function() {
                var canvas = document.getElementById("canvas");
                canvas.width = canvas.height = 400;
                canvas.style.background = "white";
                if (canvas.getContext) {
                        var ext = canvas.getContext("2d");
                        drawClock()
                        setInterval(drawClock, 1000);
                }

                function drawClock() {
                        var x = 200;
                        var y = 200;
                        var r = 150;
                        ext.clearRect(0, 0, canvas.width, canvas.height)
                        var oData = new Date();
                        var hours = oData.getHours();
                        var minutes = oData.getMinutes();
                        var seconds = oData.getSeconds();
                        var hoursValue = (-90 + hours * 30 + minutes / 2) * Math.PI / 180; //分针过了30,时针不应该正好在整点上,2分钟一度;
                        var minutesValue = (-90 + minutes * 6) * Math.PI / 180;
                        var secondsValue = (-90 + seconds * 6) * Math.PI / 180;
                        ext.lineWidth = 2
                        ext.arc(x, y, r, 0, Math.PI * 2, false);
                        ext.stroke();
                        //画小刻度
                        for (var i = 0; i < 60; i++) {
                                ext.strokeStyle = "black"
                                ext.lineWidth = 1;
                                ext.beginPath();
                                ext.moveTo(x, y);
                                ext.arc(x, y, r, 6 * i * Math.PI / 180, 6 * (i + 1) * Math.PI / 180, false)
                                ext.closePath();
                                ext.stroke();
                        }
                        drawBlankCircle(10)
                                //画大刻度
                        for (var i = 0; i < 12; i++) {
                                ext.lineWidth = 3;
                                ext.strokeStyle = "green"
                                ext.beginPath();
                                ext.moveTo(x, y);
                                ext.arc(x, y, r, 30 * i * Math.PI / 180, 30 * (i + 1) * Math.PI / 180, false)
                                ext.closePath();
                                ext.stroke();
                        }
                        drawBlankCircle(15)
                                //画空白覆盖圆;
                        function drawBlankCircle(d) {
                                ext.fillStyle = "white"
                                ext.beginPath();
                                ext.arc(x, y, r - d, 0, Math.PI * 2, false);
                                ext.closePath();
                                ext.fill();
                        }
                        //画时针;
                        ext.lineWidth = 5;
                        ext.strokeStyle = "#f90"
                        ext.beginPath();
                        ext.moveTo(x, y);
                        ext.arc(x, y, r - 60, hoursValue, hoursValue, false);
                        ext.closePath();
                        ext.stroke();
                        //画分针;
                        ext.lineWidth = 3;
                        ext.strokeStyle = "red"
                        ext.beginPath();
                        ext.moveTo(x, y);
                        ext.arc(x, y, r - 40, minutesValue, minutesValue, false);
                        ext.closePath();
                        ext.stroke();
                        //画秒针;
                        ext.lineWidth = 1;
                        ext.strokeStyle = "black"
                        ext.beginPath();
                        ext.moveTo(x, y);
                        ext.arc(x, y, r - 25, secondsValue, secondsValue, false);
                        ext.closePath();
                        ext.stroke();
                        //画表盘中心小圆;
                        ext.fillStyle = "black";
                        ext.beginPath();
                        ext.arc(x, y, 6, 0, Math.PI * 2, false);
                        ext.closePath();
                        ext.fill();
                }
        }
        </script>
</head>
<body >
<canvas id="canvas">

</canvas>

</body>
</html>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <script>
    window.onload = function() {
        var canvas = document.getElementById("canvas");
        canvas.width = canvas.height = 400;
        canvas.style.background = "white";
        if (canvas.getContext) {
            var ext = canvas.getContext("2d");
            drawClock()
            setInterval(drawClock, 1000);
        }

        function drawClock() {
            var x = 200;
            var y = 200;
            var r = 150;
            ext.clearRect(0, 0, canvas.width, canvas.height)
            var oData = new Date();
            var hours = oData.getHours();
            var minutes = oData.getMinutes();
            var seconds = oData.getSeconds();
            var hoursValue = (-90 + hours * 30 + minutes / 2) * Math.PI / 180; //分针过了30,时针不应该正好在整点上,2分钟一度;
            var minutesValue = (-90 + minutes * 6) * Math.PI / 180;
            var secondsValue = (-90 + seconds * 6) * Math.PI / 180;
            ext.lineWidth = 2
            ext.arc(x, y, r, 0, Math.PI * 2, false);
            ext.stroke();
            //画小刻度
            for (var i = 0; i < 60; i++) {
                ext.strokeStyle = "black"
                ext.lineWidth = 1;
                ext.beginPath();
                ext.moveTo(x, y);
                ext.arc(x, y, r, 6 * i * Math.PI / 180, 6 * (i + 1) * Math.PI / 180, false)
                ext.closePath();
                ext.stroke();
            }
            drawBlankCircle(10)
                //画大刻度
            for (var i = 0; i < 12; i++) {
                ext.lineWidth = 3;
                ext.strokeStyle = "green"
                ext.beginPath();
                ext.moveTo(x, y);
                ext.arc(x, y, r, 30 * i * Math.PI / 180, 30 * (i + 1) * Math.PI / 180, false)
                ext.closePath();
                ext.stroke();
            }
            drawBlankCircle(15)
                //画空白覆盖圆;
            function drawBlankCircle(d) {
                ext.fillStyle = "white"
                ext.beginPath();
                ext.arc(x, y, r - d, 0, Math.PI * 2, false);
                ext.closePath();
                ext.fill();
            }
            //画时针;
            ext.lineWidth = 5;
            ext.strokeStyle = "#f90"
            ext.beginPath();
            ext.moveTo(x, y);
            ext.arc(x, y, r - 60, hoursValue, hoursValue, false);
            ext.closePath();
            ext.stroke();
            //画分针;
            ext.lineWidth = 3;
            ext.strokeStyle = "red"
            ext.beginPath();
            ext.moveTo(x, y);
            ext.arc(x, y, r - 40, minutesValue, minutesValue, false);
            ext.closePath();
            ext.stroke();
            //画秒针;
            ext.lineWidth = 1;
            ext.strokeStyle = "black"
            ext.beginPath();
            ext.moveTo(x, y);
            ext.arc(x, y, r - 25, secondsValue, secondsValue, false);
            ext.closePath();
            ext.stroke();
            //画表盘中心小圆;
            ext.fillStyle = "black";
            ext.beginPath();
            ext.arc(x, y, 6, 0, Math.PI * 2, false);
            ext.closePath();
            ext.fill();
        }
    }
    </script>
</head>
<body >
<canvas id="canvas">

</canvas>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值