**基本思路:**
首先繪制一個矩形背景,設置顏色為灰色。在背景上繪制一個簡單的矩形外邊框,然后再繪制一個內邊框,接著加載選定的圖像做為電子鐘內部的背景圖片。然后開始繪制時鐘刻度,繪制分鐘刻度,最后獲取當前系統時間,繪制時分秒三個手柄。
**技術要點:**
使用HTML5的Canvas 2D繪制對象,主要使用context.save()與context.restore()方法來保存繪制狀態和重置繪制狀態,使用Transform和fillRect()方法來繪制時鐘和分鐘刻度。使用drawImage()方法來繪制背景圖片,使用setTimeout()方法來刷新時間顯示。
**代碼詳解:**
**獲取HTML5 Canvas繪制對象的代碼如下:**
**
~~~
var canvas = document.getElementById("canvas1");
ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, 500, 500);
~~~
**繪制時鐘刻度的代碼如下:**
~~~
var sin = Math.sin(Math.PI/6);
var cos = Math.cos(Math.PI/6);
ctx.translate(245, 245);
for (var i=0; i <= 12; i++) {
// top
ctx.fillRect(160,-7.5,30,10);
ctx.strokeRect(160,-7.5,30,10);
ctx.transform(cos, sin, -sin, cos, 0, 0);
}
~~~
**繪制分鐘分鐘刻度的代碼如下:**
~~~
var sin = Math.sin(Math.PI/30);
var cos = Math.cos(Math.PI/30);
for (var i=0; i <= 60; i++) {
ctx.fillRect(170,-5,10,2);
ctx.transform(cos, sin, -sin, cos, 0, 0);
}
~~~
**保存制狀態代碼如下:**
~~~
ctx.translate(245, 245);
ctx.save();
~~~
**恢復繪制狀態代碼如下:**
~~~
ctx.restore();
~~~
**運行效果如下:**

**程序完全源代碼如下:**
~~~
<html>
<head>
<script>
window.onload = function() {
clockHand();
};
function clockHand() {
var canvas = document.getElementById("canvas1");
ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, 500, 500);
// create background rectangle
// ctx.lineWidth = 10;
ctx.fillStyle = "gray";
ctx.fillRect(0,0,500,500);
// draw frame
ctx.lineWidth = 10;
ctx.strokeStyle = "green";
ctx.strokeRect(0,0,500,500);
// draw author infomation
ctx.fillStyle = "blue";
ctx.font = "20px Times New Roman";
ctx.fillText("-created by gloomyfish", 150, 30);
// draw inner rectangle
ctx.lineWidth = 10;
ctx.strokeStyle = "black";
ctx.strokeRect(45,45,400,400);
// create background image
var img=new Image();
img.src="background.png";
img.onload = function() {
ctx.drawImage(img,45,45,400,400);
ctx.save();
// draw marker unit
ctx.lineWidth = 2;
ctx.fillStyle = "purple";
ctx.strokeStyle = "black";
var sin = Math.sin(Math.PI/6);
var cos = Math.cos(Math.PI/6);
ctx.translate(245, 245);
for (var i=0; i <= 12; i++) {
// top
ctx.fillRect(160,-7.5,30,10);
ctx.strokeRect(160,-7.5,30,10);
ctx.transform(cos, sin, -sin, cos, 0, 0);
}
// transform back center point
// ctx.translate(245, 245);
var sin = Math.sin(Math.PI/30);
var cos = Math.cos(Math.PI/30);
for (var i=0; i <= 60; i++) {
ctx.fillRect(170,-5,10,2);
ctx.transform(cos, sin, -sin, cos, 0, 0);
}
ctx.restore();
// top
ctx.fillText("12", 233,100);
// bottom
ctx.fillText("6", 240,400);
// left
ctx.fillText("9", 90,252);
// right
ctx.fillText("3", 395,250);
// get time
ctx.save();
ctx.translate(245, 245);
ctx.save();
// dynamic show time
var now=new Date();
var hrs=now.getHours();
var min=now.getMinutes();
var sec=now.getSeconds();
//Draw hour hand
ctx.rotate(Math.PI/6*(hrs+(min/60)+(sec/3600)));
ctx.beginPath();
ctx.moveTo(0,10);
ctx.lineTo(0,-60);
ctx.stroke();
ctx.restore();
ctx.save();
//Draw minute hand
ctx.rotate(Math.PI/30*(min+(sec/60)));
ctx.beginPath();
ctx.moveTo(0,20);
ctx.lineTo(0,-110);
ctx.stroke();
ctx.restore();
ctx.save();
//Draw second hand
ctx.rotate(Math.PI/30*sec);
ctx.strokeStyle="#E33";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(0,20);
ctx.lineTo(0,-110);
ctx.stroke();
ctx.restore();
// finally store to originall point
ctx.restore();
setTimeout(clockHand,1000);
};
}
</script>
</head>
<body bgcolor="#E6E6FA">
<canvas id="canvas1" width="500" height="500">electronic clock</canvas>
</body>
</html>
~~~
**不足之處:**
每次都刷新加載image對象不怎么好,我是在google瀏覽器中測試的,建議在google瀏覽器中運行上面代碼。
- 前言
- HTML5 組件Canvas實現圖像灰度化
- HTML5 Canvas組件繪制太極圖案
- HTML5 組件Canvas實現電子鐘
- 將HTML5 Canvas的內容保存為圖片
- 純JavaScript實現HTML5 Canvas六種特效濾鏡
- 提高HTML5 Canvas性能的技巧
- HTML5 Canvas平移,放縮,旋轉演示
- HTML5 Canvas鼠標與鍵盤事件
- HTML5 Canvas漸進填充與透明
- HTML5 Canvas動畫效果演示
- HTML5 Canvas 填充與描邊(Fill And Stroke)
- HTML5 Canvas陰影用法演示
- HTML5 Canvas自定義圓角矩形與虛線(Rounded Rectangle and Dash Line)
- HTML5 Canvas中實現繪制一個像素寬的細線