Animations

Animated text transitions

Animate an SVG label through a sequence of strings, character by character, with duration scaled to the text length.

import {Animate, Boxes, Reactive, RvgTextDurationMode, Svg, VisQuill} from "@visquill/visquill-gdk"
import './styles.css'

// ─── Configuration ──────────────────────────────────────────────────────────

const strings = [
    "Hello World!",
    "This is VisQuill",
    "A Graphics Development Kit",
    "For Interactive and Animated",
    "Data Visualizations"];

/**
 * Creates an interactive text animation demonstration.
 * The text transitions sequentially through different strings when the button is clicked.
 *
 * @param div - The div element that will contain the graphic
 */
export function run(div: HTMLDivElement) {
    const rvg = VisQuill.create(div, "demos-")
    const canvas = rvg.layer("animations-text-")

    // ─── Build ──────────────────────────────────────────────────────────────

    // Text label that displays the current string
    const label = canvas.text.labelAt("", [150, 100], "label")

    // Reactive value to track the current string index (-1 means not started)
    const currentIndex = canvas.values.real(-1)

    // React to changes in currentIndex and animate the text to the next string
    Reactive.do([currentIndex], () => {
        Animate.string.animate(label, strings[currentIndex.value], 100, {
            onComplete: () => Animate.after(500,advance),
            durationMode: RvgTextDurationMode.PerCharacter,
            skipWhitespace: true
        })
    }, false)


    // Button to trigger text sequence animation: a box with a label on it
    const button = canvas.visuals.box("button", [20, 20], [120, 46])
    canvas.text.labelAt("Start", Boxes.center(button), "button-label")
    Svg.get(button).addEventListener("click", () => {
        currentIndex.value = 0
    })

    function advance(){
        // Advance to next string if there are more strings to display
        if (currentIndex.value < strings.length - 1) {
            currentIndex.value += 1
        }
    }
}

A label changes through a list of strings, character by character, with Animate.string.animate. Longer strings take longer, and Animate.after adds a pause before the next one.

How it works

currentIndex selects the string to show. It starts at -1, and the reaction is registered with false as its last argument, so it does not run until the Start button sets the index to 0. From then on, every change of the index animates the label to the next string:

Animate.string.animate(label, strings[currentIndex.value], 100, {
    onComplete: () => Animate.after(500,advance),
    durationMode: RvgTextDurationMode.PerCharacter,
    skipWhitespace: true
})

With PerCharacter, the 100 ms apply to each character, so longer strings take longer. skipWhitespace spends no time on spaces. When a string is complete, Animate.after waits 500 ms and calls advance, which moves to the next string until the last one is shown.

API

  • Animate.string.animate: animates a text from its current string to a new one
  • Animate.after: calls a function after a delay
  • RvgTextDurationMode.PerCharacter: the duration is per character, not for the whole text
  • Reactive.do: runs a function whenever its inputs change

See also

  • animations/02-state-machine

API used

Also used (9)