DEV Community

Cover image for Simple Analog Clock Using Html, CSS & Javascript

Simple Analog Clock Using Html, CSS & Javascript

Foolish Developer on July 15, 2021

In this article I am going to show you how to make an analog clock using HTML CSS and JavaScript code. I have already designed many types of analog...
Collapse
 
mayankkalbhor profile image
Mayank K

Beautiful

Collapse
 
code_mystery profile image
Foolish Developer

Thank you

Collapse
 
efpage profile image
Eckehard • Edited

I rebuilt your beautiful clock in DML, just to see how compact code would be. Here is the complete website:

<!DOCTYPE html>
<html lang="de">

<head>
  <meta charset="utf-8">
  <title>title</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://efpage.de/DML/DML_homepage/lib/DML-min.js"></script>
  <style>
  </style>
</head>

<body>
  <script>  "use strict";
    const cx = 100, cy = 100;  // Radius
    const _clockstyle = "width: " + (2 * cx) + "px;  height: " + (2 * cy) + "px;"
      + "border: 7px solid #282828; background: #585858;"
      + "border-radius: 50%; margin: 50px;"
      + "box-shadow: -4px -4px 10px rgba(67,67,67,0.5), inset 4px 4px 10px rgba(0,0,0,0.5),"
      + "inset -4px -4px 10px rgba(67,67,67,0.5), 4px 4px 10px rgba(0,0,0,0.3);"

    sidiv("", _clockstyle)
    let c = canvas2D({ width: px(2 * cx), height: px(2 * cy) })
    c.ctx.lineCap = "round"
    unselectBase()

    // Paint anything radial
    function tick(color, width, angle, length, innerlength = 0) {
      function ls(length) { return length * Math.sin(angle / 180.0 * Math.PI) }
      function lc(length) { return -length * Math.cos(angle / 180.0 * Math.PI) }
      c.setLineType(width, color)
      c.line(cx + ls(innerlength), cy + lc(innerlength), cx + ls(length), cy + lc(length))
    }

    // Draw clock
    function drawClock() {
      c.clear()
      // Draw ticks
      for (let i = 0; i < 360; i += 30)
        if ((i % 90) == 0) tick("#1df52f", 5, i, 88, 70)
        else tick("#bdbdcb", 3, i, 88, 75)

      // draw hands
      let t = new Date();  // get time
      tick("#61afff", 5, t.getHours() * 30, 50)  // hour
      tick("#71afff", 2, t.getMinutes() * 6, 70)  // min
      tick("#ee791a", 2, t.getSeconds() * 6, 80)  // s

      // drwa center
      c.setFillStyle("#4d4b63")
      c.circle(cx, cy, 10, { fill: true })
    }
    drawClock()
    setInterval(drawClock, 1000)
  </script>
</body>

</html>
Enter fullscreen mode Exit fullscreen mode

ThatΒ΄s all. For practical use it would be better to put the code into a separate class for convenience, which is pretty simple in DML.

Post edit: here it is: a working clock class, can be used as Webcomponent too...

Collapse
 
madza profile image
Madza

Very clean, both the ui and code πŸ‘πŸ˜‰

Collapse
 
code_mystery profile image
Foolish Developer

Thank you for like it

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
code_mystery profile image
Foolish Developer

ok

Collapse
 
esger profile image
Esger Jellema • Edited

Your clock is very nice, though it can be done without the JavaScript too :) (not mine)
codepen.io/nobitagit/pen/birjo

Collapse
 
code_rabbi profile image
Emeka Orji

Yh! But that would not get the current time

Collapse
 
esger profile image
Esger Jellema

This one can, after setting it.. css-tricks.com/of-course-we-can-ma...

Collapse
 
abskr82 profile image
abskr82

Good explanation.

Collapse
 
code_mystery profile image
Foolish Developer

Thank you

Collapse
 
abhaycs profile image
Abhay Sharma

Crystal clear!

Collapse
 
jfftck profile image
jfftck

I have some suggestions on how to improve this.

  1. Create a function that handles the redraw interval using window.requestAnimationFrame() and window.setTimeout() instead of window.setInterval() as it should cut down on rendering cycles.
  2. Look at using HTML canvas as it will use much less memory since only 1 HTML element is created.

