473,915 Members | 5,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Analog Clock

Frinavale
9,735 Recognized Expert Moderator Expert
This whole thing started when I wanted to display list items in a circle...it just looked way too much like a clock. So for fun I started to create a JavaScript Analog clock.

I was wondering how to draw a line using JavaScript. Everything I've found so far seems to use some JavaScript library....is there an easier way to do this?

Here's what I have so far. I know the clock is Side Ways....but when I try to change the angle so that 12 is at the top (by setting it to -90 or 270 since it's in the 3 o'clock position) it isn't right. Maybe my math is wrong or something?

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4.  
  5. <head>
  6.  <style type="text/css">
  7.   li{
  8.    position:absolute;
  9.    list-style:none;
  10.    text-align:center;
  11.    color:#CCCCCC;  
  12.   }
  13.  </style>
  14. </head>
  15. <body>
  16.  
  17.  <ul>
  18.   <li>12</li>
  19.   <li> . </li>
  20.   <li> . </li>
  21.   <li> . </li>
  22.   <li> . </li>
  23.   <li>1 </li>
  24.   <li> . </li>
  25.   <li> . </li>
  26.   <li> . </li>
  27.   <li> . </li>
  28.   <li>2 </li>
  29.   <li> . </li>
  30.   <li> . </li>
  31.   <li> . </li>
  32.   <li> . </li>
  33.   <li>3 </li>
  34.   <li> . </li>
  35.   <li> . </li>
  36.   <li> . </li>
  37.   <li> . </li>
  38.   <li>4 </li>
  39.   <li> . </li>
  40.   <li> . </li>
  41.   <li> . </li>
  42.   <li> . </li>
  43.   <li>5 </li>
  44.   <li> . </li>
  45.   <li> . </li>
  46.   <li> . </li>
  47.   <li> . </li>
  48.   <li>6 </li>
  49.   <li> . </li>
  50.   <li> . </li>
  51.   <li> . </li>
  52.   <li> . </li>
  53.   <li>7 </li>
  54.   <li> . </li>
  55.   <li> . </li>
  56.   <li> . </li>
  57.   <li> . </li>
  58.   <li>8 </li>
  59.   <li> . </li>
  60.   <li> . </li>
  61.   <li> . </li>
  62.   <li> . </li>
  63.   <li>9 </li>
  64.   <li> . </li>
  65.   <li> . </li>
  66.   <li> . </li>
  67.   <li> . </li>
  68.   <li>10</li>
  69.   <li> . </li>
  70.   <li> . </li>
  71.   <li> . </li>
  72.   <li> . </li>
  73.   <li>11</li>
  74.   <li> . </li>
  75.   <li> . </li>
  76.   <li> . </li>
  77.   <li> . </li>
  78.  </ul>
  79.  <div id="time" style="position:absolute; top:280px;"></div>
  80.  <script type="text/javascript">
  81.  
  82.  drawClock();
  83.  drawTime();
  84.  
  85.  
  86.  function drawTime(){
  87.   var listElements = document.getElementsByTagName("li");
  88.   for(var i=0; i<listElements.length; i++){
  89.    listElements[i].style.color="#CCCCCC";      
  90.    listElements[i].style.fontWeight="normal";
  91.    listElements[i].style.border="none";
  92.   }
  93.   var d = new Date();
  94.   var second = d.getSeconds();
  95.   var hour = d.getHours();
  96.   var minute = d.getMinutes();
  97.  
  98.   document.getElementById("time").innerHTML=hour+":"+minute+":"+second;
  99.   if(hour>12){hour -= 12;}
  100.   hour=hour*5;
  101.  
  102.   listElements[hour].style.color="blue";
  103.   listElements[hour].style.fontWeight="bold";
  104.   listElements[hour].style.border="solid 1px blue";
  105.   listElements[minute].style.color="green";
  106.   listElements[minute].style.fontWeight="bold";
  107.   listElements[minute].style.border="solid 1px green";
  108.   listElements[second].style.color="orange";
  109.   listElements[second].style.border="solid 1px orange";
  110.  
  111.  
  112.   setTimeout(drawTime,1000);
  113.  }
  114.  
  115.  function drawClock(){
  116.   var listElements = document.getElementsByTagName("li");
  117.   var step = (2*Math.PI)/listElements.length;
  118.   var angle=0;
  119.   //angle=180.60;//<--sets 12 to the 12 o'clock position ?? not sure why
  120.   var circleCenterX = 120;
  121.   var circleCenterY = 120;
  122.   var radius = 120;
  123.   for(var i = 0; i<listElements.length; i++)
  124.   { 
  125.     var element = listElements[i];
  126.     var left=Math.round(circleCenterX+radius*Math.cos(angle));
  127.     var top=Math.round(circleCenterY+radius*Math.sin(angle));
  128.     element.style.left = left+"px";
  129.     element.style.top = top+"px";
  130.     angle+=step;   
  131.   }
  132.  }
  133.  
  134.  </script>
  135. </body>
  136. </html>
