Certainly! Below is a self-contained HTML document that combines HTML, CSS, and JavaScript to create a visually appealing and accurate analog clock. This clock uses the <canvas> element to draw the clock face and hands, updating every second to reflect the current time.
Full HTML Code for an Analog Clock
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Perfect Analog Clock</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: #f0f0f0;
}
#clockCanvas {
background: #fff;
border: 8px solid #333;
border-radius: 50%;
box-shadow:
0 0 10px rgba(0,0,0,0.5),
inset 0 0 10px rgba(0,0,0,0.2);
}
</style>
</head>
<body>
<canvas id="clockCanvas" width="400" height="400"></canvas>
<script>
const canvas = document.getElementById('clockCanvas');
const ctx = canvas.getContext('2d');
const radius = canvas.height / 2;
ctx.translate(radius, radius); // Move to center
const clockRadius = radius * 0.90;
function drawClock() {
drawFace(ctx, clockRadius);
drawNumbers(ctx, clockRadius);
drawTime(ctx, clockRadius);
}
function drawFace(ctx, radius) {
// Draw white face
ctx.beginPath();
ctx.arc(0, 0, radius, 0, 2 * Math.PI);
ctx.fillStyle = '#fff';
ctx.fill();
// Draw border
ctx.lineWidth = radius * 0.05;
ctx.strokeStyle = '#333';
ctx.stroke();
// Draw center
ctx.beginPath();
ctx.arc(0, 0, radius * 0.05, 0, 2 * Math.PI);
ctx.fillStyle = '#333';
ctx.fill();
}
function drawNumbers(ctx, radius) {
const fontSize = radius * 0.15;
ctx.font = `${fontSize}px Arial`;
ctx.textBaseline = "middle";
ctx.textAlign = "center";
for (let num = 1; num <= 12; num++) {
const ang = num * Math.PI / 6;
ctx.rotate(ang);
ctx.translate(0, -radius * 0.85);
ctx.rotate(-ang);
ctx.fillText(num.toString(), 0, 0);
ctx.rotate(ang);
ctx.translate(0, radius * 0.85);
ctx.rotate(-ang);
}
}
function drawTime(ctx, radius) {
const now = new Date();
let hour = now.getHours();
let minute = now.getMinutes();
let second = now.getSeconds();
// Hour
hour = hour % 12;
const hourAngle = (hour * Math.PI / 6) +
(minute * Math.PI / (6 * 60)) +
(second * Math.PI / (360 * 60));
drawHand(ctx, hourAngle, radius * 0.5, radius * 0.07);
// Minute
const minuteAngle = (minute * Math.PI / 30) +
(second * Math.PI / (30 * 60));
drawHand(ctx, minuteAngle, radius * 0.75, radius * 0.07);
// Second
const secondAngle = second * Math.PI / 30;
drawHand(ctx, secondAngle, radius * 0.85, radius * 0.02, '#FF0000');
}
function drawHand(ctx, pos, length, width, color = '#333') {
ctx.beginPath();
ctx.lineWidth = width;
ctx.lineCap = "round";
ctx.strokeStyle = color;
ctx.moveTo(0, 0);
ctx.rotate(pos);
ctx.lineTo(0, -length);
ctx.stroke();
ctx.rotate(-pos);
}
// Initial draw
drawClock();
// Update every second
setInterval(drawClock, 1000);
</script>
</body>
</html>
Explanation of the Code
-
HTML Structure:
- The
<canvas> element with the ID clockCanvas is used to draw the clock. It's set to 400x400 pixels, but you can adjust the size as needed.
-
CSS Styling:
- The
body is styled to center the canvas both vertically and horizontally with a light gray background.
- The
#clockCanvas has a white background, a dark border, rounded edges (border-radius: 50%), and some shadow effects to give it a three-dimensional appearance.
-
JavaScript Functionality:
- Initialization:
- The canvas context is obtained, and the origin is moved to the center of the canvas using
ctx.translate(radius, radius).
- drawClock Function:
- Calls three functions:
drawFace, drawNumbers, and drawTime to render different parts of the clock.
- drawFace Function:
- Draws the white circular face of the clock.
- Adds a dark border around the face.
- Draws a small central circle where the hands pivot.
- drawNumbers Function:
- Places the numbers 1 through 12 around the clock face.
- Calculates the angle for each number and positions it accordingly.
- drawTime Function:
- Retrieves the current time.
- Calculates the angles for the hour, minute, and second hands, accounting for the progression of time within each unit (e.g., the hour hand moves as the minutes pass).
- Calls
drawHand to render each hand with appropriate length, width, and color.
- drawHand Function:
- Draws a single hand based on the given angle (
pos), length, width, and color.
- Rotates the canvas to the desired angle, draws the line, and then rotates it back.
- Animation:
- The clock is initially drawn once with
drawClock().
setInterval(drawClock, 1000) ensures the clock updates every second to reflect the current time accurately.
Customization
-
Size: To change the size of the clock, adjust the width and height attributes of the <canvas> element and ensure the JavaScript calculations scale accordingly.
-
Colors: Modify the fillStyle, strokeStyle, and other color-related properties in the CSS and JavaScript to match your preferred color scheme.
-
Styles: Additional CSS can be added to enhance the appearance further, such as adding gradients, textures, or different shadow effects.
How to Use
-
Save the Code: Copy the entire code above into a text editor and save it with an .html extension, for example, analog-clock.html.
-
Open in Browser: Double-click the saved HTML file or open it in your preferred web browser to view the analog clock in action.
This implementation provides a smooth and accurate analog clock that updates in real-time. Feel free to enhance and modify it to better suit your design preferences or functional requirements.