In this article, we are going to understand how to display a running clock using HTML 5. In this, we will show the polar clock and the date-time from your system clock in the browser.
Here we will use some JavaScript and some styles along with the HTML code. Just go through the steps to see how to create this application.
Let's see how the ClockHTML5 application can be created. To do so go through the following steps.
Step 1 : Open a HTML editor or Visual Studio.
Open File menu ->select new ->Choose Website then.
This is where we will create an HTML5 application.
- Go to Solution Explorer
- Right-click on the Application name
- Select Add-->add new item
- Now in the window that opens, select an HTML page or a new Webform
- Rename it to clockHtml5.aspx
Step 2: In this section, we will create the style for the media and create the .css on the media screen. Put the given script in the Head section of the HTML or in between the <head>--</head> tags.
Here two CSS files are used for design purposes; one is for the section div, named d1.css and the other d2.css for the whole body of the web page.
d1.css scripting
- body
- {
- background: #C1FFC1;
- color: #fff;
- font: 300 100.1% "Helvetica Neue" , Helvetica, "Arial Unicode MS" , Arial, sans-serif;
- }
- #holder
- {
- height: 442px;
- left: 42%;
- margin: -240px 0 0 -320px;
- position: absolute;
- top: 57%;
- width: 842px;
- font-size: x-large;
- background-color: #008000;
- }
- #duplicate {
- bottom: 0;
- font: 300 .7em "Helvetica Neue", Helvetica, ;
- position: absolute;
- right: 1em;
- text-align: right;
- }
- #duplicate d {
- color: #fff;
- }
d2.css scripting
- body {
- background: #fff;
- color: #000;
- font: 100.1% "Comic Sans MS", Lucida, Verdana, sans-serif;
- }
- #holder {
- height: 480px;
- left: 50%;
- margin: 0 0 0 -320px;
- position: absolute;
- top: 0;
- width: 640px;
- }
- #duplicate {
- bottom: 0;
- font-size: .7em;
- position: absolute;
- right: 1em;
- text-align: right;
- }
Step 3 : In this part we need to work on some JavaScript. For fully understanding how the JavaScript works download the attached .rar file and run the ClockHTML5 application.
The whole JavaScript looks as in the following.
- window.onload = function ()
- {
- var dee = Color("holder", 600, 600),
- Rid = 200,
- init = true,
- param = { stroke: "#fff", "stroke-width": 30 },
- hash = document.location.hash,
- marksAttr = { fill: hash || "#444", stroke: "none" },
- html = [
- document.getElementById("h"),
- document.getElementById("m"),
- document.getElementById("s"),
- document.getElementById("d"),
- document.getElementById("mnth"),
- document.getElementById("ampm")
- ];
-
- dee.customAttributes.arc = function (value, total, Rid)
- {
- var beta = 360 / total * value,
- p = (90 - alpha) * Math.PI / 180,
- x = 300 + Rid * Math.cos(a),
- y = 300 - Rid * Math.sin(a),
- color = "hsb(".concat(Math.round(Rid) / 200, ",", value / total, ", .75)"),
- way;
- if (total == value)
- {
- way = [["M", 300, 300 - Rid], ["A", Rid, Rid, 0, 1, 1, 299.99, 300 - Rid]];
- }
- else
- {
- way = [["M", 300, 300 - Rid], ["A", Rid, Rid, 0, +(alpha > 180), 1, x, y]];
- }
- return { way: way, stroke: color };
- };
- function drawMarks(Rid, total)
- {
- if (total == 31)
- {
- var dt = new Date;
- dt.setDate(1);
- dt.setMonth(dt.getMonth() + 1);
- dt.setDate(-1);
- total = dt.getDate();
- }
- var color = "hsb(".concat(Math.round(Rid) / 200, ", 1, .75)"),
- out = dee.set();
- for (var value = 0; value < total; value++)
- {
- var alpha = 360 / total * value,
- p = (90 - alpha) * Math.PI / 180,
- x = 300 + Rid * Math.cos(a),
- y = 300 - Rid * Math.sin(a);
- out.push(r.circle(x, y, 2).attr(marksAttr));
- }
- return out;
- }
- (function ()
- {
- var dt = new Date,
- am = (dt.getHours() < 12),
- h = dt.getHours() % 12 || 12;
- updateVal(dt.getSeconds(), 60, 200, sec, 2);
- updateVal(dt.getMinutes(), 60, 160, min, 1);
- updateVal(h, 12, 120, hor, 0);
- updateVal(dt.getDate(), 31, 80, day, 3);
- updateVal(dt.getMonth() + 1, 12, 40, mon, 4);
- pm[(am ? "hide" : "show")]();
- html[5].innerHTML = am ? "AM" : "PM";
- setTimeout(arguments.callee, 1000);
- init = false;
- })();
- };
Step 4: In this section, we are going to become familiar with the body part of HTML scripting. Replace this script from the body section of the clockHtml5.aspx page. Here we pass a #holder in the div id that is defined in the d1.css file.
- <body>
- <div id="holder"></div>
- <div id="time">
- <span id="h"></span>:<span id="m"></span>:<span id="s"></span> <span id="ampm"></span> Date <span id="d"></span>/<span id="mnth"></span>
- </div>
- </body>
Step 5 : The complete code for the ClockHTML5 application:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="clockHtml5.aspx.cs" Inherits="Reflectionimage._Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <meta charset="utf-8">
- <title></title>
- <link rel="stylesheet" href="d1.css" media="screen">
- <link rel="stylesheet" href="d2.css" media="print">
- <script src="javascript.js"></script>
- <script type="text/javascript">
- From Step 3 JavaScript copy from there and paste it here.
- </script
- <style media="screen">
- #holder {
- height: 600px;
- margin: -300px 0 0 -300px;
- width: 600px;
- }
- #time {
- text-align: center;
- font: 80 3em "Helvetica Neue", Helvetica, Arial, sans-serif;
- }
- </style>
- </head>
- <body>
- <div id="holder"></div>
- <div id="time">
- <span id="h"></span>:<span id="m"></span>:<span id="s"></span> <span id="ampm"></span> Date <span id="d"></span>/<span id="mnth"></span>
- </div>
- </body>
- </html>
Step 6 : Output Press F5
Note: For the accurate output of HTML5 applications, you must have the Google Chrome browser on your PC. You will see here the polar clock and the date and time of your system in the browser.
Time is running continuously, you can watch in the given figure.
Here are some useful resources