Animations

Morph a line between shapes

Morph a polyline between flat, sine, square and sawtooth waves with one call per transition and an easing per shape.

// Demonstrates one-shot shape morph animations using Animate.shape.morph.
// A polyline cycles through signal waveforms — flat, sine, square, sawtooth —
// each transition using easing that matches the character of the target shape.

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

// ─── Layout ───────────────────────────────────────────────────────────────
const VERTICES  = 64       // resolution of the waveform
const WIDTH     = 400      // total horizontal span in canvas units
const AMPLITUDE = 80       // max vertical displacement

// ─── Pose helper ──────────────────────────────────────────────────────────
// Samples a function f(t) where t runs 0→1 across the width.
// x is fixed per vertex; only y varies between poses.
function pose(f: (t: number) => number): Point[] {
    return Array.from({ length: VERTICES }, (_, i) => {
        const t = i / (VERTICES - 1)
        return { x: t * WIDTH, y: f(t) }
    })
}

// ─── Poses ────────────────────────────────────────────────────────────────
const flat     = pose(_t => 0)
const sine     = pose(t  => -Math.sin(t * Math.PI * 4) * AMPLITUDE)
const square   = pose(t  => (Math.sin(t * Math.PI * 4) >= 0 ? -1 : 1) * AMPLITUDE)
const sawtooth = pose(t  => ((t * 4) % 1 - 0.5) * AMPLITUDE * 2)

const poses = [flat, sine, square, sawtooth, flat]

type Step = { duration: number; easing: (t: number) => number }

const steps: Step[] = [
    { duration: 1200, easing: EasingFunctions.easeInOutSine  },  // flat   → sine
    { duration:  600, easing: EasingFunctions.easeInOutCubic },  // sine   → square
    { duration:  900, easing: EasingFunctions.easeInCubic    },  // square → sawtooth
    { duration: 1000, easing: EasingFunctions.easeInOutSine  },  // sawtooth → flat
]

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

    // Centre the waveform on the canvas
    Vectors.copy([100, 200], canvas.anchor)

    // ─── Shape ────────────────────────────────────────────────────────────
    const waveform = canvas.visuals.polyline("wave", flat)

    // ─── Baseline ─────────────────────────────────────────────────────────
    canvas.visuals.segment("baseline", [0, 0], [WIDTH, 0])

    // ─── State machine ────────────────────────────────────────────────────
    // Each state morphs to the next pose, then advances on completion.
    const state = canvas.values.real(0)

    Reactive.do([state], () => {
        const idx = state.value % steps.length
        const { duration, easing } = steps[idx]

        Animate.shape.morph(waveform, poses[idx + 1], duration, {
            easing,
            onComplete: () => { state.value += 1 }
        })
    })
}

A polyline morphs through flat, sine, square and sawtooth waves in a loop. Animate.shape.morph moves its vertices to each pose, and onComplete starts the next one.

How it works

Each pose is an array of 64 points, sampled from a function once at startup: flat, sine, square, sawtooth and back to flat. All poses share the same x values, so the vertices only move up and down.

A reaction morphs the polyline to the next pose; when the morph completes it advances state, which starts the next one. Each step has its own duration and easing.

Animate.shape.morph(waveform, poses[idx + 1], duration, {
    easing,
    onComplete: () => { state.value += 1 }
})

The morph writes into the polyline's own points, so no code is needed to keep the visual in sync.

API

  • Animate.shape.morph: animates the vertices of a shape to the points of a target pose
  • Reactive.do: runs a function now and whenever its inputs change
  • canvas.visuals.polyline: the polyline that is morphed

See also

  • animations/05-breathing
  • animations/02-state-machine

API used

Also used (11)