-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclock2.html
More file actions
215 lines (193 loc) · 6.84 KB
/
clock2.html
File metadata and controls
215 lines (193 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vintage Analog Clock</title>
<style>
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #1e1e1e;
margin: 0;
color: #ffffff;
}
canvas {
background-color: #333;
border: 10px solid #444;
box-shadow: 0px 0px 30px rgba(0, 0, 0, 0.9);
transition: all 0.5s ease;
}
.circle {
border-radius: 50%;
}
.control-bar {
margin-top: 20px;
}
.control-bar button {
padding: 10px 15px;
font-size: 16px;
cursor: pointer;
background-color: #444;
color: #fff;
border: none;
border-radius: 5px;
margin: 5px;
}
.active {
background-color: #ff4500;
}
</style>
</head>
<body>
<canvas id="vintageClock" class="circle"></canvas>
<div class="control-bar">
<button id="romanButton">Roman Numbers</button>
<button id="arabicButton">Arabic Numbers</button>
</div>
<script>
const canvas = document.getElementById('vintageClock');
const ctx = canvas.getContext('2d');
let numberStyle = 'arabic'; // Default number style
const buttons = {
'roman': document.getElementById('romanButton'),
'arabic': document.getElementById('arabicButton'),
};
function updateButtonStyles() {
for (const key in buttons) {
buttons[key].classList.remove('active');
}
buttons[numberStyle].classList.add('active');
window.location.hash = numberStyle;
}
function resizeCanvas() {
canvas.classList.add('circle');
const size = Math.min(window.innerWidth, window.innerHeight) * 0.8;
canvas.width = size;
canvas.height = size;
const radius = canvas.height / 2;
ctx.setTransform(1, 0, 0, 1, 0, 0); // Reset transformations
ctx.translate(radius, radius);
drawClock();
}
function drawClock() {
const radius = canvas.height / 2;
ctx.clearRect(-radius, -radius, canvas.width, canvas.height);
drawFace(ctx, radius);
drawMinuteMarks(ctx, radius);
drawNumbers(ctx, radius);
drawTime(ctx, radius);
}
function drawFace(ctx, radius) {
ctx.beginPath();
ctx.arc(0, 0, radius, 0, 2 * Math.PI);
ctx.fillStyle = '#333';
ctx.fill();
// Draw clock rim
ctx.strokeStyle = '#444';
ctx.lineWidth = radius * 0.07;
ctx.stroke();
// Draw clock center
ctx.beginPath();
ctx.arc(0, 0, radius * 0.05, 0, 2 * Math.PI);
ctx.fillStyle = '#ff4500';
ctx.fill();
}
function drawMinuteMarks(ctx, radius) {
for (let i = 0; i < 60; i++) {
const angle = (i * Math.PI / 30) - (Math.PI / 2);
ctx.beginPath();
const length = (i % 5 === 0) ? radius * 0.15 : radius * 0.07;
ctx.lineWidth = (i % 5 === 0) ? 2 : 1;
ctx.strokeStyle = '#aaa';
ctx.moveTo(radius * 0.9 * Math.cos(angle), radius * 0.9 * Math.sin(angle));
ctx.lineTo((radius * 0.9 - length) * Math.cos(angle), (radius * 0.9 - length) * Math.sin(angle));
ctx.stroke();
}
}
function drawNumbers(ctx, radius) {
ctx.font = radius * 0.12 + "px serif";
ctx.fillStyle = '#fff';
ctx.textBaseline = "middle";
ctx.textAlign = "center";
const romanNumerals = ["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII"];
for (let num = 1; num <= 12; num++) {
const angle = (num * Math.PI / 6) - (Math.PI / 2);
const x = radius * 0.75 * Math.cos(angle);
const y = radius * 0.75 * Math.sin(angle);
let displayNum;
switch (numberStyle) {
case 'roman':
displayNum = romanNumerals[num - 1];
break;
case 'arabic':
default:
displayNum = num.toString();
break;
}
ctx.fillText(displayNum, x, y);
}
}
function drawTime(ctx, radius) {
const now = new Date();
let hour = now.getHours();
let minute = now.getMinutes();
let second = now.getSeconds();
// Hour hand
hour = hour % 12;
hour = (hour * Math.PI / 6) + (minute * Math.PI / (6 * 60)) + (second * Math.PI / (360 * 60));
drawHand(ctx, hour, radius * 0.5, radius * 0.07);
// Minute hand
minute = (minute * Math.PI / 30) + (second * Math.PI / (30 * 60));
drawHand(ctx, minute, radius * 0.75, radius * 0.05);
// Second hand
second = (second * Math.PI / 30);
ctx.strokeStyle = '#ff4500';
drawHand(ctx, second, radius * 0.85, radius * 0.02);
}
function drawHand(ctx, pos, length, width) {
ctx.beginPath();
ctx.lineWidth = width;
ctx.lineCap = "round";
ctx.moveTo(0, 0);
ctx.rotate(pos);
ctx.lineTo(0, -length);
ctx.stroke();
ctx.rotate(-pos);
}
function updateClock() {
drawClock();
updateButtonStyles();
}
function loadFromHash() {
const hash = window.location.hash.substring(1);
switch (hash) {
case 'roman':
numberStyle = 'roman';
break;
case 'arabic':
default:
numberStyle = 'arabic';
break;
}
updateClock();
}
document.getElementById('romanButton').addEventListener('click', () => {
numberStyle = 'roman';
updateClock();
});
document.getElementById('arabicButton').addEventListener('click', () => {
numberStyle = 'arabic';
updateClock();
});
window.addEventListener('hashchange', loadFromHash);
window.addEventListener('resize', resizeCanvas);
loadFromHash();
resizeCanvas();
setInterval(updateClock, 1000);
</script>
</body>
</html>