Tween, sequence and stagger SVG shapes
Tween a shape's position and colour, sequence three tweens, stagger five dots from one timeline.
Apache-2.0Runs on this page
Package: @awacloud/graphic-anim
Real SVG shapes animated three ways through @awacloud/graphic-anim: a
single eased tween on position and colour, a timeline that chains three
tweens, and a timeline that staggers one tween over a group of dots.
Uses @awacloud/graphic-anim (Apache-2.0). This example's code is Apache-2.0.
When to use it
Any interface motion on SVG: a marker that travels and changes colour, a
list that fills in one item after another, a group that ripples. For a
rate tied to real time, see graphic-anim/clock.
Copy-paste steps
npm install @awacloud/fw @awacloud/graphic-anim- Serve
node_modules/@awacloud/fw/src/at/vendor/fw/andnode_modules/@awacloud/graphic-anim/src/at/vendor/graphic-anim/(node serve.jsfrom the examples root does both; any static server works). - Copy
index.html,main.jsandstyle.cssnext to each other and openindex.htmlthrough that server. The../../_style/examples.csslink is optional: copy that file along, or delete the link.
What it shows
fw.runtime.registerAllDeep(graphicModules)thenfw.runtime.resolve('graphic.anim')givessvgBuild,tween,timelineandschedulerin one object; no bundler is involved.svgBuild.build(desc)makes a real SVG element from a node description. The page keeps each element it builds and hands it to the tweens by reference: nogetElementByIdper tick.tween({ target, property, from, to, duration, easing })animates one property; a number (cx) and a colour (fill) go through the same call.Timeline#sequence(specs)plays the tweens back to back, andTimeline#stagger(specs, delay)starts each onedelayms after the previous, all from a single timeline.- A tween or timeline stays on the shared scheduler until it finishes or is
removed:
dispose()callsscheduler.removeon each, which is also what Restart does before it rebuilds the stage. - Limits: the page offers no pause or scrub of the running animation, only Restart.
Code
main.js
// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { graphicModules } from '@awacloud/graphic-anim';
fw.runtime.registerAllDeep(graphicModules);
const { svgBuild, tween: tweenModule, timeline, scheduler } = fw.runtime.resolve('graphic.anim');
/** Build a shape from a node description and add it to the mount; the page keeps the reference, no lookup per tick. */
const shape = (/** @type {SVGElement} */ mount, /** @type {object} */ desc) => mount.appendChild(svgBuild.build(desc));
/**
* Three ways to move shapes, each on its own row of the stage. Returns what to stop on dispose.
* @param {SVGElement} mount
* @returns {{ dispose(): void }}
*/
export function mountEngine(mount) {
const running = [];
// 1. One tween moves the dot and a second one recolours it: eased, in parallel.
const dot = shape(mount, { tag: 'circle', attrs: { cx: 10, cy: 20, r: 8, fill: '#3366ff' } });
const move = { target: dot, duration: 1400, easing: 'easeInOutQuad' };
running.push(tweenModule.tween({ ...move, property: 'cx', from: 10, to: 190 }));
running.push(tweenModule.tween({ ...move, property: 'fill', from: '#3366ff', to: '#ff6633' }));
// 2. A timeline runs three tweens one after the other.
const bars = [0, 1, 2].map((i) => shape(mount, {
tag: 'rect', attrs: { x: 10, y: 45 + i * 16, width: 14, height: 10, fill: '#33aa66' },
}));
const chain = new timeline.Timeline();
chain.sequence(bars.map((target) => ({
target, property: 'x', from: 10, to: 170, duration: 500, easing: 'easeInOutQuad',
})));
chain.play();
running.push(chain);
// 3. Another timeline staggers the same tween over five dots, 90 ms apart.
const dots = [0, 1, 2, 3, 4].map((i) => shape(mount, {
tag: 'circle', attrs: { cx: 10 + i * 20, cy: 110, r: 5, fill: '#8855dd' },
}));
const wave = new timeline.Timeline();
wave.stagger(dots.map((target) => ({
target, property: 'cy', from: 110, to: 80, duration: 600, easing: 'easeOutQuad',
})), 90);
wave.play();
running.push(wave);
// A tween or timeline that has not finished stays on the shared scheduler until it is removed.
return { dispose: () => running.forEach((item) => scheduler.scheduler.remove(item)) };
}
let handle = mountEngine(/** @type {SVGElement} */ (document.getElementById('mount')));
/** Clear the stage and run the three animations again. */
export function restart() {
const mount = /** @type {SVGElement} */ (document.getElementById('mount'));
handle.dispose();
mount.replaceChildren();
handle = mountEngine(mount);
}
document.getElementById('restart')?.addEventListener('click', restart);index.html
<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>graphic-anim · Tween, sequence and stagger SVG shapes</title>
<link rel="stylesheet" href="../../_style/examples.css">
<link rel="stylesheet" href="./style.css">
<script type="importmap">
{ "imports": {
"@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/",
"@awacloud/graphic-anim": "/vendor/graphic-anim/main.js", "@awacloud/graphic-anim/": "/vendor/graphic-anim/" } }
</script>
<script type="module" src="./main.js"></script>
</head>
<body>
<main>
<h1>Tween, sequence and stagger SVG shapes</h1>
<p>Real SVG shapes moved by one tween, a sequenced timeline and a staggered group.</p>
<svg id="mount" viewBox="0 0 200 130" role="img" aria-label="A dot, three bars and five dots, animating"></svg>
<p><button id="restart" type="button">Restart</button></p>
<ul>
<li>Blue to orange dot: a single tween, position and colour, eased.</li>
<li>Green bars: one timeline, three tweens in sequence.</li>
<li>Purple dots: one timeline, a delay per element.</li>
</ul>
</main>
</body>
</html>style.css
/* SPDX-License-Identifier: Apache-2.0 */
#mount { display: block; width: min(100%, 32rem); height: auto; background: #1a1a1a; border-radius: 8px; }