These are the ways that I would expand on this code to improve, not only performance, but my skill set.

Collapse
 
maixuanhan profile image
Han Mai

I doubt that.

  1. How will requestAnimationFrame() help in this case? Interval resolution is 1 second.
  2. Are there any measurements for this or you’ve just guessed?
Collapse
 
code_rabbi profile image
Emeka Orji

This is amazing!!

πŸ‘πŸ‘

Collapse
 
code_mystery profile image
Foolish Developer

Thank you πŸ˜€πŸ˜€πŸ˜

Collapse
 
code_rabbi profile image
Emeka Orji • Edited

😍Amazingly, I found myself doing the same thing today. I built an Analog Clock! And I coded the whole thing on my android device.πŸ‘ŒπŸ‘Œ. Thanks man

Collapse
 
ayabouchiha profile image
Aya Bouchiha

Amazing

Collapse
 
code_mystery profile image
Foolish Developer

Thank you

Collapse
 
madrafj profile image
Ahmad Rafsanjani

Beautiful..
Btw 'SecondHand' distracted me😁

Collapse
 
code_mystery profile image
Foolish Developer

πŸ˜‚πŸ˜‚πŸ˜…

Collapse
 
artydev profile image
artydev

Great :-)

Collapse
 
luzhenqian profile image
luzhenqian

Great! thank you for writing such a wonderful tutorial. Can I translate it and repost it to the developer community website in China?

Collapse
 
code_mystery profile image
Foolish Developer

Of course

Collapse
 
ashutoshmishra profile image
Ashutosh Mishra

Your UI is very amazing and beautiful

Collapse
 
code_mystery profile image
Foolish Developer

Thank you

Collapse
 
nitesh603 profile image
nitesh603

Thank you!

Collapse
 
code_mystery profile image
Foolish Developer

Welcome 😍

Collapse
 
rentacar profile image
Info Comment hidden by post author - thread only accessible via permalink
Rentacar
Collapse
 
hosseinjafari9574 profile image
Hossein_jafari

lovely :)

Collapse
 
curioushokie profile image
CuriousHokie

Very nice clock solution. Different from the clock solution on W3Schools: w3schools.com/graphics/canvas_cloc...

Collapse
 
forkbikash profile image
Bikash Mishra

Good one!

Collapse
 
code_mystery profile image
Foolish Developer

Thank you BIKASH MISHRA

Collapse
 
jreegene profile image
Jeremiah

Nice one. Thanks for sharing

Collapse
 
code_mystery profile image
Foolish Developer

Welcome 😍

Collapse
 
exoticallisticsm profile image
Salieu Sesay

Excellent coding flow.

I wished all code supports are written as elegant as this.

Collapse
 
cwilliams2024 profile image
Calvin Williams

Wow

Collapse
 
yogesh27 profile image
yogesh-kumar-27

learn a lot form this as a beginner

Collapse
 
code_mystery profile image
Foolish Developer

Your comments have encouraged me to write more articles of this kind. Thank you 😍

Collapse
 
eliseogarcia1024 profile image
EliseoGarcia1024

What time is it
Nice.

Collapse
 
code_mystery profile image
Foolish Developer

Thank you 😍

Collapse
 
goesttheinfor profile image
Goest The Infor

Dope πŸ‘Œ

Collapse
 
code_mystery profile image
Foolish Developer

😍

Collapse
 
jona42ui profile image
JonathanThembo

Thanks foolishdev., tho;
My clock is not starting, I have no clie why

Collapse
 
aakashsingh1210 profile image
aakashsingh1210

Wow

Collapse
 
code_mystery profile image
Foolish Developer • Edited

😍

Collapse
 
code_mystery profile image
Foolish Developer

😍

Collapse
 
sujoywebdev profile image
Sujoy Dutta

Thankyou, works like a charm. Smart idea and the neo look is awesome.

Collapse
 
code_mystery profile image
Foolish Developer

Welcome 😍

Collapse
 
piyush181 profile image
Piyush Raj

I followed along, but it gives me this error: javascript.js:10 Uncaught TypeError: Cannot read property 'style' of null
at setDate (javascript.js:10)
at javascript.js:23

Some comments have been hidden by the post's author - find out more