Make Clock

By (user deleted)

Description

It same time on your phone :)

Code

Insert this code into your page

[[html]]
<!DOCTYPE html>
<html>
<body>

<canvas id="canvas" width="400" height="400"
style="background-color:#333">
</canvas>

<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var radius = canvas.height / 2;
ctx.translate(radius, radius);
radius = radius * 0.90
setInterval(drawClock, 1000);

function drawClock() {
  drawFace(ctx, radius);
  drawNumbers(ctx, radius);
  drawTime(ctx, radius);
}

function drawFace(ctx, radius) {
  var grad;
  ctx.beginPath();
  ctx.arc(0, 0, radius, 0, 2*Math.PI);
  ctx.fillStyle = 'white';
  ctx.fill();
  grad = ctx.createRadialGradient(0,0,radius*0.95, 0,0,radius*1.05);
  grad.addColorStop(0, '#333');
  grad.addColorStop(0.5, 'white');
  grad.addColorStop(1, '#333');
  ctx.strokeStyle = grad;
  ctx.lineWidth = radius*0.1;
  ctx.stroke();
  ctx.beginPath();
  ctx.arc(0, 0, radius*0.1, 0, 2*Math.PI);
  ctx.fillStyle = '#333';
  ctx.fill();
}

function drawNumbers(ctx, radius) {
  var ang;
  var num;
  ctx.font = radius*0.15 + "px arial";
  ctx.textBaseline="middle";
  ctx.textAlign="center";
  for(num = 1; num < 13; num++){
    ang = num * Math.PI / 6;
    ctx.rotate(ang);
    ctx.translate(0, -radius*0.85);
    ctx.rotate(-ang);
    ctx.fillText(num.toString(), 0, 0);
    ctx.rotate(ang);
    ctx.translate(0, radius*0.85);
    ctx.rotate(-ang);
  }
}

function drawTime(ctx, radius){
    var now = new Date();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    //hour
    hour=hour%12;
    hour=(hour*Math.PI/6)+
    (minute*Math.PI/(6*60))+
    (second*Math.PI/(360*60));
    drawHand(ctx, hour, radius*0.5, radius*0.07);
    //minute
    minute=(minute*Math.PI/30)+(second*Math.PI/(30*60));
    drawHand(ctx, minute, radius*0.8, radius*0.07);
    // second
    second=(second*Math.PI/30);
    drawHand(ctx, second, radius*0.9, radius*0.02);
}

function drawHand(ctx, pos, length, width) {
    ctx.beginPath();
    ctx.lineWidth = width;
    ctx.lineCap = "round";
    ctx.moveTo(0,0);
    ctx.rotate(pos);
    ctx.lineTo(0, -length);
    ctx.stroke();
    ctx.rotate(-pos);
}
</script>

</body>
</html>
[[/html]]

In action

After inserting


Thanks to tsangk for this great snippet: conditional-blocks


text above inserted with:

[[include :snippets:if START |unique=1|type=equal|var1=%%name%%|var2=conditional-blocks]]
**##red|Thanks to tsangk for this great snippet:##** [[[code:conditional-blocks]]]
[[include :snippets:if END]]



Other snippets posted by (user deleted)

