• canvas画时钟


    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <title>canvas画时钟</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>
  • 相关阅读:
    关于WM_CTLCOLOREDIT的处理的一些问题
    Duilib非官方更新贴~
    一个非常简单的返回局部字符数组的C语言程序, 请问其输出结果?
    更改Windows控制台默认缓冲区行数和宽度
    最新版Duilib在VS2012下编译错误的解决方法
    记C语言浮点数运算处理 "坑" 一则
    修改stb_image.c以让Duilib直接支持Ico格式的图标显示
    一个通过网络转换Ico到Png图片的小小程序(Ico2Png)
    编程调节Win7/Win8系统音量的一种方法
    分享一个最近研究的手机QQ3.0的协议(版本1.4)
  • 原文地址:https://www.cnblogs.com/jone-chen/p/5038417.html
Copyright © 2020-2023  润新知