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
| function go() { var date=new Date(); var hours = date.getHours(); hours = hours > 12 ? hours - 12 : hours; var minutes = date.getMinutes(); var seconds = date.getSeconds(); var milliSeconds=date.getMilliseconds();
var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.clearRect(0,0,400,400);
ctx.save(); ctx.beginPath(); ctx.lineWidth = "5"; ctx.translate(200, 200); ctx.rotate(Math.PI / 6 * (hours+minutes/60)); ctx.moveTo(0, 20); ctx.lineTo(0, -80); ctx.stroke(); ctx.restore();
ctx.save(); ctx.beginPath(); ctx.lineWidth = "3"; ctx.translate(200, 200); ctx.rotate(Math.PI / 30 * (minutes+seconds/60)); ctx.moveTo(0, 25); ctx.lineTo(0, -100); ctx.stroke(); ctx.restore();
ctx.save(); ctx.beginPath(); ctx.lineWidth = "1"; ctx.translate(200, 200); ctx.rotate(Math.PI / 30 * (seconds+milliSeconds/1000)); ctx.moveTo(0, 30); ctx.lineTo(0, -120); ctx.stroke(); ctx.restore();
ctx.save(); ctx.beginPath(); ctx.strokeStyle = "#000"; ctx.lineWidth = "4"; ctx.arc(200, 200, 150, 0, Math.PI * 2, false); ctx.stroke(); ctx.restore(); for (var i = 1; i <= 12; i++) { ctx.save(); ctx.beginPath(); ctx.lineWidth = "3"; ctx.translate(200, 200); ctx.rotate(Math.PI / 6 * i); ctx.moveTo(0, -135); ctx.lineTo(0, -150); ctx.stroke(); ctx.restore(); } for (var j = 1; j <= 60; j++) { ctx.save(); ctx.beginPath(); ctx.lineWidth = "1"; ctx.translate(200, 200); ctx.rotate(Math.PI / 30 * j); ctx.moveTo(0, -140); ctx.lineTo(0, -150); ctx.stroke(); ctx.restore(); } ctx.save(); ctx.arc(200, 200, 1, 0, Math.PI * 2, false); ctx.fillStyle = "#fff"; ctx.fill(); ctx.restore(); requestAnimationFrame(go); } requestAnimationFrame(go);
</html>
|