import { VisQuill, Animate, Circles, EasingFunctions, Pins, Points, Reactive, 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 5: the clock runs.
export function run(div: HTMLDivElement) {
  const rvg = VisQuill.create(div, "clock-");
  const canvas = rvg.canvas;

  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);

  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 });

  // Once a second, each angle is animated to the angle of the current time.
  // The time only grows, so the second hand moves on past 12 and does not
  // turn back. If the browser pauses the timer (in a hidden tab), the next
  // tick catches up.
  const timer = 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);
  }, 1000);

  // The timer does not belong to the graphic. run returns a function that
  // stops it when the demo is removed from the page.
  return () => clearInterval(timer);
}
/* Each class is the prefix "clock-" plus the name used in demo.ts. */

.clock-face {
  fill: #faf6ef;
  stroke: #1c1a17;
  stroke-width: 3px;
}

/* All ticks are thin; the class "long" makes every fifth one thicker. */
.clock-tick { stroke: #1c1a17; stroke-width: 1px; }
.clock-long { stroke-width: 3px; }

.clock-number {
  fill: #1c1a17;
  font: 500 16px Inter, system-ui, sans-serif;
  text-anchor: middle;
  dominant-baseline: middle;
  user-select: none;
}

.clock-hand { stroke: #1c1a17; stroke-linecap: round; }
.clock-hour { stroke-width: 6px; }
.clock-minute { stroke-width: 4px; }
.clock-second { stroke: #c0503f; stroke-width: 2px; }
.clock-cap { r: 5px; fill: #1c1a17; }

/* Part 6: the timer. The arc is a thick, light line just inside the rim. */
.clock-timer-arc {
  fill: none;
  stroke: #e9c48f;
  stroke-width: 14px;
}

.clock-knob {
  r: 10px;
  fill: #d8913a;
  stroke: #faf6ef;
  stroke-width: 2px;
  cursor: grab;
}
.clock-knob:active { cursor: grabbing; }

.clock-readout {
  fill: #1c1a17;
  font: 500 14px "JetBrains Mono", ui-monospace, monospace;
  text-anchor: middle;
  dominant-baseline: middle;
  user-select: none;
}

Build a clock · Part 5 of 6

Ticking

Once a second, the angles are animated to the current time.

01

Once a second

setInterval runs the function every second. It reads the time and animates each angle to its new value with Animate.real.eased: from the current value to the target, in 300 milliseconds.

The time keeps growing, and so do the angles. At 12 the second hand moves on to the next tick instead of turning back by a full turn.

02

Easing

An easing function shapes the animation. easeOutBack moves slightly past the target and then back, which looks like the jump of a mechanical second hand. The minute and hour hands use the default easing.

03

Stopping the timer

The timer is not part of the graphic, so the graphic cannot stop it. run returns a function that stops it. This page calls it when the demo is removed.