Staggered animation of many objects
Deal seven cards into a fan one after another and fold them back from the outside in: staggered 2D animation in TypeScript.
// Demonstrates staggered animation of multiple independent objects.
// Cards fly from a deck to a fanned hand one by one. Each card is a layer;
// Animate.pin.easedAll moves and turns all anchor pins as one animation,
// with stagger for the delays. Clicking again collects them back.
import {VisQuill, Animate, Boxes, Pins, Svg, EasingFunctions, type RvgAnimation} from "@visquill/visquill-gdk"
import './styles.css'
// ─── Layout ───────────────────────────────────────────────────────────────
const CARD_W = 52, CARD_H = 74
const CARD_COUNT = 7
const DECK_X = 250, DECK_Y = 330
// Fan: cards sit on a circular arc. ARC_X/Y is the arc centre far below
// the visible cards, so they appear to fan upward from a shared hand point.
const ARC_X = 250, ARC_Y = 610, ARC_R = 370, SPREAD = 0.65
// The pin of card i in the fan: on the circle, turned along its tangent.
// Angles start at 3 o'clock, so -π/2 is the top of the circle.
function fanPin(i: number) {
const t = i / (CARD_COUNT - 1)
const angle = -Math.PI / 2 + (t - 0.5) * SPREAD
return Pins.placeAndAlignOnCircle([ARC_X, ARC_Y], ARC_R, angle)
}
// The pin of the deck: upright, at the deck position.
const DECK_PIN = Pins.withOrientation([DECK_X, DECK_Y], [1, 0])
export function run(div: HTMLDivElement) {
const rvg = VisQuill.create(div, "demos-")
const canvas = rvg.layer("card-deal-")
// ─── Deck shadow (static cards for depth illusion) ─────────────────
for (let i = 3; i >= 1; i--) {
canvas.visuals.box("deck-shadow",
[DECK_X - CARD_W / 2 + i, DECK_Y - CARD_H / 2 - i * 2],
[DECK_X + CARD_W / 2 + i, DECK_Y + CARD_H / 2 - i * 2])
}
// ─── Cards ─────────────────────────────────────────────────────────
// Each card is a layer with the card drawn around its origin. The layer's
// anchor is a pin (position + orientation), so animating the anchor moves
// and turns the card together.
const cards = Array.from({length: CARD_COUNT}, () => {
const layer = canvas.layer()
Pins.copy(DECK_PIN, layer.anchor)
layer.visuals.box("card", [-CARD_W / 2, -CARD_H / 2], [CARD_W / 2, CARD_H / 2])
return layer.anchor
})
// ─── Deal / Collect ────────────────────────────────────────────────
// One easedAll call per click moves all cards; stagger starts them one
// after another, in the order of the array. A new click replaces the
// running animation (cancel also drops cards that have not started yet),
// and the cards turn around from where they are.
// Cards are created 0→6 so card 6 sits on top (SVG z-order): deal the
// top card first. Collect from the outside inward, the middle card last.
const dealOrder = [6, 5, 4, 3, 2, 1, 0]
const collectOrder = [0, 6, 1, 5, 2, 4, 3]
let dealt = false
let animation: RvgAnimation | null = null
function deal() {
animation?.cancel()
animation = Animate.pin.easedAll(
dealOrder.map(i => cards[i]),
dealOrder.map(i => fanPin(i)),
520,
{stagger: 130, easing: EasingFunctions.easeOutCubic},
)
}
function collect() {
animation?.cancel()
animation = Animate.pin.easedAll(
collectOrder.map(i => cards[i]),
collectOrder.map(() => DECK_PIN),
420,
{stagger: 50, easing: EasingFunctions.easeInCubic},
)
}
// ─── Button: a box with a label on it ──────────────────────────────
const button = canvas.visuals.box("button", [200, 406], [300, 430])
const label = canvas.text.labelAt("Deal", Boxes.center(button), "button-label")
Svg.get(button).addEventListener("click", () => {
dealt ? collect() : deal()
dealt = !dealt
label.value = dealt ? "Collect" : "Deal"
})
}Seven cards are dealt from a deck into a fan, one after another, and collected back. Each card is a
layer whose anchor pin is moved and turned with Animate.pin.eased; the fan positions come from
Pins.placeAndAlignOnCircle.
How it works
Each card is a layer drawn around its origin; its anchor is a pin (position and orientation). Animate.pin.easedAll animates all anchors with one call, so every card moves and turns. stagger starts them one after another, in array order:
animation?.cancel()
animation = Animate.pin.easedAll(
dealOrder.map(i => cards[i]),
dealOrder.map(i => fanPin(i)),
520,
{stagger: 130, easing: EasingFunctions.easeOutCubic},
)
Dealing starts with the last card created, which is drawn on top of the deck. Collecting starts with the two outer cards and ends with the middle one. A click during a move cancels the running animation, including cards that have not started yet; the cards turn around where they are.
The fan positions come from Pins.placeAndAlignOnCircle: a pin on a large circle below the cards, turned along the circle's tangent.
API
Animate.pin.easedAll: animates many pins (position and orientation), one handle for allRvgEasedOptions.stagger: delay between the starts of consecutive pinsRvgAnimation.cancel: stops the animation where it isPins.placeAndAlignOnCircle: a pin on a circle at an angle, turned along the tangent
Notes
- Angles start at 3 o'clock, so
-Math.PI / 2is the top of the circle.
See also
animations/02-state-machineanimations/14-stagger




