Analog Clock

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    Analog Clock

    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?

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
     <style type="text/css">
      li{
       position:absolute;
       list-style:none;
       text-align:center;
       color:#CCCCCC;  
      }
     </style>
    </head>
    <body>
      
     <ul>
      <li>12</li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li>1 </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li>2 </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li>3 </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li>4 </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li>5 </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li>6 </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li>7 </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li>8 </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li>9 </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li>10</li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li>11</li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
      <li> . </li>
     </ul>
     <div id="time" style="position:absolute; top:280px;"></div>
     <script type="text/javascript">
    
     drawClock();
     drawTime();
    
    
     function drawTime(){
      var listElements = document.getElementsByTagName("li");
      for(var i=0; i<listElements.length; i++){
       listElements[i].style.color="#CCCCCC";      
       listElements[i].style.fontWeight="normal";
       listElements[i].style.border="none";
      }
      var d = new Date();
      var second = d.getSeconds();
      var hour = d.getHours();
      var minute = d.getMinutes();
    
      document.getElementById("time").innerHTML=hour+":"+minute+":"+second;
      if(hour>12){hour -= 12;}
      hour=hour*5;
      
      listElements[hour].style.color="blue";
      listElements[hour].style.fontWeight="bold";
      listElements[hour].style.border="solid 1px blue";
      listElements[minute].style.color="green";
      listElements[minute].style.fontWeight="bold";
      listElements[minute].style.border="solid 1px green";
      listElements[second].style.color="orange";
      listElements[second].style.border="solid 1px orange";
    
    
      setTimeout(drawTime,1000);
     }
    
     function drawClock(){
      var listElements = document.getElementsByTagName("li");
      var step = (2*Math.PI)/listElements.length;
      var angle=0;
      //angle=180.60;//<--sets 12 to the 12 o'clock position ?? not sure why
      var circleCenterX = 120;
      var circleCenterY = 120;
      var radius = 120;
      for(var i = 0; i<listElements.length; i++)
      { 
        var element = listElements[i];
        var left=Math.round(circleCenterX+radius*Math.cos(angle));
        var top=Math.round(circleCenterY+radius*Math.sin(angle));
        element.style.left = left+"px";
        element.style.top = top+"px";
        angle+=step;   
      }
     }
    
     </script>
    </body>
    </html>
    Thanks!

    -Frinny
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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)

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Thanks Dorm. Using 1.5*Math.PI properly set 12 at the 12 o'clock position.

      Code:
      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
      "http://www.w3.org/TR/html4/strict.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      
      <head>
       <style type="text/css">
        li{
         position:absolute;
         list-style:none;
         text-align:center;
         color:#CCCCCC;  
        }
       </style>
      </head>
      <body>
        
       <ul>
        <li>12</li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li>1 </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li>2 </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li>3 </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li>4 </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li>5 </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li>6 </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li>7 </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li>8 </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li>9 </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li>10</li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li>11</li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
        <li> . </li>
       </ul>
       <div id="time" style="position:absolute; top:280px;"></div>
       <script type="text/javascript">
      
       drawClock();
       drawTime();
      
      
       function drawTime(){
        var listElements = document.getElementsByTagName("li");
        for(var i=0; i<listElements.length; i++){
         listElements[i].style.color="#CCCCCC";      
         listElements[i].style.fontWeight="normal";
         listElements[i].style.border="none";
        }
        var d = new Date();
        var second = d.getSeconds();
        var hour = d.getHours();
        var minute = d.getMinutes();
      
        document.getElementById("time").innerHTML=hour+":"+minute+":"+second;
        if(hour>12){hour -= 12;}
        hour=hour*5;
        
        listElements[hour].style.color="blue";
        listElements[hour].style.fontWeight="bold";
        listElements[hour].style.border="solid 1px blue";
        listElements[minute].style.color="green";
        listElements[minute].style.fontWeight="bold";
        listElements[minute].style.border="solid 1px green";
        listElements[second].style.color="orange";
        listElements[second].style.border="solid 1px orange";
      
      
        setTimeout(drawTime,1000);
       }
      
       function drawClock(){
        var listElements = document.getElementsByTagName("li");
        var step = (2*Math.PI)/listElements.length;
        var angle=1.5*Math.PI;
        var circleCenterX = 120;
        var circleCenterY = 120;
        var radius = 120;
        for(var i = 0; i<listElements.length; i++)
        { 
          var element = listElements[i];
          var left=Math.round(circleCenterX+radius*Math.cos(angle));
          var top=Math.round(circleCenterY+radius*Math.sin(angle));
          element.style.left = left+"px";
          element.style.top = top+"px";
          angle+=step;   
        }
       }
      
       </script>
      </body>
      </html>

      -Frinny

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        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.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          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

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            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
            Last edited by Dormilich; Dec 17 '09, 03:02 PM. Reason: added link

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              *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

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                found a JS drawing library by chance:
                High Performance JavaScript Vector Graphics Library

                cross browser compatible, but not compareable to vector graphics.

                Comment

                Working...