DIV CSS 佈局教程網

以 JavaScript 與 Canvas 繪製時鐘的完整範例
編輯:關於JavaScript     

Canvas 的旋轉與繪圖 API 很適合拿來做時鐘:把座標原點移到畫布中心,再用 rotate 依角度畫出刻度與指針,整段程式碼其實相當直觀。以下是一個可直接執行的範例。

1. 頁面與繪圖程式

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas 時鐘</title>
</head>
<body>
<canvas id="canvas" width="300" height="300"></canvas>
<script>
var canvas = document.getElementById("canvas");
var context = canvas.getContext('2d');

function draw(){
  // 取得當前時分秒
  var now = new Date();
  var sec  = now.getSeconds();
  var min  = now.getMinutes();
  var hour = now.getHours();
  hour >= 12 && (hour = hour - 12);

  var radius = Math.min(canvas.width / 2, canvas.height / 2);

  // 初始化畫布
  context.save();
  context.clearRect(0, 0, canvas.width, canvas.height);
  context.translate(canvas.width / 2, canvas.height / 2);
  context.rotate(-Math.PI / 2); // 讓 12 點方向朝上

  // 小時刻度
  context.save();
  context.strokeStyle = "black";
  context.lineWidth = 3;
  context.lineCap = "round";
  context.beginPath();
  for (var i = 0; i < 12; i++) {
    context.rotate(Math.PI / 6);
    context.moveTo(radius - 30, 0);
    context.lineTo(radius - 10, 0);
  }
  context.stroke();
  context.restore();

  // 分鐘刻度
  context.save();
  context.lineWidth = 2;
  context.beginPath();
  for (var i = 0; i < 60; i++) {
    if (i % 5 != 0) {
      context.moveTo(radius - 15, 0);
      context.lineTo(radius - 10, 0);
    }
    context.rotate(Math.PI / 30);
  }
  context.stroke();
  context.restore();

  // 時針
  context.save();
  context.rotate((Math.PI / 6) * hour + (Math.PI / 360) * min + (Math.PI / 21600) * sec);
  context.lineWidth = 6;
  context.beginPath();
  context.moveTo(-10, 0);
  context.lineTo(radius * 0.5, 0);
  context.stroke();
  context.restore();

  // 分針
  context.save();
  context.rotate((Math.PI / 30) * min + (Math.PI / 1800) * sec);
  context.strokeStyle = "#29A8DE";
  context.lineWidth = 4;
  context.beginPath();
  context.moveTo(-20, 0);
  context.lineTo(radius * 0.7, 0);
  context.stroke();
  context.restore();

  // 秒針
  context.save();
  context.rotate(sec * Math.PI / 30);
  context.strokeStyle = "red";
  context.lineWidth = 2;
  context.beginPath();
  context.moveTo(-30, 0);
  context.lineTo(radius * 0.9, 0);
  context.stroke();
  context.restore();

  // 外框
  context.save();
  context.lineWidth = 4;
  context.strokeStyle = "gray";
  context.beginPath();
  context.arc(0, 0, radius, 0, Math.PI * 2, true);
  context.stroke();
  context.restore();

  context.restore();
}

window.onload = function(){ setInterval(draw, 1000); };
</script>
</body>
</html>

2. 單獨看 JavaScript 部分

把繪圖邏輯抽出來看,關鍵只有幾行:

var canvas = document.getElementById("canvas");
var context = canvas.getContext('2d');

function draw(){
  var now = new Date();
  var sec = now.getSeconds(), min = now.getMinutes(), hour = now.getHours();
  hour >= 12 && (hour -= 12);

  var radius = Math.min(canvas.width / 2, canvas.height / 2);

  context.save();
  context.clearRect(0, 0, canvas.width, canvas.height);
  context.translate(canvas.width / 2, canvas.height / 2);
  context.rotate(-Math.PI / 2);

  // 時針角度 = 每小時 30° + 每分鐘 0.5° + 每秒 1/120°
  context.save();
  context.rotate((Math.PI / 6) * hour + (Math.PI / 360) * min + (Math.PI / 21600) * sec);
  context.lineWidth = 6;
  context.beginPath();
  context.moveTo(-10, 0);
  context.lineTo(radius * 0.5, 0);
  context.stroke();
  context.restore();

  // 分針角度 = 每分鐘 6° + 每秒 0.1°
  context.save();
  context.rotate((Math.PI / 30) * min + (Math.PI / 1800) * sec);
  context.lineWidth = 4;
  context.beginPath();
  context.moveTo(-20, 0);
  context.lineTo(radius * 0.7, 0);
  context.stroke();
  context.restore();

  // 秒針角度 = 每秒 6°
  context.save();
  context.rotate(sec * Math.PI / 30);
  context.strokeStyle = "red";
  context.lineWidth = 2;
  context.beginPath();
  context.moveTo(-30, 0);
  context.lineTo(radius * 0.9, 0);
  context.stroke();
  context.restore();

  context.restore();
}

window.onload = function(){ setInterval(draw, 1000); };

原理拆解

  • translate 移原點:把 (0,0) 搬到畫布中心,之後所有繪製都以中心為基準。
  • rotate 轉角度:刻度用迴圈每次轉 30°(小時)或 6°(分鐘)畫一格。
  • 時針要連續走動:時針角度除了「小時 × 30°」,還要加上分與秒的貢獻,這樣才不會每到整點就跳一格。
  • save/restore 成對使用:每次旋轉後復原狀態,避免角度累積污染後續繪製。

小提醒:範例把 rotate(-Math.PI / 2) 放在刻度之前,因此刻度是從 12 點順時針畫的;若要改為逆時針或調整起點,只要改這一行即可。另外原範例的 if(!i%5==0) 邏輯有誤(會恆為 false),已修正為 i % 5 != 0。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved