<div class="container">
<header>
<h1>Tick tock.</h1>
</header>
<div class="rowElem">
<button class="myBtn" onclick="analogTime()">Show Clock</button>
</div>
</div>
<div class="container" id="result">
<h2 id="canvas-head">Digital</h2>
<div id="digital"></div>
<h2 id="canvas-head">Analog</h2>
<canvas id="analog" width="310" height="310">Sorry, your browser does not support the most awesome HTML5 element, canvas. Have you considered <a href="http://google.com/chrome">updating</a>?</canvas>
</div>
function drawClock(){
var d = new Date();
var hours = d.getHours() % 12;
var min = d.getMinutes();
var seconds = d.getSeconds();
var minutes = min + hours*60;
var angle = minutes/2;
var minangle = min*6;
var secangle = seconds*6;
//set canvas variables
var canvas = document.getElementById('analog'),
ctx = canvas.getContext('2d');
canvas.width = canvas.width;
//draw circle for clock outline
ctx.beginPath();
ctx.arc(155, 155, 150, 0, 2 * Math.PI);
ctx.lineWidth = 1;
ctx.strokeStyle = '#000';
ctx.stroke();
ctx.translate(0, 0);
ctx.rotate(0);
ctx.save();
ctx.save();
ctx.save();
//draw and rotate hours arm
ctx.translate(155, 155);
ctx.rotate(angle*Math.PI/180);
ctx.beginPath();
ctx.moveTo(0, 0)
ctx.lineTo(0, -100);
ctx.lineTo(0, 20);
ctx.lineWidth = 3;
ctx.stroke();
//draw and rotate seconds arm
ctx.restore();
ctx.translate(155,155);
ctx.rotate(secangle*Math.PI/180);
ctx.beginPath();
ctx.moveTo(0, 0)
ctx.lineTo(0, -130);
ctx.lineTo(0, 20);
ctx.lineWidth = 1;
ctx.stroke();
//draw and rotate minutes arm
ctx.restore();
ctx.translate(155,155);
ctx.rotate(minangle*Math.PI/180);
ctx.beginPath();
ctx.moveTo(0, 0)
ctx.lineTo(0, -130);
ctx.lineTo(0, 20);
ctx.lineWidth = 2;
ctx.stroke();
//add numbers to clock
ctx.restore();
ctx.translate(155,155)
ctx.font = "30px Arial";
ctx.fillText("12",-20,-120);
ctx.fillText("3", 125, 12);
ctx.fillText("6", -8, 140);
ctx.fillText("9", -140, 12);
//display digital clock above analog clock
//check if hours = 0 to return 12
if(hours == 0){
hours = 12;
}
if(min < 10){//if min less than 10 add a zero in front of min.
min = '0' + min;
}
if(seconds < 10){//if min less than 10 add a zero in front of min.
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.