Introduction
Hello, fellow developers! Today, I'm thrilled to share a project I recently completed: an Analog Clock. This project is a visually appealing and interactive way to display time using a traditional analog clock face. It's an excellent project for honing your JavaScript, CSS, and HTML skills, particularly in working with animations, DOM manipulation, and time-based functions. Whether you're a beginner looking to practice or an experienced developer wanting to create a classic clock interface, this project is a great choice.
Project Overview
The Analog Clock is a real-time clock that mimics the appearance and functionality of a traditional analog clock. The clock dynamically updates every second, with the hour, minute, and second hands rotating smoothly to reflect the current time. This project is ideal for developers who want to practice building dynamic and visually appealing web applications.
Features
- Real-Time Clock: The clock updates every second, showing the current time with moving hour, minute, and second hands.
- Smooth Animations: The clock hands rotate smoothly, creating a realistic analog clock effect.
- Responsive Design: The clock is designed to be responsive, ensuring it looks great on various devices and screen sizes.
- Minimalist Design: The clock features a clean and simple design, focusing on functionality and elegance.
Technologies Used
- HTML: Used to structure the webpage and the clock's layout.
- CSS: Applied to style the clock, including positioning the hands and adding smooth animations.
- JavaScript: Implemented to handle the clock's time calculations, update the DOM, and manage the hands' rotation.
Project Structure
Here's a quick look at the project structure:
Analog-Clock/
├── index.html
├── style.css
└── script.js
- index.html: Contains the HTML structure of the webpage.
- style.css: Holds the CSS styles, including animations and responsive design.
- script.js: Manages the dynamic aspects of the clock using JavaScript.
Installation
To get started with the project, follow these steps:
-
Clone the repository:
git clone https://github.com/abhishekgurjar-in/Analog-Clock.git
-
Open the project directory:
cd Analog-Clock
-
Run the project:
- You can either run it on a local server or simply open the
index.html
file in a web browser.
- You can either run it on a local server or simply open the
Usage
- Open the website in a web browser.
- Watch the clock as it displays the current time with a smooth animation of the hour, minute, and second hands.
Code Explanation
HTML
The index.html
file sets up the structure of the webpage, including the clock container and the header. Below is a snippet of the HTML code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Analog Clock</title>
<link rel="stylesheet" href="style.css" />
<script src="script.js" defer></script>
</head>
<body>
<div class="header">
<h1>Analog Clock</h1>
</div>
<div id="clockContainer">
<div id="hour"></div>
<div id="minute"></div>
<div id="second"></div>
</div>
<div class="footer">
<p>Made with ❤️ by Abhishek Gurjar</p>
</div>
</body>
</html>
CSS
The style.css
file styles the clock's container and hands, ensuring they rotate correctly to display the time. Key styles include:
#clockContainer {
position: relative;
margin: auto;
height: 30vw;
width: 30vw;
background: url(clock.png) no-repeat;
background-size: 100%;
}
#hour,
#minute,
#second {
position: absolute;
background: black;
border-radius: 10px;
transform-origin: bottom;
}
#hour {
width: 1.8%;
height: 25%;
top: 25%;
left: 48.85%;
opacity: 0.8;
}
#minute {
width: 1.6%;
height: 30%;
top: 19%;
left: 48.9%;
opacity: 0.8;
}
#second {
width: 1%;
height: 40%;
top: 9%;
left: 49.25%;
opacity: 0.8;
}
.header {
margin: 80px;
text-align: center;
}
.footer {
margin: 50px;
text-align: center;
}
JavaScript
The script.js
file handles the calculation of the current time and updates the rotation of the clock's hands accordingly. Below is a snippet of the JavaScript code:
setInterval(() => {
const date = new Date();
const hourTime = date.getHours();
const minuteTime = date.getMinutes();
const secondTime = date.getSeconds();
const hourRotation = 30 * hourTime + minuteTime / 2;
const minuteRotation = 6 * minuteTime;
const secondRotation = 6 * secondTime;
const hour = document.getElementById('hour');
const minute = document.getElementById('minute');
const second = document.getElementById('second');
hour.style.transform = `rotate(${hourRotation}deg)`;
minute.style.transform = `rotate(${minuteRotation}deg)`;
second.style.transform = `rotate(${secondRotation}deg)`;
}, 1000);
Live Demo
You can check out the live demo of the Analog Clock here.
Conclusion
Building this Analog Clock was a rewarding experience that allowed me to delve deeper into JavaScript animations and DOM manipulation. I hope this project inspires you to create your own interactive and visually appealing applications. Feel free to explore the code, customize it, and use it in your own projects. Happy coding!
Credits
This project was inspired by the classic design of analog clocks and the need for a simple, real-time time display tool.
Author
- Abhishek Gurjar
Top comments (6)
I think this clock is very interesting
This is a great first step. Next I suggest learning how to make it scalable using an .svg instead of a .png and declarative SMIL animation on elements within the SVG instead of javascript setInterval on style of generic DIVs.
Here's an example SVG analog clock.
Cool. Only one thing: it might not matter in most cases But in your solution the seconds could be off by almost a second.
Awesome project!
I love it.
I like the idea, it's gonna be good if you make the clock as a npm package, and add a way to make custom CSS for it
Some comments may only be visible to logged-in visitors. Sign in to view all comments.