Skip to content

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
Vance Muchongo committed Dec 12, 2024
1 parent 8ad633b commit 355d9d5
Showing 1 changed file with 58 additions and 35 deletions.
93 changes: 58 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# JavaScript Canvas Clock
# JavaScript Canvas Clock ⏰✨

This is a simple clock created using JavaScript and HTML5 Canvas. Steps 1 to 10 show how the clock wwas made.
This is a vibrant and interactive clock application created using JavaScript and HTML5 Canvas! Here's how I brought it to life ⭐✨.

![Display Image](images/clock-main.gif)

## Languages used

[![forthebadge](https://forthebadge.com/images/badges/made-with-javascript.svg)](https://forthebadge.com)
[![forthebadge](https://forthebadge.com/images/badges/uses-html.svg)](https://forthebadge.com)
[![forthebadge](https://forthebadge.com/images/badges/made-with-javascript.svg)](https://forthebadge.com)
[![forthebadge](https://forthebadge.com/images/badges/uses-html.svg)](https://forthebadge.com)
[![forthebadge](https://forthebadge.com/images/badges/uses-css.svg)](https://forthebadge.com)

## Table of Contents
- [JavaScript Canvas Clock](#javascript-canvas-clock)
- [JavaScript Canvas Clock ⏰✨](#javascript-canvas-clock)
- [Languages used](#languages-used)
- [Table of Contents](#table-of-contents)
- [1. Canvas Dimensions](#1-canvas-dimensions)
Expand All @@ -24,70 +24,93 @@ This is a simple clock created using JavaScript and HTML5 Canvas. Steps 1 to 10
- [8. Using setInterval](#8-using-setinterval)
- [9. Clearing the Canvas](#9-clearing-the-canvas)
- [10. Smooth Clock Movement](#10-smooth-clock-movement)
- [11. Project Completion Date](#11-project-completion-date)
- [12. License](#12-license)
- [11. Fetching User Timezone via IP 🌐](#11-fetching-user-timezone-via-ip)
- [12. Project Completion Date](#12-project-completion-date)
- [13. License](#13-license)

## 1. Canvas Dimensions
The canvas dimensions are specified in your code. You can adjust the width and height to fit your requirements.
## 1. Canvas Dimensions 🎨
I set the canvas dimensions in the HTML, allowing flexibility to adjust the width and height to meet specific design needs.

## 2. Origin and Coordinates
The origin of the canvas is typically at the top-left corner (0, 0). Coordinates increase as you move right (X-axis) and down (Y-axis).
## 2. Origin and Coordinates ➑️
The canvas origin is at the top-left corner (0, 0), and coordinates increase as you move right (X-axis) and down (Y-axis). This forms the base of our clock design.

## 3. Drawing Axes
You can create a function to draw axes to help with your design.
## 3. Drawing Axes 🎩
To visualize the grid, I created a function that draws horizontal and vertical axes, emphasizing the center with red lines.

![Drawing Axes](images/clock%20(1).png)

## 4. Drawing a Circle
A circle can be drawn by generating points along its circumference and creating a path from one point to the next. Parameterise a circle with radius, r, and centre, c, as follows:
## 4. Drawing a Circle πŸ”Ή
To draw the clock face, I parameterized a circle using its radius and center coordinates. By iterating over different angles (ΞΈ), I constructed the circle smoothly. πŸ”¬

![Drawing a Circle Equation](images/clock%20(2).png)

Then create a function that draws a circle of radius 240 centered in the middle of the canvas. Draw
the circle by generating points by sweeping over different values of ΞΈ ∈ [0, 2Ο€] and creating a
path from p(ΞΈi) to p(ΞΈi+1). Close and then stroke the path. See how the circle becomes smoother as you increase the number of points.

![Drawing a Circle](images/clock%20(3).png)

## 5. Adding Clock Tics
To represent hours and minutes on the clock, you can create a function that draws lines from the center to different radii.
## 5. Adding Clock Tics πŸ•
To mark hours and minutes, I used a function to add "tics" at calculated angles. Longer tics represent hours, while shorter ones denote minutes.

![Adding Clock Tics](images/clock%20(4).png)

## 6. Displaying Current Datetime
You can use a JavaScript date object to display the current datetime.
## 6. Displaying Current Datetime πŸ“…
I displayed the current datetime using JavaScript's `Date` object and updated it dynamically in the paragraph below the clock.

```javascript
let d = new Date();
document.getElementById("some_id").innerHTML = d;
```
![Displaying Current Datetime](images/clock%20(5).png)

## 7. Creating Clock Hands
Functions convert the current time into ΞΈ values for each clock hand and draw the lines.
## 7. Creating Clock Hands ⏳
I calculated angles for hour, minute, and second hands based on the current time. Using these angles, I drew hands radiating from the clock center.

![Creating Clock Hands](images/clock%20(6).png)

## 8. Using setInterval
The `setInterval` function is used to update the clock every second.
## 8. Using setInterval ⏲
To ensure the clock updated in real-time, I used `setInterval` to refresh the drawing every second. This added a smooth and consistent animation. ✨

![Using setInterval](images/clock%20(7).png)

## 9. Clearing the Canvas
The canvas should be cleared before each render. Use the following code to clear the canvas:
## 9. Clearing the Canvas πŸ’₯
Before redrawing the clock for each frame, I cleared the canvas using:

```javascript
graphics.clearRect(0, 0, canvas.width, canvas.height);
```
This avoided overlaps and ensured clean rendering.

![Clearing the Canvas](images/clock%20(8).png)

## 10. Smooth Clock Movement
Adjust the code to make the clock hands move smoothly, and update the second hand every 10ms. Remove the axes/guidelines.
## 10. Smooth Clock Movement πŸŒͺ
By calculating fractional angles for seconds and minutes, I made the clock hands move smoothly instead of jumping between ticks. This added realism.

![Smooth Clock Movement](images/clock-main.gif)

## 11. Project Completion Date
## 11. Fetching User Timezone via IP 🌐
To customize the clock to the user's timezone, I integrated the `ipapi.co` API. Here's how:

- I fetched the user's timezone using their IP address.
- I used the detected timezone to adjust the time displayed by the clock.
- If timezone detection failed, I defaulted to the local time offset.

### Key Functions:
1. `**fetchTimeZone()**`:
- Queries the API and extracts the timezone.
- Adjusts the time offset accordingly.

2. `**updateDateTime()**`:
- Updates the displayed time every second using the fetched timezone.

### API Details:
```javascript
async function fetchTimeZone() {
const response = await fetch('https://ipapi.co/json/');
const data = await response.json();
console.log(data.timezone);
}
```
This integration ensures a personalized experience for users worldwide! ✨⌚

## 12. Project Completion Date πŸ“†
This project was completed on Sunday, 13 August 2023, at 5:20 PM.

## 12. License
## 13. License 🌐
This project is licensed under the [MIT License](LICENSE).

0 comments on commit 355d9d5

Please sign in to comment.