import { VisQuill, Animate, Arcs, Circles, EasingFunctions, Pins, Points, Reactive, Vectors, type Pin, type Real, type RvgGroup } from "@visquill/visquill-gdk";
import "./styles.css";
// The current local time in seconds, counted from 1970.
function now(): number {
const date = new Date();
return date.getTime() / 1000 - date.getTimezoneOffset() * 60;
}
// A hand: a layer that turns with its angle.
function hand(canvas: RvgGroup, twelve: Pin, style: string, length: number, angle: Real) {
const layer = canvas.layer();
layer.visuals.segment(style, [0, 0], [length, 0]);
Reactive.do([angle], () => {
Pins.rotateBy(twelve, angle, layer.anchor);
});
}
// Part 6: a timer for up to 60 seconds.
export function run(div: HTMLDivElement) {
const rvg = VisQuill.create(div, "clock-");
const canvas = rvg.canvas;
canvas.handles.defaultZIndex = 1;
const face = canvas.visuals.circle("face");
Circles.circleAt([280, 170], 130, face);
// A pin at the center that points to 12, and turned copies of it.
const twelve = Pins.withOrientation([280, 170], [0, -1]);
const at = (fraction: number) => Pins.rotateBy(twelve, fraction * 2 * Math.PI);
// The timer's arc and readout. They are created before the ticks and
// hands, so they are drawn below them.
const arc = canvas.visuals.arc("timer-arc region-arc");
const readout = canvas.text.label("", "readout region-readout");
Points.copy([280, 225], readout);
for (let i = 0; i < 60; i++) {
const long = i % 5 === 0;
const pin = at(i / 60);
canvas.visuals.segment(long ? "tick long" : "tick", Pins.moveForward(pin, long ? 116 : 123), Pins.moveForward(pin, 130));
}
for (let h = 1; h <= 12; h++) {
const label = canvas.text.label(String(h), "number");
Points.copy(Pins.moveForward(at(h / 12), 100), label);
}
const t = now();
const hourAngle = canvas.values.real((t / 43200) * 2 * Math.PI);
const minuteAngle = canvas.values.real((t / 3600) * 2 * Math.PI);
const secondAngle = canvas.values.real((t / 60) * 2 * Math.PI);
hand(canvas, twelve, "hand hour", 65, hourAngle);
hand(canvas, twelve, "hand minute", 100, minuteAngle);
hand(canvas, twelve, "hand second", 112, secondAngle);
canvas.visuals.point("cap", { x: 280, y: 170 });
// The knob is a handle. It starts at 12, which means 0 seconds.
const knob = canvas.handles.disk("knob region-knob", Pins.moveForward(twelve, 130));
let seconds = 0;
// The arc starts at 12. Arcs.arcAt takes the start as an angle measured
// clockwise from the x-axis; Vectors.angle gives it for the 12 o'clock direction.
const start = Vectors.angle(Pins.getOrientation(twelve), true);
// When the knob moves, it is put back onto the rim. The angle from 12 to
// the knob, clockwise, gives the seconds (a full turn is 60), the arc and
// the readout.
Reactive.do([knob], () => {
Circles.closestPoint(face, knob, knob);
const toKnob = Vectors.direction([280, 170], knob);
const angle = Vectors.angleTo(Pins.getOrientation(twelve), toKnob, true).value;
seconds = Math.round((angle / (2 * Math.PI)) * 60) % 60;
Arcs.arcAt([280, 170], 122, start, seconds > 0 ? angle : 0, arc);
readout.value = seconds > 0 ? `${seconds} s` : "";
});
const ticker = setInterval(() => {
const t = Math.round(now());
Animate.real.eased(secondAngle, (t / 60) * 2 * Math.PI, 300, {
easing: EasingFunctions.easeOutBack,
});
Animate.real.eased(minuteAngle, (t / 3600) * 2 * Math.PI, 300);
Animate.real.eased(hourAngle, (t / 43200) * 2 * Math.PI, 300);
// The countdown moves the knob back by one second, but not while the
// user holds it. The knob's reaction then updates the arc and the
// readout, the same way as when it is dragged.
if (seconds > 0 && !knob.active.value) {
const target = Pins.moveForward(at((seconds - 1) / 60), 130);
Animate.point.eased(knob, target, 300, { easing: EasingFunctions.easeOutCubic });
}
}, 1000);
return () => clearInterval(ticker);
}