Thanks!

-Frinny
Dec 16 '09 #1
7 5034
Dormilich
8,658 Recognized Expert Moderator Expert
but when I try to change the angle so that 12 is at the top (by setting it to -90 or 270 since it's in the 3 o'clock position) it isn't right.
well, it works for me. (OK, if I use it in rad, aka 270° = 1.5*Math.PI)
Dec 16 '09 #2
Frinavale
9,735 Recognized Expert Moderator Expert
Thanks Dorm. Using 1.5*Math.PI properly set 12 at the 12 o'clock position.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4.  
  5. <head>
  6.  <style type="text/css">
  7.   li{
  8.    position:absolute;
  9.    list-style:none;
  10.    text-align:center;
  11.    color:#CCCCCC;  
  12.   }
  13.  </style>
  14. </head>
  15. <body>
  16.  
  17.  <ul>
  18.   <li>12</li>
  19.   <li> . </li>
  20.   <li> . </li>
  21.   <li> . </li>
  22.   <li> . </li>
  23.   <li>1 </li>
  24.   <li> . </li>
  25.   <li> . </li>
  26.   <li> . </li>
  27.   <li> . </li>
  28.   <li>2 </li>
  29.   <li> . </li>
  30.   <li> . </li>
  31.   <li> . </li>
  32.   <li> . </li>
  33.   <li>3 </li>
  34.   <li> . </li>
  35.   <li> . </li>
  36.   <li> . </li>
  37.   <li> . </li>
  38.   <li>4 </li>
  39.   <li> . </li>
  40.   <li> . </li>
  41.   <li> . </li>
  42.   <li> . </li>
  43.   <li>5 </li>
  44.   <li> . </li>
  45.   <li> . </li>
  46.   <li> . </li>
  47.   <li> . </li>
  48.   <li>6 </li>
  49.   <li> . </li>
  50.   <li> . </li>
  51.   <li> . </li>
  52.   <li> . </li>
  53.   <li>7 </li>
  54.   <li> . </li>
  55.   <li> . </li>
  56.   <li> . </li>
  57.   <li> . </li>
  58.   <li>8 </li>
  59.   <li> . </li>
  60.   <li> . </li>
  61.   <li> . </li>
  62.   <li> . </li>
  63.   <li>9 </li>
  64.   <li> . </li>
  65.   <li> . </li>
  66.   <li> . </li>
  67.   <li> . </li>
  68.   <li>10</li>
  69.   <li> . </li>
  70.   <li> . </li>
  71.   <li> . </li>
  72.   <li> . </li>
  73.   <li>11</li>
  74.   <li> . </li>
  75.   <li> . </li>
  76.   <li> . </li>
  77.   <li> . </li>
  78.  </ul>
  79.  <div id="time" style="position:absolute; top:280px;"></div>
  80.  <script type="text/javascript">
  81.  
  82.  drawClock();
  83.  drawTime();
  84.  
  85.  
  86.  function drawTime(){
  87.   var listElements = document.getElementsByTagName("li");
  88.   for(var i=0; i<listElements.length; i++){
  89.    listElements[i].style.color="#CCCCCC";      
  90.    listElements[i].style.fontWeight="normal";
  91.    listElements[i].style.border="none";
  92.   }
  93.   var d = new Date();
  94.   var second = d.getSeconds();
  95.   var hour = d.getHours();
  96.   var minute = d.getMinutes();
  97.  
  98.   document.getElementById("time").innerHTML=hour+":"+minute+":"+second;
  99.   if(hour>12){hour -= 12;}
  100.   hour=hour*5;
  101.  
  102.   listElements[hour].style.color="blue";
  103.   listElements[hour].style.fontWeight="bold";
  104.   listElements[hour].style.border="solid 1px blue";
  105.   listElements[minute].style.color="green";
  106.   listElements[minute].style.fontWeight="bold";
  107.   listElements[minute].style.border="solid 1px green";
  108.   listElements[second].style.color="orange";
  109.   listElements[second].style.border="solid 1px orange";
  110.  
  111.  
  112.   setTimeout(drawTime,1000);
  113.  }
  114.  
  115.  function drawClock(){
  116.   var listElements = document.getElementsByTagName("li");
  117.   var step = (2*Math.PI)/listElements.length;
  118.   var angle=1.5*Math.PI;
  119.   var circleCenterX = 120;
  120.   var circleCenterY = 120;
  121.   var radius = 120;
  122.   for(var i = 0; i<listElements.length; i++)
  123.   { 
  124.     var element = listElements[i];
  125.     var left=Math.round(circleCenterX+radius*Math.cos(angle));
  126.     var top=Math.round(circleCenterY+radius*Math.sin(angle));
  127.     element.style.left = left+"px";
  128.     element.style.top = top+"px";
  129.     angle+=step;   
  130.   }
  131.  }
  132.  
  133.  </script>
  134. </body>
  135. </html>

