DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> 關於HTML
用 HTML5 Canvas 做時鐘:三角函數與指針繪製實作
編輯:關於HTML     

這個範例用 Canvas 畫一個即時走動的時鐘,同時示範了如何把時間換算成角度、用三角函數決定指針終點座標,以及定期重繪的做法。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Canvas 時鐘</title>
<script>
function draw(){
  setInterval(Composite, 1000);
}

function Composite(){
  var canvas = document.getElementById("canvas");
  if (canvas == null) return false;
  var context = canvas.getContext("2d");
  context.clearRect(0, 0, canvas.width, canvas.height);
  context.save();

  var myDate = new Date();
  var hour = myDate.getHours() >= 12 ? myDate.getHours() - 12 : myDate.getHours();
  var minute = myDate.getMinutes();
  var second = myDate.getSeconds();
  var year = myDate.getFullYear();          // 完整年份(四位數)
  var month = myDate.getMonth() + 1 < 10 ? "0" + (myDate.getMonth() + 1) : myDate.getMonth() + 1;
  var date = myDate.getDate() < 10 ? "0" + myDate.getDate() : myDate.getDate();
  var week = myDate.getDay();               // 0-6,0 代表週日

  switch (week) {
    case 0: week = "星期日"; break;
    case 1: week = "星期一"; break;
    case 2: week = "星期二"; break;
    case 3: week = "星期三"; break;
    case 4: week = "星期四"; break;
    case 5: week = "星期五"; break;
    case 6: week = "星期六"; break;
  }

  var hour1 = myDate.getHours() < 10 ? "0" + myDate.getHours() : myDate.getHours();
  var minute1 = myDate.getMinutes() < 10 ? "0" + myDate.getMinutes() : myDate.getMinutes();
  var second1 = myDate.getSeconds() < 10 ? "0" + myDate.getSeconds() : myDate.getSeconds();

  var s = year + "年" + month + "月" + date + "日 " + week + hour1 + ":" + minute1 + ":" + second1;
  document.getElementById("tt").innerHTML = s;

  // 錶盤
  context.beginPath();
  context.fillStyle = "lightblue";
  context.arc(150, 150, 100, 0, Math.PI * 2, false);
  context.fill();
  context.closePath();

  // 秒針
  context.beginPath();
  context.strokeStyle = "red";
  context.moveTo(150, 150);
  var secondhu = second / 60 * 2 * Math.PI;
  context.lineTo(Math.sin(secondhu) * 90 + 150, -Math.cos(secondhu) * 90 + 150);
  context.lineWidth = "1";
  context.closePath();
  context.stroke();

  // 分針
  context.beginPath();
  context.strokeStyle = "blue";
  context.moveTo(150, 150);
  var minutehu = (minute + second / 60) / 60 * 2 * Math.PI;
  context.lineTo(Math.sin(minutehu) * 70 + 150, -Math.cos(minutehu) * 70 + 150);
  context.lineWidth = "3";
  context.closePath();
  context.stroke();

  // 時針
  context.beginPath();
  context.strokeStyle = "black";
  context.moveTo(150, 150);
  var hourhu = (hour + minute / 60) / 12 * 2 * Math.PI;
  context.lineTo(Math.sin(hourhu) * 60 + 150, -Math.cos(hourhu) * 60 + 150);
  context.lineWidth = "5";
  context.closePath();
  context.stroke();

  // 數字
  context.font = "bold 20px sans-serif";
  context.fillStyle = "black";
  for (var i = 1; i <= 12; i++) {
    var txthu = i / 12 * 2 * Math.PI;
    context.fillText(i, Math.sin(txthu) * 85 + 141, -Math.cos(txthu) * 85 + 155);
  }

  context.restore();
}
</script>
</head>
<body onLoad="draw()">
  

canvas 元素示例

幾個值得留意的細節

每畫一支指針前都先呼叫 beginPath(),這一步不能省——否則路徑會累積,先前的線段會被重複描邊。

角度換算的公式是「比例 × 2π」:秒針是 second/60,分針要把秒換算進來成為 (minute + second/60)/60,時針則是 (hour + minute/60)/12。少了後面的進位項,指針就會每分鐘跳一次而不是平滑走動。

座標使用 sin 配 x、負 cos 配 y,是因為 Canvas 的 y 軸向下,而 0 點鐘方向朝上,負號負責把方向翻正。最後加上 150 這個圓心偏移量,把結果移到畫布中央。

畫面之所以每秒更新,靠的是 setInterval(Composite, 1000);每次重繪前先 clearRect 清空整塊畫布,避免舊指針殘留。實務上若想更順,可以改用 requestAnimationFrame 並搭配 CSS 旋轉,效能會更好。

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