-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.html
More file actions
152 lines (138 loc) · 4.32 KB
/
clock.html
File metadata and controls
152 lines (138 loc) · 4.32 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script
src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.11.2/p5.min.js"
integrity="sha512-1YMgn4j8cIL91s14ByDGmHtBU6+F8bWOMcF47S0cRO3QNm8SKPNexy4s3OCim9fABUtO++nJMtcpWbINWjMSzQ=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
></script>
<title>Clock with Toggle</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #a887e0;
}
.toggle-container {
position: absolute;
top: 20px;
left: 20px;
background: #ffffff;
padding: 10px;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<div class="toggle-container">
<label>
<input type="checkbox" id="toggle-smooth" />
Smooth Second Hand
</label>
</div>
<script>
let centerX, centerY;
let diameter, radius;
let smoothSecondHand = false; // Default to ticking
let bgColor;
function setup() {
createCanvas(windowWidth, windowHeight);
setupClock();
// Add event listener for the toggle switch
const toggle = document.getElementById("toggle-smooth");
toggle.addEventListener("change", () => {
smoothSecondHand = toggle.checked;
});
}
function setupClock() {
centerX = width / 2;
centerY = height / 2;
diameter = min(width, height) - 200;
radius = diameter / 2;
bgColor = color(168, 135, 224);
}
function draw() {
background(bgColor);
drawClockBody();
drawHands();
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
setupClock();
}
function drawClockBody() {
stroke(255);
strokeWeight(3);
noFill();
circle(centerX, centerY, diameter);
drawHourMarkers();
drawMinuteMarkers();
}
function drawHourMarkers() {
stroke(255);
strokeWeight(8);
const deltaTheta = TWO_PI / 12;
for (let i = 0; i < 12; i++) {
const theta = i * deltaTheta;
const x1 = centerX + radius * 0.9 * cos(theta);
const y1 = centerY + radius * 0.9 * sin(theta);
const x2 = centerX + radius * 0.8 * cos(theta);
const y2 = centerY + radius * 0.8 * sin(theta);
line(x1, y1, x2, y2);
}
}
function drawMinuteMarkers() {
stroke(200);
strokeWeight(4);
const deltaTheta = TWO_PI / 60;
for (let i = 0; i < 60; i++) {
const theta = i * deltaTheta;
const x1 = centerX + radius * 0.9 * cos(theta);
const y1 = centerY + radius * 0.9 * sin(theta);
const x2 = centerX + radius * 0.85 * cos(theta);
const y2 = centerY + radius * 0.85 * sin(theta);
line(x1, y1, x2, y2);
}
}
function drawHands() {
const now = new Date();
// Seconds
const seconds = now.getSeconds();
const millis = now.getMilliseconds();
let secondTheta;
if (smoothSecondHand) {
secondTheta = map(
seconds * 1000 + millis,
0,
60000,
-HALF_PI,
TWO_PI - HALF_PI
);
} else {
secondTheta = map(seconds, 0, 60, -HALF_PI, TWO_PI - HALF_PI);
}
drawHand(secondTheta, radius * 0.8, color(235, 119, 52), 2);
// Minutes
const minutes = now.getMinutes() + seconds / 60;
const minuteTheta = map(minutes, 0, 60, -HALF_PI, TWO_PI - HALF_PI);
drawHand(minuteTheta, radius * 0.6, color(93, 27, 207), 5);
// Hours
const hours = (now.getHours() % 12) + minutes / 60;
const hourTheta = map(hours, 0, 12, -HALF_PI, TWO_PI - HALF_PI);
drawHand(hourTheta, radius * 0.5, color(255), 8);
}
function drawHand(theta, length, strokeColor, weight) {
stroke(strokeColor);
strokeWeight(weight);
const x = centerX + length * cos(theta);
const y = centerY + length * sin(theta);
line(centerX, centerY, x, y);
}
</script>
</body>
</html>