1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| html页面代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" > <meta name="viewport" content="width=device-width,initial-scale=1"> <title>canvas</title> <style type="text/css"> div { text-align: center; margin-top: 250px; } #appStudy{ border:1px solid #cccccc; } </style> </head>
<body> <div > <canvas id="app_study" ></canvas> </div> <script type="text/javascript" src="appStudy.js"></script> </body>
</html> 脚本代码 var dom = document.getElementById("clock"); var ctx = dom.getContext("2d"); var height = ctx.canvas.height; var width = ctx.canvas.width; var r = height / 2; var rem = width / 200; var ss = 0; //表盘 function drawBackgroup() { ctx.save(); ctx.translate(r, r); ctx.beginPath(); ctx.lineWidth = 8 * rem; ctx.arc(0, 0, r - ctx.lineWidth, 2 * Math.PI, false); ctx.stroke(); //画表盘数字 var hourNumber = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2]; ctx.font = 18 * rem + "px Arial"; ctx.textAlign = "center"; ctx.textBaseline = "middle" hourNumber.forEach(function (number, i) { var rad = 2 * Math.PI / 12 * i; var x = Math.cos(rad) * (r - 30 * rem); var y = Math.sin(rad) * (r - 30 * rem); ctx.fillText(number, x, y); }); //刻度点 for (var i = 0; i < 60; i++) { var rad = 2 * Math.PI / 60 * i; var x = Math.cos(rad) * (r - 18 * rem); var y = Math.sin(rad) * (r - 18 * rem); ctx.beginPath();
if (i % 5 == 0) { ctx.fillStyle = "#000000"; ctx.arc(x, y, 2 * rem, 0, 2 * Math.PI, false); } else { ctx.fillStyle = "#cccccc"; ctx.arc(x, y, 2 * rem, 0, 2 * Math.PI, false); } ctx.fill(); }; } //时针 function drawHour(huor, min) { ctx.save(); ctx.beginPath(); ctx.fillStyle = "#000000"; var sss = Math.PI / 360 * min; var hoss = 2 * Math.PI * huor / 12; ss = hoss + sss; ctx.rotate(ss); ctx.lineWidth = 3 * rem; ctx.lineCap = "round"; ctx.moveTo(-6 * rem, 15 * rem) ctx.lineTo(6 * rem, 15 * rem); ctx.lineTo(2, -r / 2); ctx.lineTo(-2, -r / 2); ctx.fill(); ctx.restore(); } //分针 function drawMinute(min) { ctx.save(); ctx.beginPath(); ctx.fillStyle = "#000"; ctx.rotate(2 * Math.PI * min / 60); ctx.lineWidth = 6 * rem; ctx.lineCap = "round"; ctx.moveTo(-2 * rem, 40 * rem); ctx.lineTo(0, -r / 12 * 7); ctx.stroke(); ctx.restore(); } //秒针 function drawMiao(miao) { ctx.save(); ctx.beginPath(); ctx.fillStyle = "#ff0000"; ctx.rotate(2 * Math.PI * miao / 60); ctx.lineWidth = 3 * rem; ctx.lineCap = "round"; ctx.moveTo(-3 * rem, 45 * rem) ctx.lineTo(3 * rem, 45 * rem); ctx.lineTo(1, -r / 3 * 2); ctx.lineTo(-1, -r / 3 * 2); ctx.fill(); ctx.restore(); } //表中心点 function drawDot() { ctx.beginPath(); ctx.fillStyle = "#fff"; ctx.arc(0, 0, 2 * rem, 0, 2 * rem * Math.PI, false); ctx.fill(); ctx.stroke(); }
function draw() { ctx.clearRect(0, 0, width, height); var nowTime = new Date(); var hour = nowTime.getHours(); var min = nowTime.getMinutes(); var miao = nowTime.getSeconds();
drawBackgroup(); drawHour(hour, min); drawMinute(min); drawMiao(miao); drawDot(); ctx.restore(); } draw(); //定时器 setInterval(draw, 1000);
|