If you want to make Analog Clock using JavaScript then this tutorial will help you completely. Here I have shared a step-by-step tutorial on how to make JavaScript analog clock.
Analog clocks we all use at home. But if you want you can make it digitally and only with the help of HTML CSS and JavaScript. This type of project will help you a lot if you are a beginner. The time shown here is not the time on any server. It will take time for your device then update that time every second.
JavaScript’s New Date () method is used to capture time from the device. Then setInterval is used to update it every second. In addition, depending on some basic calculations, the hands of hours, minutes, and seconds keep rotating.
See the Pen analog clock 7 by Foolish Developer (@fghty) on CodePen.
Here time has to be converted into degrees and these hands will continue to rotate according to that degree. Earlier I shared with you the design of making many types of analog clocks. However, it is built in a completely modern and simple way.
Here I have not used the number 1 to 12. Instead, we have created a few symbols that will work for this number. But these signs I have created in a completely new strategy that will surely surprise you.
Analog Clock using HTML CSS and Javascript
Here first I created its basic design using HTML and CSS. Then I implemented it using JavaScript. If you only want to get the source code then you can use the download button at the bottom of the article. However, I urge you to follow the tutorials below.
This tutorial will help you to know the complete step-by-step how I made it (analog clock using HTML CSS and javascript). With each step, I have shown the possible results with pictures. Every beginner can easily understand.
1. Basic structure of an analog clock
The basic structure of the analog clock has been created using the following HTML and CSS code.
The following CSS helped to design the webpage. Here I have used light white as the background color of the web page.
Now I have designed this simple analog clock. Here the width of the clock: 30rem, height: 30rem, and border-radius helped to make it round.
A white border of 8 px has been used to enhance the beauty. Since the background color of the clock and the background color of the webpage are used the same, the box-shadow has helped to understand the size of the clock.
2. Create two colorful lines
Using the following codes, we have created two lines in this analog clock vertically. These two lines will basically help to better understand the time. The CSS code ‘:: before’ and ‘:: after’ first helped to create these two lines.
3. Create four more colorful lines
Now we have created four more lines in the JavaScript analog clock. As a result, there are currently a total of 6 lines in the clock, and each of these lines is at a 30-degree angle to each other.
4. Draw a circle in the center of the clock
Now I have created a circle in this analog clock. This circle will be in the middle of the clock. As a result, some of the lines are covered and some parts of the last can be seen. Now some parts in this last will help to work as numbers from 1 to 12.
Now, using the CSS below, I have made a small point in the very middle of this clock. The hands will continue to rotate around this point. Border-radius has been used to make this point width: 16px, height: 16px and make it completely round.
5. Make 3 hands in analog clock
Now is the time to create three hands that will help to point out the times. These hands will indicate the hours, minutes, and seconds, respectively. I have designed each hand differently. Here you can change the colors according to your needs.
6. Activate the analog clock using JavaScript
Above we have completely designed this analog clock. Now is the time to implement it using some JavaScript. As I said before, the hands in this clock keep rotating by converting time into degrees.
Below I have given all the JavaScript code together. I have given the necessary explanation for each line. Hopefully, those explanations will help you understand the JavaScript code.
Hopefully from the tutorial above you have learned how to create this JavaScript analog clock. If you have any problems while creating this project (Create an Analog Clock using HTML, CSS, and JavaScript), please let me know in the comments.