-Frinny
Dec 17 '09 #3
Dormilich
8,658 Recognized Expert Moderator Expert
mathematical note:
all trigonometric and exponential function use unit-less parameters by default.

in case of the circle, 2π represents the full circle (same as 360°).
reason: c = 2π·r, by dividing c by r (to make the circumference independent (and unit-less)) you get 2π for a full circle, always. q.e.d.

in case of exponential function you can argue, that the exponent is the number, a value is multiplied with itself and naturally this number (the repetition times) is unit-less.

extending trigonometric functions from exponential functions: complex numbers can be written either trigonometric or exponential style, which uses the angle as parameter.
r·cos(φ) + i·r·sin(φ) = r·exp(iφ)
thus, if the exponential style can’t use unit-bearing values, the trigonometric can’t either.
Dec 17 '09 #4
Frinavale
9,735 Recognized Expert Moderator Expert
Thanks for the refresher Dorm. I actually had to look up all of that stuff yesterday because it's been such a long time since I've used trig.

I think I might be getting closer to finding an answer to my original question: "how do you draw a line".

I think the answer might be to use the HTML5 canvas.

:)

-Frinny
Dec 17 '09 #5
Dormilich
8,658 Recognized Expert Moderator Expert
canvas seems like a good idea. JavaScript was not made for drawing to begin with. you may also think about using SVG graphics in XHTML.

SVG/HTML @ MDC
Dec 17 '09 #6
Frinavale
9,735 Recognized Expert Moderator Expert
*Sigh* Internet Explorer...
When will MS get their *stuff* together enough to keep up!
Whatever...I'm dedicating today to learning about the <canvas> and the rest of the new features offered in HTML 5.

I know JavaScript and HTML weren't actually supposed to be used for what I want to do (draw lines etc). I was toying with the idea of putting a whole bunch of <span>s or maybe some inner lists so that I could simulate drawing a line using these HTML elements....but I didn't like the idea. The other idea I had was to use images...but didn't like that idea either (my images would have to be angled properly and I don't think I have the graphics skills to do this using Paint or Gimp). So the <canvas> looks very appealing to me :)

-Frinny
Dec 17 '09 #7
Dormilich
8,658 Recognized Expert Moderator Expert
found a JS drawing library by chance:
High Performance JavaScript Vector Graphics Library

cross browser compatible, but not compareable to vector graphics.
Dec 18 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
6709
by: James Harriman | last post by:
Hi, I need to be able to measure a time interval in milliseconds on a windows machine. I have tried using time.clock() but it appears to measure time in seconds...Is there a way to measure time more precisely? Regards, James Harriman v38zy@unb.ca
0
1155
by: Robert Brewer | last post by:
Analog is a popular web log analyzer and report generator. I wrote a small Python helper for it today for my Dev Manager, looked for similar stuff via Google, and came up dry. Just curious if anyone else has similar snippets; I thought I might try to make a moderately-complete Python library module for Analog. Reply with links, discussion, or code. :) Robert Brewer MIS
33
47658
by: Pushkar Pradhan | last post by:
I'm using clock() to time parts of my code e.g. clk1 = clock(); /* code */ clk2 = clock(); /* calculate time in secs */ ...... clk1 = clock(); /* code */ clk2 = clock();
54
4045
by: CoreyWhite | last post by:
The following experiment is a demonstration of TIME TRAVEL. When writing this program, and testing it out I found that sometimes the program would engage itself in time travel but other times it would not. After careful testing and reprogramming I have optimized the code so it should work every time, however I recommend that you leave the room while it is running and think of something else. It takes about 5 minuets to run so it is...
2
2929
by: cj | last post by:
Any body know a neat trick to put a analog clock on a form that would be updated by a timer? Far out in the uncharted backwaters of the unfashionable end of the Western Spiral arm of the Galaxy lies a small unregarded yellow sun. Orbiting this at a distance of roughly ninety-eight million miles is an utterly insignificant little blue-green planet whose ape-descended life forms are so amazingly primitive that they still think digital...
12
4108
by: cenktarhancenk | last post by:
is there a way to display a ticking clock in a web page using javascript? but not in a textbox, rather as text that i can change the style, font etc. cenk tarhan
0
2083
Ali Rizwan
by: Ali Rizwan | last post by:
Is any body knw how to make an analog clock in vb6.0 Thanks in Advance
0
1650
fayazmd
by: fayazmd | last post by:
Hi, I am creating an analog clock on my form by paint method. By default it is displaying at top left corner of my form. i wanted to put that clock wherever i want, means i want to display that clock at particular location. How can i use paint to display at particular location
6
2151
by: Jerry Spence1 | last post by:
Strange request, but does anyone know of a conrol which is an analogue clock face. I don't want it to work like a clock, I just want to be able to grab the hands and set an alarm time for use in my program. Obviously I need to be able to read the time that it is set to. Any ideas? -Jerry
0
9881
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11354
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10923
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7256
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5943
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3368
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.