您好,欢迎访问一九零五行业门户网

Canvas绘制出时钟的代码示例

本篇文章给大家带来的内容是关于canvas绘制出时钟的代码示例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
完整代码:
<!doctype html><html><head>    <meta charset="utf-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <meta http-equiv="x-ua-compatible" content="ie=edge">    <title>document</title>    <style type="text/css">        div {            text-align: center;            margin-top: 250px;        }    </style></head><body>    <div>        <canvas id="clock" height="200px" width="200px">你的浏览器不支持canvas</canvas>    </div>    <script>        var dom = document.getelementbyid('clock');        var ctx = dom.getcontext('2d');        var width = ctx.canvas.width;        var height = ctx.canvas.height;        var r = width / 2;        //绘制表盘        function drawbackground() {            ctx.save();            ctx.translate(r, r);            ctx.beginpath();            ctx.linewidth = 10;            ctx.arc(0, 0, r - 5, 0, 2 * math.pi, false);            ctx.stroke();            var hournumbers = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2];            ctx.font = '18px arial';            ctx.textalign = 'center';            ctx.textbaseline = 'middle';            //小时数字            hournumbers.foreach(function (number, i) {                var rad = 2 * math.pi / 12 * i;                var x = math.cos(rad) * (r - 30);                var y = math.sin(rad) * (r - 30);                ctx.filltext(number, x, y);                // console.log(x)            })            //绘制分刻度            for (var i = 0; i < 60; i++) { var rad = 2 * math.pi / 60 * i; var x = math.cos(rad) * (r - 18); var y = math.sin(rad) * (r - 18); ctx.beginpath(); if (i % 5 == 0) { ctx.fillstyle = '#000'; ctx.arc(x, y, 2, 0, 2 * math.pi, false); } else { ctx.fillstyle = '#ccc'; ctx.arc(x, y, 2, 0, 2 * math.pi, false); } ctx.fill(); } } //绘制时针 function drawhour(hour, minute) { ctx.save(); ctx.beginpath(); var rad = 2 * math.pi / 12 * hour; var mrad = 2 * math.pi / 12 / 60 * minute; ctx.rotate(rad + mrad); ctx.linewidth = 6; ctx.linecap = 'round'; ctx.moveto(0, 10); ctx.lineto(0, -r / 2); ctx.stroke(); ctx.restore(); } //绘制分针 function drawminute(minute) { ctx.save(); ctx.beginpath(); var rad = 2 * math.pi / 60 * minute; ctx.rotate(rad); ctx.linewidth = 3; ctx.linecap = 'round'; ctx.moveto(0, 10); ctx.lineto(0, -r + 30); ctx.stroke(); ctx.restore(); } //绘制秒针 function drawsecond(second) { ctx.save(); ctx.beginpath(); ctx.fillstyle = 'red' var rad = 2 * math.pi / 60 * second; ctx.rotate(rad); ctx.moveto(-2, 20); ctx.lineto(2, 20); ctx.lineto(1, -r + 18); ctx.lineto(-1, -r + 18); ctx.fill(); ctx.restore(); } //绘制指针的端点 function drawdot() { ctx.beginpath(); ctx.fillstyle = 'white'; ctx.arc(0, 0, 3, 0, 2 * math.pi, false); ctx.fill(); } //动起来 function draw() { //清除之前所绘制的 ctx.clearrect(0, 0, width, height); var now = new date(); var hour = now.gethours(); var minute = now.getminutes(); var second = now.getseconds(); drawbackground(); drawhour(hour, minute); drawminute(minute); drawsecond(second) drawdot(); ctx.restore(); } //draw(); setinterval(draw, 1000); </script></body></html>
以上就是canvas绘制出时钟的代码示例的详细内容。
其它类似信息

推荐信息