Chatbox - 26 Mar 2012 11:07
Google +1 Button - 19 Mar 2012 19:40
Per Category CSS Module - 06 Mar 2012 03:38
Advanced Search - 25 Feb 2012 12:29
This Site Last Updated... - 16 Feb 2012 02:37
Zoho Writer - 16 Feb 2012 01:57
Zoho Sheet - 16 Feb 2012 01:56
Zoho Show - 16 Feb 2012 01:55
Zoho Polls - 16 Feb 2012 01:53
Yahoo! WebPlayer - 16 Feb 2012 01:53
WoW Tooltips - 16 Feb 2012 01:52
Wishlist - 16 Feb 2012 01:52
Widgetbox Panel - 16 Feb 2012 01:52
Welcome - 16 Feb 2012 01:52
Voicethread - 16 Feb 2012 01:51
Vimeo - 16 Feb 2012 01:51
Video Quiz - 16 Feb 2012 01:51
Video Player - 16 Feb 2012 01:50
Video from Revver - 16 Feb 2012 01:50
Using Divs - 16 Feb 2012 01:50
Tweet Button - 16 Feb 2012 01:49
Top Nav Menu with ListPages - 16 Feb 2012 01:48
TOC's from many pages - 16 Feb 2012 01:47
Thumbnail page list using ListPages - 16 Feb 2012 01:47
Tags as variables - 16 Feb 2012 01:46
Tabs - 16 Feb 2012 01:45
Table Alterations - 16 Feb 2012 01:45
Syntax Highlighter - 16 Feb 2012 01:45
Standalone Page Buttons - 16 Feb 2012 01:44
Sproutbuilder - 16 Feb 2012 01:44
Social Bookmarking - 16 Feb 2012 01:42
Slideshare - 16 Feb 2012 01:42
Slideboom - 16 Feb 2012 01:42
Slideaware - 16 Feb 2012 01:41
Simple Gallery - 16 Feb 2012 01:41
Show A YouTube Video Saved On Your Page - 16 Feb 2012 01:41
Shoutbox.Us - 16 Feb 2012 01:40
Search and tags in the same page! - 16 Feb 2012 01:40
Schooltube - 16 Feb 2012 01:40
Schedules - 16 Feb 2012 01:40
Remove Duplications - 16 Feb 2012 01:39
Remember The Milk Todo - 16 Feb 2012 01:39
RegExp Date Wizard - 16 Feb 2012 01:39
Reflections - 16 Feb 2012 01:39
Referencing to a Common Reference Page - 16 Feb 2012 01:38
Redirect: To - 16 Feb 2012 01:37
Recent Changes (mini) - 16 Feb 2012 01:36
Rating Summary - 16 Feb 2012 01:35
"Random Page!" link - 16 Feb 2012 01:35
Use any html code on a page - 16 Feb 2012 01:34
Quoted Blockquotes - 16 Feb 2012 01:33
Quimble Poll - 16 Feb 2012 01:33
Quikmaps Maps - 16 Feb 2012 01:33
Previous Next - 16 Feb 2012 01:32
PollDaddy - 16 Feb 2012 01:32
Poll - 16 Feb 2012 01:31
Playlist - 16 Feb 2012 01:31
Pictures with the text under it - 16 Feb 2012 01:31
Picasaweb - 16 Feb 2012 01:30
picasa flash - 16 Feb 2012 01:30
Photobucket Widget - 16 Feb 2012 01:29
Page Previews (Snap.com) - 16 Feb 2012 01:29
Page Collections and Sequence Boxes - 16 Feb 2012 01:28
Pabpixies Gadgets - 16 Feb 2012 01:28
Os Detection - 16 Feb 2012 01:27
Ohloh - 16 Feb 2012 01:27
NewPage Button - 16 Feb 2012 01:26
Newline in lists - 16 Feb 2012 01:26
Module Listpages Tags - 16 Feb 2012 01:24
Module Feed Drilldown: Details Page - 16 Feb 2012 01:24
Module Feed Drilldown - 16 Feb 2012 01:24
Module Feed: Collapsible - 16 Feb 2012 01:23
Module Categories - 16 Feb 2012 01:23
MiniRecentPosts With Limit - 16 Feb 2012 01:23
Minialist theme - 16 Feb 2012 01:22
Mimic Colspan With Multiple Tables - 16 Feb 2012 01:21
Mibbit - 16 Feb 2012 01:21
Meebo me - 16 Feb 2012 01:20
Making default templates work - 16 Feb 2012 01:20
ListPages: Horizontal? - 16 Feb 2012 01:20
Layout With Tables - 16 Feb 2012 01:19
LaTeX Table - 16 Feb 2012 01:19
Js Kit Ratings - 16 Feb 2012 01:15
Jotform - 16 Feb 2012 01:14
JavaScript Addition - 16 Feb 2012 01:14
Java Applets - 16 Feb 2012 01:13
jamendo widgets - 16 Feb 2012 01:13
Invisible edit-sections button with fixed screen position - 16 Feb 2012 01:12
Instacalc - 16 Feb 2012 01:12
Indent - 16 Feb 2012 01:12
Import the Digg Feed - 16 Feb 2012 01:11
Import a News Feed - 16 Feb 2012 01:10
iframe embed - 16 Feb 2012 01:08
Google YouTube Videobar - 16 Feb 2012 01:06
Google Translate - 16 Feb 2012 01:06
Google Maps - 16 Feb 2012 01:05
Google Gadgets - 16 Feb 2012 01:05
Google Document - 16 Feb 2012 01:04
Google Analytics - 16 Feb 2012 01:04


Rate this solution

If you think this solution is useful — rate it up!

rating: +8+x
Add a New Comment
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License