View raw output
Here's a self-contained HTML file that renders a working analog clock with hour, minute, and second hands plus the numbers 1–12, all drawn on a canvas.
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Analog Clock</title>
<style>
html, body {
height: 100%;
margin: 0;
}
body {
display: flex;
align-items: center;
justify-content: center;
background: radial-gradient(circle at 50% 25%, #2b3040 0%, #12141b 65%, #0a0b0f 100%);
font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
overflow: hidden;
}
canvas {
display: block;
width: min(85vmin, 520px);
height: min(85vmin, 520px);
border-radius: 50%;
box-shadow:
0 24px 60px rgba(0, 0, 0, 0.65),
0 0 0 1px rgba(255, 255, 255, 0.06),
0 0 60px rgba(120, 170, 255, 0.12);
}
</style>
</head>
<body>
<canvas id="clock" aria-label="Analog clock" role="img"></canvas>
<script>
(function () {
"use strict";
var SIZE = 1000; // logical drawing size
var CX = SIZE / 2;
var CY = SIZE / 2;
var TAU = Math.PI * 2;
var canvas = document.getElementById("clock");
var ctx = canvas.getContext("2d");
/* ---------- sizing ---------- */
function resize() {
var dpr = window.devicePixelRatio || 1;
var rect = canvas.getBoundingClientRect();
var px = Math.max(1, Math.round(rect.width * dpr));
canvas.width = px;
canvas.height = px;
var scale = px / SIZE;
ctx.setTransform(scale, 0, 0, scale, 0, 0);
}
window.addEventListener("resize", resize);
/* ---------- helpers ---------- */
function polar(r, angle) {
// angle 0 = 12 o'clock, clockwise
return [CX + r * Math.sin(angle), CY - r * Math.cos(angle)];
}
function circle(r) {
ctx.beginPath();
ctx.arc(CX, CY, r, 0, TAU);
}
/* ---------- face ---------- */
function drawFace() {
// outer bezel
circle(482);
var bezel = ctx.createRadialGradient(CX - 140, CY - 170, 40, CX, CY, 490);
bezel.addColorStop(0, "#48506180");
bezel.addColorStop(0.45, "#262c38");
bezel.addColorStop(1, "#141821");
ctx.fillStyle = bezel;
ctx.fill();
// dial
circle(452);
var dial = ctx.createRadialGradient(CX - 110, CY - 150, 40, CX, CY, 470);
dial.addColorStop(0, "#ffffff");
dial.addColorStop(0.62, "#f2f4f9");
dial.addColorStop(1, "#d5dbe6");
ctx.fillStyle = dial;
ctx.fill();
// subtle inner rim
ctx.save();
circle(452);
ctx.lineWidth = 3;
ctx.strokeStyle = "rgba(255,255,255,0.85)";
ctx.stroke();
ctx.restore();
// ticks
for (var i = 0; i < 60; i++) {
var angle = (i / 60) * TAU;
var major = i % 5 === 0;
var inner = major ? 404 : 424;
var outer = 440;
var p1 = polar(inner, angle);
var p2 = polar(outer, angle);
ctx.beginPath();
ctx.moveTo(p1[0], p1[1]);
ctx.lineTo(p2[0], p2[1]);
ctx.lineCap = "round";
ctx.lineWidth = major ? 8 : 3;
ctx.strokeStyle = major ? "#222835" : "#a7b0c0";
ctx.stroke();
}
// numbers 1..12
ctx.save();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#1c2230";
ctx.font = "700 72px Georgia, 'Times New Roman', serif";
for (var h = 1; h <= 12; h++) {
var a = (h / 12) * TAU;
var pos = polar(348, a);
ctx.fillText(String(h), pos[0], pos[1]);
}
ctx.restore();
// brand text
ctx.save();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "rgba(28,34,48,0.45)";
ctx.font = "600 26px system-ui, sans-serif";
ctx.fillText("QUARTZ", CX, CY + 168);
ctx.restore();
}
/* ---------- hands ---------- */
function drawHand(angle, length, width, color, tail, glow) {
var tip = polar(length, angle);
var base = polar(-tail, angle);
ctx.save();
if (glow) {
ctx.shadowColor = "rgba(0,0,0,0.38)";
ctx.shadowBlur = 12;
ctx.shadowOffsetY = 4;
}
ctx.beginPath();
ctx.moveTo(base[0], base[1]);
ctx.lineTo(tip[0], tip[1]);
ctx.lineCap = "round";
ctx.lineWidth = width;
ctx.strokeStyle = color;
ctx.stroke();
ctx.restore();
}
function drawCenter() {
circle(18);
ctx.fillStyle = "#1c2230";
ctx.fill();
circle(8);
ctx.fillStyle = "#e63946";
ctx.fill();
circle(18);
ctx.lineWidth = 2;
ctx.strokeStyle = "rgba(255,255,255,0.35)";
ctx.stroke();
}
/* ---------- animation ---------- */
function frame() {
var now = new Date();
var ms = now.getMilliseconds();
var s = now.getSeconds() + ms / 1000;
var m = now.getMinutes() + s / 60;
var h = (now.getHours() % 12) + m / 60;
ctx.clearRect(0, 0, SIZE, SIZE);
drawFace();
// hour hand
drawHand((h / 12) * TAU, 250, 22, "#232936", 62, true);
// minute hand
drawHand((m / 60) * TAU, 356, 14, "#232936", 76, true);
// second hand
drawHand((s / 60) * TAU, 414, 5, "#e63946", 100, true);
drawCenter();
requestAnimationFrame(frame);
}
resize();
frame();
})();
</script>
</body>
</html>
```
### Analog Clock Mechanics & Design
This clock is driven entirely by the browser's real time, updating every animation frame for a smooth sweep.
- **Timekeeping logic** uses `new Date()` on each frame, converting hours, minutes, seconds, and milliseconds into angles. The second hand moves continuously (not ticking) because milliseconds are folded into the seconds value.
- **Canvas rendering** draws the dial once per frame at a logical 1000×1000 size, then scales to fit your screen with device pixel ratio support for crisp edges.
- **Visual hierarchy** gives the hour hand a shorter, thicker dark stroke, the minute hand a longer thinner dark stroke, and the second hand a bright red accent. The shadow and center cap add depth without distracting from readability.
---
**Optimization Tip:** You can customize the clock's size by changing the `min(85vmin, 520px)` value in the `canvas` CSS rule. To change the second hand color, look for the `#e63946` hex code in the `drawHand` call and the `drawCenter` function.