import { VisQuill, Circles, 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. One turn of the
// second hand takes 60 s, of the minute hand 3600 s, of the hour hand 43200 s.
function now(): number {
  const date = new Date();
  return date.getTime() / 1000 - date.getTimezoneOffset() * 60;
}

// A hand is a layer with a segment along its x-axis, from 0 to the length
// of the hand. The layer's anchor is a pin: whenever the angle changes, a
// copy of the 12 o'clock pin, turned by the angle, is written into it, and
// the layer turns with it.
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 4: the hands.
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);
  }

  // Each hand has its angle as a reactive value: the part of a full turn
  // (2π) that has passed, clockwise from 12.
  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 region-hands", 65, hourAngle);
  hand(canvas, twelve, "hand minute region-hands", 100, minuteAngle);
  hand(canvas, twelve, "hand second region-hands", 112, secondAngle);
  canvas.visuals.point("cap region-hands", { x: 280, y: 170 });
}
/* 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 4 of 6

Hands

Three angles as reactive values, and a layer for each hand that turns with its angle.

01

The time in seconds

now returns the local time as a number of seconds. The second hand makes one turn every 60 seconds, the minute hand every 3600, the hour hand every 43200 (12 hours).

02

Angles as values

canvas.values.real creates a reactive number. Everything that depends on it is updated when it changes. Here it holds the angle of a hand: the time divided by the length of one turn, times 2π.

03

A hand is a layer

A layer is a group with its own origin, its anchor. The anchor is a pin: its position is the origin of the layer, its direction is the layer's x-axis.

The hand is a segment on the x-axis of its layer, from 0 to the length of the hand, and is never changed. Reactive.do runs whenever the angle changes and writes a copy of twelve, turned by the angle, into the anchor. The layer turns, and the hand with it.

04

Three hands

The hands share the class hand; hour, minute and second set width and color. The cap is a point that covers the ends of the hands.