import { VisQuill, Circles, Pins } from "@visquill/visquill-gdk";
import "./styles.css";

// Part 2: tick marks.
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 is a point with a direction. This one sits at the center and
  // points to 12. It is never changed; the clock only uses turned copies.
  const twelve = Pins.withOrientation([280, 170], [0, -1]);

  // A copy of the pin, turned clockwise by a fraction of a full turn:
  // 0.25 points to 3, 0.5 to 6.
  const at = (fraction: number) => Pins.rotateBy(twelve, fraction * 2 * Math.PI);

  // One tick per minute, from a point inside the rim to the rim. Every
  // fifth tick is longer and has the extra class "long".
  for (let i = 0; i < 60; i++) {
    const long = i % 5 === 0;
    const pin = at(i / 60);
    canvas.visuals.segment(long ? "tick long region-ticks" : "tick region-ticks",
      Pins.moveForward(pin, long ? 116 : 123),
      Pins.moveForward(pin, 130));
  }
}
/* 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 2 of 6

Tick marks

A pin that points to 12, and sixty turned copies of it for the ticks.

01

A pin that points to 12

A pin is a point with a direction. Pins.withOrientation creates one at the center of the clock, pointing up, to 12.

Pins.rotateBy returns a copy of a pin, turned clockwise by an angle. A full turn is 2π, so at(0.25) points to 3 and at(0.5) to 6. The pin twelve itself never changes; everything else on the clock is measured from it.

02

The ticks

Pins.moveForward moves a pin along its direction. For each minute, the turned pin is moved to the rim (130 pixels) and to a point a little inside it; the tick is the segment between the two.

The ticks never change, so they are computed once when they are created.

03

Two kinds of ticks

All ticks have the class tick. Every fifth tick also has the class long, which makes it thicker.