説明
Firefox 1.5以降、Safari 2以降、Opera 9以降では
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>JavaScript Sample</title>
<link rel="stylesheet" type="text/css" href="main.css" media="all">
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<h1>アナログ時計を表示する</h1>
<canvas id="clock" width="128" height="128"></canvas>
</body>
</html>
window.onload = function(){
canvasObj = document.getElementById("clock");
conObj = canvasObj.getContext("2d");
setInterval("imageClock.timeImage()", 1000);
}
var imageClock = {
timeImage : function(){
var dateObj = new Date();
var H = dateObj.getHours() % 12;
var M = dateObj.getMinutes();
var S = dateObj.getSeconds();
conObj.drawImage(imgBaseObj,0,0);
this.draw(H*30, imgHObj);
this.draw(M*6, imgMObj);
this.draw(S*6, imgSObj);
conObj.drawImage(imgCenterObj,56,56);
},
draw : function(n, img){
conObj.save();
conObj.translate(64,64);
conObj.rotate((180+n) * Math.PI / 180);
conObj.drawImage(img,0,0);
conObj.restore();
}
}
imgBaseObj = new Image(128,128);
imgBaseObj.src = "images/clock.png";
imgCenterObj = new Image(16,17);
imgCenterObj.src = "images/pin.png";
imgHObj = new Image(3,64);
imgHObj.src = "images/h.png";
imgMObj = new Image(3,64);
imgMObj.src = "images/m.png";
imgSObj = new Image(3,64);
imgSObj.src = "images/s.png";