2025-11-16 12:29:19 +01:00
|
|
|
---
|
|
|
|
|
title: Clock
|
|
|
|
|
tags:
|
|
|
|
|
- demoExample
|
|
|
|
|
---
|
|
|
|
|
{% assign pageBase = "../../" -%}
|
|
|
|
|
{% assign bodyClass = "body_clock" -%}
|
|
|
|
|
{% layout "hippie/full.liquid" %}
|
|
|
|
|
|
|
|
|
|
{% block body %}
|
|
|
|
|
<main>
|
|
|
|
|
<canvas id="clock" width="512" height="512"></canvas>
|
2025-11-16 12:57:43 +01:00
|
|
|
<p>
|
|
|
|
|
<button id="toggleFormat">12-Stunden-Format</button>
|
|
|
|
|
</p>
|
2025-11-16 12:29:19 +01:00
|
|
|
</main>
|
|
|
|
|
{% endblock %}
|
|
|
|
|
|
|
|
|
|
{% block script %}
|
|
|
|
|
{{ block.super -}}
|
|
|
|
|
<script>
|
|
|
|
|
// Page script
|
|
|
|
|
const canvas = document.getElementById('clock');
|
|
|
|
|
const ctx = canvas.getContext('2d');
|
2025-11-16 12:57:43 +01:00
|
|
|
let is24HourFormat = true;
|
|
|
|
|
|
|
|
|
|
document.getElementById('toggleFormat').addEventListener('click', () => {
|
|
|
|
|
is24HourFormat = !is24HourFormat;
|
|
|
|
|
document.getElementById('toggleFormat').textContent = is24HourFormat ? '12-Stunden-Format' : '24-Stunden-Format';
|
|
|
|
|
});
|
2025-11-16 12:29:19 +01:00
|
|
|
|
2025-11-16 13:21:25 +01:00
|
|
|
function drawCircle(seconds, minutes, hours, dayOfWeek, dayOfMonth, month, daysInCurrentMonth) {
|
2025-11-16 12:35:39 +01:00
|
|
|
const centerX = canvas.width / 2;
|
|
|
|
|
const centerY = canvas.height / 2;
|
2025-11-16 13:21:25 +01:00
|
|
|
const lineWidth = 16;
|
|
|
|
|
const lineGap = 8;
|
|
|
|
|
const maxSize = canvas.width / 2 - lineWidth;
|
2025-11-16 12:35:39 +01:00
|
|
|
|
|
|
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
|
|
2025-11-16 12:43:10 +01:00
|
|
|
function drawArc(value, maxValue, radius, strokeStyle) {
|
|
|
|
|
const startAngle = -0.5 * Math.PI; // Start at the top
|
|
|
|
|
const endAngle = startAngle + (2 * Math.PI * (value / maxValue));
|
|
|
|
|
|
|
|
|
|
ctx.beginPath();
|
|
|
|
|
ctx.arc(centerX, centerY, radius, startAngle, endAngle, false);
|
|
|
|
|
ctx.lineWidth = 16;
|
|
|
|
|
ctx.strokeStyle = strokeStyle;
|
|
|
|
|
ctx.stroke();
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-16 13:21:25 +01:00
|
|
|
drawArc(seconds, 60, maxSize, 'black');
|
|
|
|
|
drawArc(minutes, 60, maxSize - lineWidth - lineGap, 'lightgrey');
|
2025-11-16 12:57:43 +01:00
|
|
|
drawArc(
|
|
|
|
|
is24HourFormat ? hours : hours % 12,
|
|
|
|
|
is24HourFormat ? 24 : 12,
|
2025-11-16 13:21:25 +01:00
|
|
|
maxSize - (lineWidth + lineGap) * 2,
|
2025-11-16 12:57:43 +01:00
|
|
|
'white'
|
|
|
|
|
);
|
2025-11-16 13:21:25 +01:00
|
|
|
drawArc(dayOfWeek, 7, maxSize - (lineWidth + lineGap) * 3, '#fad803');
|
|
|
|
|
drawArc(dayOfMonth, daysInCurrentMonth, maxSize - (lineWidth + lineGap) * 4, '#d30a51');
|
|
|
|
|
drawArc(month, 12, maxSize - (lineWidth + lineGap) * 5, '#273f8b');
|
2025-11-16 12:35:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateCircle() {
|
2025-11-16 12:43:10 +01:00
|
|
|
const currentDate = new Date();
|
|
|
|
|
const currentSeconds = currentDate.getSeconds();
|
|
|
|
|
const currentMinutes = currentDate.getMinutes();
|
|
|
|
|
const currentHours = currentDate.getHours();
|
2025-11-16 13:09:16 +01:00
|
|
|
const currentDayOfWeek = getNumericWeekday(currentDate);
|
2025-11-16 13:21:25 +01:00
|
|
|
const currentDayOfMonth = currentDate.getDate();
|
2025-11-16 13:05:25 +01:00
|
|
|
const currentMonth = currentDate.getMonth() + 1; // Get current month (0-11)
|
2025-11-16 13:21:25 +01:00
|
|
|
const daysInCurrentMonth = daysInMonth(currentMonth, currentDate.getFullYear());
|
2025-11-16 12:43:10 +01:00
|
|
|
|
2025-11-16 13:21:25 +01:00
|
|
|
drawCircle(
|
|
|
|
|
currentSeconds,
|
|
|
|
|
currentMinutes,
|
|
|
|
|
currentHours,
|
|
|
|
|
currentDayOfWeek,
|
|
|
|
|
currentDayOfMonth,
|
|
|
|
|
currentMonth,
|
|
|
|
|
daysInCurrentMonth
|
|
|
|
|
);
|
2025-11-16 12:29:19 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-16 13:09:16 +01:00
|
|
|
// TODO: Parameter für Wochenstart ergänzen
|
|
|
|
|
function getNumericWeekday(date) {
|
|
|
|
|
const weekday = date.getDay(); // 0 (Sunday) to 6 (Saturday)
|
|
|
|
|
return (weekday === 0) ? 7 : weekday;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-16 13:21:25 +01:00
|
|
|
function daysInMonth(month, year) {
|
|
|
|
|
return new Date(year, month, 0).getDate();
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-16 12:35:39 +01:00
|
|
|
updateCircle();
|
|
|
|
|
setInterval(updateCircle, 1000);
|
2025-11-16 12:29:19 +01:00
|
|
|
</script>
|
|
|
|
|
{% endblock %}
|