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
| let canvas1 = document.querySelector("#canvas1"); let ctx = canvas1.getContext("2d"); ctx.clearRect(0, 0, 800, 600); ctx.save(); ctx.translate(400, 300); ctx.rotate(-Math.PI / 2);
ctx.beginPath(); ctx.arc(0, 0, 200, 0, 2 * Math.PI); ctx.strokeStyle = "darkgrey"; ctx.lineWidth = 10; ctx.stroke(); ctx.closePath();
function renderClock() { ctx.clearRect(0, 0, 800, 600); ctx.save(); ctx.translate(400, 300); ctx.rotate(-Math.PI / 2); ctx.beginPath(); ctx.arc(0, 0, 200, 0, 2 * Math.PI); ctx.strokeStyle = "darkgrey"; ctx.lineWidth = 10; ctx.stroke(); ctx.closePath();
function drawLine({ count, start, end, width, color }) { let i = 0; for (; i < count; i++) { ctx.beginPath(); ctx.rotate((2 * Math.PI) / count); ctx.moveTo(start, 0); ctx.lineTo(end, 0); ctx.lineWidth = width; ctx.strokeStyle = color; ctx.stroke(); ctx.closePath(); } } drawLine({ count: 60, start: 180, end: 190, width: 2, color: "orangered", }); drawLine({ count: 12, start: 180, end: 200, width: 10, color: "darkgrey", });
let time = new Date(); let min = time.getMinutes(); let sec = time.getSeconds(); let hour = time.getHours(); hour = hour > 12 ? hour - 12 : hour; ctx.save(); ctx.beginPath(); ctx.rotate(((2 * Math.PI) / 60) * sec); ctx.moveTo(-30, 0); ctx.lineTo(170, 0); ctx.lineWidth = 2; ctx.strokeStyle = "red"; ctx.stroke(); ctx.closePath(); ctx.restore(); ctx.save(); ctx.beginPath(); ctx.rotate(((2 * Math.PI) / 60) * min + ((2 * Math.PI) / 3600) * sec); ctx.moveTo(-20, 0); ctx.lineTo(150, 0); ctx.lineWidth = 4; ctx.strokeStyle = "darkblue"; ctx.stroke(); ctx.closePath(); ctx.restore(); ctx.save(); ctx.beginPath(); ctx.rotate( ((2 * Math.PI) / 12) * hour + ((2 * Math.PI) / 12 / 60) * min + ((2 * Math.PI) / 12 / 60 / 60) * sec ); ctx.moveTo(-10, 0); ctx.lineTo(130, 0); ctx.lineWidth = 6; ctx.strokeStyle = "darkgrey"; ctx.stroke(); ctx.closePath(); ctx.restore(); ctx.beginPath(); ctx.arc(0, 0, 10, 0, 2 * Math.PI); ctx.fillStyle = "deepskyblue"; ctx.fill(); ctx.closePath(); ctx.restore(); } setInterval(function () { renderClock(); }, 1000);
|