timgolding
09-13-2012, 11:48 AM
What to do when you are bored...
Make a clock using Java script and HTML5's canvas.
<!DOCTYPE HTML>
<html>
<head>
<title>JS / Canvas clock</title>
<style type="text/css">
#canvas
{
border:1px solid black;
}
</style>
</head>
<body onload="init();">
<h1>JS clock using canvas</h1>
<canvas id="canvas" width="420" height="420"">
This text is displayed if your browser does not support HTML5 Canvas.
</canvas>
<script type="text/javascript">
function init() {
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
intertimer = setInterval(draw, 1);
}
function draw()
{
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
//draw base clock
ctx.lineWidth = 2;
ctx.strokeStyle = "black";
ctx.fillStyle = "silver";
ctx.beginPath();
ctx.arc(210, 210, 200, 0, (2 * Math.PI), false);
ctx.fill();
ctx.stroke();
ctx.closePath();
//draw 5 minutes marks
var segment = ((2*Math.PI)/12);
ctx.lineWidth = 4;
for(i = 0 ; i<12 ;i++)
{
ctx.beginPath();
ctx.moveTo(210+(Math.sin(segment*i)*180), 210-(Math.cos(segment*i)*180));
ctx.lineTo(210+(Math.sin(segment*i)*190), 210-(Math.cos(segment*i)*190));
ctx.stroke();
ctx.closePath();
}
var min_segs = ((2*Math.PI)/60);
var hour_segs = ((2*Math.PI)/12);
//draw hands
ctx.lineWidth = 6;
ctx.strokeStyle = "red";
ctx.beginPath();
ctx.moveTo(210,210);
ctx.lineTo(210+(Math.sin(hour_segs*(hours+(minutes/60)))*150), 210-(Math.cos(hour_segs*(hours+(minutes/60)))*150));
ctx.stroke();
ctx.closePath();
ctx.lineWidth = 4;
ctx.strokeStyle = "green";
ctx.beginPath();
ctx.moveTo(210,210);
ctx.lineTo(210+(Math.sin(min_segs*(minutes+(seconds/60)))*180), 210-(Math.cos(min_segs*(minutes+(seconds/60)))*180));
ctx.stroke();
ctx.closePath();
ctx.lineWidth = 2;
ctx.strokeStyle = "blue";
ctx.beginPath();
ctx.moveTo(210,210);
ctx.lineTo(210+(Math.sin(min_segs*seconds)*190), 210-(Math.cos(min_segs*seconds)*190));
ctx.stroke();
ctx.closePath();
}
</script>
</body>
</html>
Make a clock using Java script and HTML5's canvas.
<!DOCTYPE HTML>
<html>
<head>
<title>JS / Canvas clock</title>
<style type="text/css">
#canvas
{
border:1px solid black;
}
</style>
</head>
<body onload="init();">
<h1>JS clock using canvas</h1>
<canvas id="canvas" width="420" height="420"">
This text is displayed if your browser does not support HTML5 Canvas.
</canvas>
<script type="text/javascript">
function init() {
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
intertimer = setInterval(draw, 1);
}
function draw()
{
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
//draw base clock
ctx.lineWidth = 2;
ctx.strokeStyle = "black";
ctx.fillStyle = "silver";
ctx.beginPath();
ctx.arc(210, 210, 200, 0, (2 * Math.PI), false);
ctx.fill();
ctx.stroke();
ctx.closePath();
//draw 5 minutes marks
var segment = ((2*Math.PI)/12);
ctx.lineWidth = 4;
for(i = 0 ; i<12 ;i++)
{
ctx.beginPath();
ctx.moveTo(210+(Math.sin(segment*i)*180), 210-(Math.cos(segment*i)*180));
ctx.lineTo(210+(Math.sin(segment*i)*190), 210-(Math.cos(segment*i)*190));
ctx.stroke();
ctx.closePath();
}
var min_segs = ((2*Math.PI)/60);
var hour_segs = ((2*Math.PI)/12);
//draw hands
ctx.lineWidth = 6;
ctx.strokeStyle = "red";
ctx.beginPath();
ctx.moveTo(210,210);
ctx.lineTo(210+(Math.sin(hour_segs*(hours+(minutes/60)))*150), 210-(Math.cos(hour_segs*(hours+(minutes/60)))*150));
ctx.stroke();
ctx.closePath();
ctx.lineWidth = 4;
ctx.strokeStyle = "green";
ctx.beginPath();
ctx.moveTo(210,210);
ctx.lineTo(210+(Math.sin(min_segs*(minutes+(seconds/60)))*180), 210-(Math.cos(min_segs*(minutes+(seconds/60)))*180));
ctx.stroke();
ctx.closePath();
ctx.lineWidth = 2;
ctx.strokeStyle = "blue";
ctx.beginPath();
ctx.moveTo(210,210);
ctx.lineTo(210+(Math.sin(min_segs*seconds)*190), 210-(Math.cos(min_segs*seconds)*190));
ctx.stroke();
ctx.closePath();
}
</script>
</body>
</html>