82 lines
No EOL
2.3 KiB
Text
82 lines
No EOL
2.3 KiB
Text
---
|
|
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>
|
|
<p>
|
|
<button id="toggleFormat">12-Stunden-Format</button>
|
|
</p>
|
|
</main>
|
|
{% endblock %}
|
|
|
|
{% block script %}
|
|
{{ block.super -}}
|
|
<script>
|
|
// Page script
|
|
const canvas = document.getElementById('clock');
|
|
const ctx = canvas.getContext('2d');
|
|
let is24HourFormat = true;
|
|
|
|
document.getElementById('toggleFormat').addEventListener('click', () => {
|
|
is24HourFormat = !is24HourFormat;
|
|
document.getElementById('toggleFormat').textContent = is24HourFormat ? '12-Stunden-Format' : '24-Stunden-Format';
|
|
});
|
|
|
|
function drawCircle(seconds, minutes, hours, dayOfWeek, month) {
|
|
const centerX = canvas.width / 2;
|
|
const centerY = canvas.height / 2;
|
|
const radius = 128;
|
|
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
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();
|
|
}
|
|
|
|
drawArc(seconds, 60, radius, 'black');
|
|
drawArc(minutes, 60, radius - 20, 'lightgrey');
|
|
drawArc(
|
|
is24HourFormat ? hours : hours % 12,
|
|
is24HourFormat ? 24 : 12,
|
|
radius - 40,
|
|
'white'
|
|
);
|
|
drawArc(dayOfWeek, 7, radius - 60, '#fad803');
|
|
drawArc(month, 12, radius - 80, '#d30a51');
|
|
}
|
|
|
|
function updateCircle() {
|
|
const currentDate = new Date();
|
|
const currentSeconds = currentDate.getSeconds();
|
|
const currentMinutes = currentDate.getMinutes();
|
|
const currentHours = currentDate.getHours();
|
|
const currentDayOfWeek = getNumericWeekday(currentDate);
|
|
const currentMonth = currentDate.getMonth() + 1; // Get current month (0-11)
|
|
|
|
drawCircle(currentSeconds, currentMinutes, currentHours, currentDayOfWeek, currentMonth);
|
|
}
|
|
|
|
// TODO: Parameter für Wochenstart ergänzen
|
|
function getNumericWeekday(date) {
|
|
const weekday = date.getDay(); // 0 (Sunday) to 6 (Saturday)
|
|
return (weekday === 0) ? 7 : weekday;
|
|
}
|
|
|
|
updateCircle();
|
|
setInterval(updateCircle, 1000);
|
|
</script>
|
|
{% endblock %} |