この関数は、クロック秒をアークで使用できるラジアンに変換します。
// zero seconds (the 12 oclock position)
var startingAngle = secondsToRadians(0);
// get the current seconds and its associated radian angle
var currentSeconds=new Date().getSeconds();
var endingAngle = secondsToRadians(currentSeconds);
function secondsToRadians(seconds) {
var degrees=(seconds-15)*6;
var radians=(degrees * Math.PI)/180;
return(radians);
}
ここにコードとフィドルがあります: http://jsfiddle.net/m1erickson/BR6TY/
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
body{ background-color: ivory; }
canvas{border:1px solid red;}
</style>
<script>
$(function(){
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var centerX = Math.floor(canvas.width / 2);
var centerY = Math.floor(canvas.height / 2);
var radius = Math.floor(canvas.width / 3);
var startingAngle = secondsToRadians(0);
ctx.fillStyle = "#819FF0";
ctx.strokeStyle="black";
ctx.lineWidth=3;
ctx.font="24px Verdana";
setInterval(function() {
// draw the arc about every second
// set the arc to reflect the actual clock seconds
drawClockWedge( new Date().getSeconds() );
}, 1000);
function drawClockWedge(seconds){
// clear the canvas
ctx.save();
ctx.clearRect(0,0,canvas.width,canvas.height);
// calculate the seconds (in actual time)
// and convert to radians that .arc requires
var endingAngle = secondsToRadians(seconds);
// draw a closed arc representing elapsed seconds
ctx.beginPath();
ctx.moveTo(centerX, centerY);
ctx.arc(centerX, centerY, radius, startingAngle, endingAngle, false);
ctx.closePath();
ctx.fill();
ctx.stroke();
// also display the seconds on top
ctx.fillText("["+seconds+"]",centerX-25,centerY-radius-20);
ctx.restore();
}
function secondsToRadians(seconds) {
var degrees=(seconds-15)*6;
var radians=(degrees * Math.PI)/180;
return(radians);
}
}); // end $(function(){});
</script>
</head>
<body>
<canvas id="canvas" width=300 height=300></canvas>
</body>
</html>