feat: Update HippieClock

- Add sections
- Sections now use ring max value
This commit is contained in:
sthag 2026-03-01 10:49:51 +01:00
parent ad135a58c1
commit 05f2ab1c0d

View file

@ -295,7 +295,8 @@ tags:
this.date = this.getTime(date); this.date = this.getTime(date);
this.options = options || { this.options = options || {
size: Math.floor(this.getSize().value * 0.9), size: Math.floor(this.getSize().value * 0.9),
h24: true h24: true,
sections: true
}; };
this.parts = this.createContext(['bkg', 'hands']); this.parts = this.createContext(['bkg', 'hands']);
this.shapes = []; this.shapes = [];
@ -332,38 +333,68 @@ tags:
part.context.clearRect(0, 0, part.element.width, part.element.height); part.context.clearRect(0, 0, part.element.width, part.element.height);
}); });
this.shapes.forEach(shape => { let ctx = undefined;
if (shape.type === 'circle') {
const radius = this.toPixelSize(shape.radius) / 2;
this.parts[0].context.fillStyle = shape.color; this.shapes
this.parts[0].context.beginPath(); .filter(item => item.type === 'circle')
this.parts[0].context.arc( .forEach((shape) => {
const radius = this.toPixelSize(shape.radius) / 2;
ctx = this.parts[0].context;
ctx.fillStyle = shape.color;
ctx.beginPath();
ctx.arc(
this.toPixelX(shape.center), this.toPixelX(shape.center),
this.toPixelY(shape.center), this.toPixelY(shape.center),
radius, radius,
0, 0,
Math.PI * 2 Math.PI * 2
); );
this.parts[0].context.fill(); ctx.fill();
} else if (shape.type === 'ring') { });
this.shapes
.filter(item => item.type === 'ring')
.forEach((shape) => {
if (this.options.sections) {
const outerRadius = this.toPixelSize(shape.radius) / 2;
const innerRadius = this.toPixelSize(shape.radius) / 2 - shape.stroke;
ctx = this.parts[0].context;
for (let i = 0; i < shape.max; i++) {
const angle = (i * (360 / shape.max) - 90) * (Math.PI / 180); // -90 to start at top
const outerX = this.toPixelX(shape.center) + outerRadius * Math.cos(angle);
const outerY = this.toPixelY(shape.center) + outerRadius * Math.sin(angle);
const innerX = this.toPixelX(shape.center) + innerRadius * Math.cos(angle);
const innerY = this.toPixelY(shape.center) + innerRadius * Math.sin(angle);
ctx.strokeStyle = 'black';
// TODO: Stärke an shape.max orientieren
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(outerX, outerY);
ctx.lineTo(innerX, innerY);
ctx.stroke();
}
}
const radius = this.toPixelSize(shape.radius) / 2 - shape.stroke / 2; const radius = this.toPixelSize(shape.radius) / 2 - shape.stroke / 2;
const start = -0.5 * Math.PI; // Start at the top const start = -0.5 * Math.PI; // Start at the top
const angle = start + (2 * Math.PI * (shape.angle / shape.max)); const angle = start + (2 * Math.PI * (shape.angle / shape.max));
ctx = this.parts[1].context;
this.parts[1].context.strokeStyle = shape.color; ctx.strokeStyle = shape.color;
this.parts[1].context.lineWidth = shape.stroke; ctx.lineWidth = shape.stroke;
this.parts[1].context.beginPath(); ctx.beginPath();
this.parts[1].context.arc( ctx.arc(
this.toPixelX(shape.center), this.toPixelX(shape.center),
this.toPixelY(shape.center), this.toPixelY(shape.center),
radius, radius,
start, start,
angle angle
); );
this.parts[1].context.stroke(); ctx.stroke();
} });
});
} }
update() { update() {
@ -484,27 +515,38 @@ tags:
addRing(id, radius, angle, max, color = 'black', center = .5, stroke = 16) { addRing(id, radius, angle, max, color = 'black', center = .5, stroke = 16) {
this.shapes.push({type: 'ring', id, radius, angle, max, color, center, stroke}); this.shapes.push({type: 'ring', id, radius, angle, max, color, center, stroke});
} }
addSection(id, radius, color = 'black', center = .5, stroke = 1) {
this.shapes.push({type: 'section', id, radius, color, center, stroke});
}
} }
const container = document.querySelector('#clock main'); const container = document.querySelector('#clock main');
const clock = new HippieClock(container); const clock = new HippieClock(container);
clock.addCircle('base', .5, 1); clock.addCircle('base', .5, 1);
// clock.addRing('seconds', .5, 1, 0, 60);
clock.draw(); clock.draw();
setInterval(() => { setInterval(() => {
clock.update(); clock.update();
}, 1000); }, 1000);
// TODO: Aktionen gehören quasi zu HippieClock
document.getElementById('tglFormat').addEventListener('click', () => { document.getElementById('tglFormat').addEventListener('click', () => {
if (clock) { if (clock) {
clock.updateOptions({h24: !clock.options.h24}) clock.updateOptions({h24: !clock.options.h24})
document.getElementById('tglFormat').textContent = clock.options.h24 ? '12-Stunden' : '24-Stunden'; document.getElementById('tglFormat').textContent = clock.options.h24 ? '12-Stunden' : '24-Stunden';
console.debug(clock);
} else { } else {
console.log('No clock available'); console.log('No clock available');
} }
}); });
document.getElementById('tglSections').addEventListener('click', () => {
if (clock) {
clock.updateOptions({sections: !clock.options.sections})
} else {
console.log('No clock available');
}
});
</script> </script>
{% endblock %} {% endblock %}