better commentary

This commit is contained in:
waltem01 2024-03-08 10:25:20 +01:00
parent d60800bc3f
commit e672e854c9

View File

@ -1,16 +1,21 @@
<script lang="ts"> <script lang="ts">
import { onMount } from 'svelte'; import { onMount } from 'svelte';
// Data structure to keep track of clock
interface Clock { interface Clock {
// Clock circle radius
body: number; body: number;
// Clock hand radii
hands: { hands: {
minute: number; minute: number;
hour: number; hour: number;
}; };
// Clock time percentages
time: { time: {
minute: number; minute: number;
hour: number; hour: number;
}; };
// Clock hand positions
positions?: { positions?: {
minute: { minute: {
x: number; x: number;
@ -21,9 +26,11 @@
y: number; y: number;
}; };
}; };
// Clock update speed in seconds
speed: number; speed: number;
} }
//.System variables
const clock: Clock = { const clock: Clock = {
body: 50, body: 50,
hands: { hands: {
@ -39,8 +46,8 @@
const TWO_PI = 2 * Math.PI; const TWO_PI = 2 * Math.PI;
const HALF_PI = Math.PI / 2; const HALF_PI = Math.PI / 2;
// (x = r * cos(a), y = r * sin(a))
function calcPos(time: 'minute' | 'hour', operation: 'sin' | 'cos') { function calcPos(time: 'minute' | 'hour', operation: 'sin' | 'cos') {
// Calculate (x = r * cos(a), y = r * sin(a))
const radius = clock.hands[time]; const radius = clock.hands[time];
const angle = TWO_PI * clock.time[time]; const angle = TWO_PI * clock.time[time];
const relative = Math[operation](angle - HALF_PI); const relative = Math[operation](angle - HALF_PI);
@ -52,10 +59,12 @@
const current = new Date(); const current = new Date();
const time = current.toTimeString(); const time = current.toTimeString();
const [hour, minute, _] = time.split(' ')[0].split(':'); const [hour, minute, _] = time.split(' ')[0].split(':');
// Update time variables
clock.time = { clock.time = {
hour: Number(hour) / 12, hour: Number(hour) / 12,
minute: Number(minute) / 60 minute: Number(minute) / 60
}; };
// Update hand positions
clock.positions = { clock.positions = {
minute: { minute: {
x: calcPos('minute', 'cos'), x: calcPos('minute', 'cos'),
@ -74,8 +83,10 @@
}); });
</script> </script>
<!-- Virtual clock display using SVG -->
<div class="bg-black"> <div class="bg-black">
<svg class="text-white" viewBox="0 0 100 100"> <svg class="text-white" viewBox="0 0 100 100">
<!-- Clock body -->
<circle class="stroke-current" cx="50" cy="50" r={clock.body} fill="none" stroke-width="2" /> <circle class="stroke-current" cx="50" cy="50" r={clock.body} fill="none" stroke-width="2" />
<line <line
class="stroke-current" class="stroke-current"
@ -85,6 +96,7 @@
y2={clock.positions?.minute.y ?? 0} y2={clock.positions?.minute.y ?? 0}
stroke-width="1" stroke-width="1"
/> />
<!-- Clock hours hand -->
<line <line
class="stroke-current" class="stroke-current"
x1="50" x1="50"