Hello readers, Today in this blog you’ll learn how to create a real-time pendulum clock with Javascript. Earlier, you learned to send emails using PHP. I have also created a blog on other cool stuff about web designing.

 

Creating such a pendulum clock is really fun and full of excitement. Trying these sorts of fun projects helps you to grow your skills. Today I’m going to show you how you can create a real-time pendulum clock with awesome animation. The whole animation is done with CSS and the time-showing functionality is created in Javascript. At first, I created some div for the frame, pendulum & other parts of the clock then I styled them nicely with CSS and at last, I created time-showing functionality using Javascript.

 

As you notice, the numbers of the clock are perfectly placed in the correct position just like in a real clock. For that, I have given the ‘position relative’ property to the parent div and the ‘position absolute’ property to the child div which makes all numbers from 1 to 12 be placed at the same place by overlapping each other. After that, I rotated the numbers starting from 30 degrees to 360 degrees increasing each by the value of 30.

 

The pendulum completes its animation within 2 seconds but it is given for infinite time so the animation never ends. The pendulum moves 30 degrees to the right and returns to 0 degrees within 1 second and then it moves 30 degrees to the left and again returns to 0 degrees within the remaining 1 second. I have also set the ‘transform-origin’ property at the top of the pendulum which does not allow to move the top of the pendulum. It makes the top of the pendulum be placed in the same position without rotating. That’s all how I animate pendulum. The cross at the bottom of the pendulum is created with the ‘after’ pseudo-element which has the clip-path property to give the cross shape.

 

For getting the exact time I used the ‘Date()’ function of Javascript, then I stored the hour, minute & second values in different variables. I have submitted the hour time to the hour hand, the minute time to the minute hand & second time to the second hand. At last, I have set the rotation property to all the hands so that they continue their rotation as per time. That’s all I have done for showing time with hands.


If you’re feeling difficult to understand what I am saying. You can watch a full video tutorial on this program (Real Time Pendulum Clock with Javascript).

 

You Might Also Like

Video Tutorial of Real-Time Pendulum Clock with Javascript

All this page design and animation are based on CSS and time showing are based on Javascript. If you’re a beginner you can also create this type of pendulum clock. If you like this program (Real Time Pendulum Clock with Javascript) and want to get source code. You can easily get the source code of this program. To get the source code you just need to scroll down.

Real-Time Pendulum Clock with Javascript [Source Codes]

To create this program (Real Time Pendulum Clock with Javascript), you need to create three files one HTML file, one CSS file & one Javascript file. After creating these files just paste the following code into your file. First, create an HTML file with the name index.html and paste the given codes into your HTML file. Note it, you’ve to create a file with a .html extension.

HTML

Second, create a CSS file with the name style.css and paste the given code into your CSS file. Note it, you’ve to create a file with a .css extension.

CSS

At last, create a Javascript file with the name script.js and paste the given code into your Javascript file. Note it, you’ve to create a file with a .js extension.

 JavaScript

That’s all, now you’ve successfully created a Real-Time Pendulum Clock with Javascript. If your code does not work or you’ve faced any errors/problems then please comment down. If you feel this article had helped you, please share it with your friends.

Post a Comment

Previous Post Next Post