Animations

Animated analog clock

An SVG analog clock that ticks in real time, with an overshooting second hand and smoothly gliding minute and hour hands.

// A clock whose hands are positions on circles. Each hand is driven by one
// reactive Real, the fraction of a full turn, which Animate.real.eased moves
// forward every second.

import {VisQuill, Animate, Reactive, Circles, Segments, EasingFunctions, type Real} from "@visquill/visquill-gdk"
import './styles.css'

// ─── Layout ───────────────────────────────────────────────────────────────
const CX = 240, CY = 200
const FACE_R = 120
const HR_LEN = 58, MIN_LEN = 85, SEC_LEN = 100
const TAIL_LEN = 15

// A circle through 12, 3 and 6 o'clock. Circles.pointAt(dial, 0) is then
// 12 o'clock and ratios run clockwise, so a fraction of a turn is directly a
// position on the dial. Ratios outside [0, 1] wrap around.
function dial(radius: number) {
    return Circles.circle3P([CX, CY - radius], [CX + radius, CY], [CX, CY + radius])
}

export function run(div: HTMLDivElement) {
    const rvg = VisQuill.create(div, "demos-")
    const canvas = rvg.layer("clock-")

    // ─── Face ─────────────────────────────────────────────────────────────
    Circles.circleAt([CX, CY], FACE_R, canvas.visuals.circle("face"))

    // Tick marks: major at 12/3/6/9, minor elsewhere
    const rim = dial(FACE_R)
    const majorStart = dial(FACE_R - 16)
    const minorStart = dial(FACE_R - 8)
    for (let i = 0; i < 12; i++) {
        const major = i % 3 === 0
        canvas.visuals.segment(
            major ? "tick-major" : "tick-minor",
            Circles.pointAt(major ? majorStart : minorStart, i / 12),
            Circles.pointAt(rim, i / 12)
        )
    }

    // ─── Hands ────────────────────────────────────────────────────────────
    // A hand runs from a short tail opposite its tip (ratio + 0.5) to the tip.
    function hand(style: string, length: number, turn: Real) {
        const segment = canvas.visuals.segment(style)
        const tip = dial(length)
        const tail = dial(TAIL_LEN)
        Reactive.do([turn], () => {
            Segments.segment(Circles.pointAt(tail, turn.value + 0.5), Circles.pointAt(tip, turn), segment)
        })
    }

    const now = new Date()
    const s = now.getSeconds()
    const m = now.getMinutes() + s / 60
    const h = (now.getHours() % 12) + m / 60

    const secTurn = canvas.values.real(s / 60)
    const minTurn = canvas.values.real(m / 60)
    const hrTurn  = canvas.values.real(h / 12)

    hand("hour", HR_LEN, hrTurn)
    hand("minute", MIN_LEN, minTurn)
    hand("second", SEC_LEN, secTurn)

    // Centre cap on top of the hands
    canvas.visuals.point("cap", {x: CX, y: CY})

    // ─── Tick ─────────────────────────────────────────────────────────────
    // Every second each hand moves on by its share of a turn. The values only
    // grow, so the second hand never runs backwards when it passes 12.
    setInterval(() => {
        Animate.real.eased(secTurn, secTurn.value + 1 / 60, 300, {
            easing: EasingFunctions.easeOutBack
        })
        Animate.real.eased(minTurn, minTurn.value + 1 / 3600, 900, {
            easing: EasingFunctions.easeInOutSine
        })
        Animate.real.eased(hrTurn, hrTurn.value + 1 / 43200, 900, {
            easing: EasingFunctions.easeInOutSine
        })
    }, 1000)
}

An analog clock whose hands are driven by animated numbers. Once a second Animate.real.eased moves each hand's value on, and a reaction places the hand on the dial with Circles.pointAt; the second hand overshoots slightly with easeOutBack.

How it works

Each hand has one Real: how far it has turned, as a fraction of a full turn. A reaction places the hand on a circle at that fraction. Once a second, Animate.real.eased moves each value on by its share of a turn, and the reaction redraws the hand on every frame.

Reactive.do([turn], () => {
    Segments.segment(Circles.pointAt(tail, turn.value + 0.5), Circles.pointAt(tip, turn), segment)
})
// …
Animate.real.eased(secTurn, secTurn.value + 1 / 60, 300, {
    easing: EasingFunctions.easeOutBack
})

dial() builds each circle with Circles.circle3P through 12, 3 and 6 o'clock, so ratio 0 is 12 o'clock and ratios run clockwise. Ratios above 1 wrap around. The values only grow, so the second hand never runs backwards at 12.

API

  • Animate.real.eased: animates a Real to a target value
  • Reactive.do: runs a function now and whenever its inputs change
  • Circles.pointAt: the point at a ratio along a circle
  • Circles.circle3P: the circle through three points, starting at the first
  • Segments.segment: writes a segment into an existing visual

See also

  • animations/01-easing
  • geometry/circles-03-sampling

API used

Also used (11)