Skip to content

Vue — @pixodesk/svg-animator-vue

Use this in a Vue 3 or Nuxt app: drop in the component, pass it the JSON, and it renders the animation and controls its playback. It wraps the web player and renders the SVG through Vue’s virtual DOM, so it is SSR-safe and Nuxt-ready. It mirrors the React component feature for feature, so the two guides read the same.

Terminal window
npm install @pixodesk/svg-animator-vue
<script setup lang="ts">
import { SvgAnimationPlayer } from '@pixodesk/svg-animator-vue';
import animation from './animation.json';
</script>
<template>
<SvgAnimationPlayer :doc="animation" playUsingActionTriggers />
</template>

The component renders the document’s root <svg> directly — there is no wrapper element. To set its size, give the element that contains it a width and height (or put style on the component itself — see the props table); the SVG keeps its proportions.

Every driver prop starts with its verb — play… plays, show… shows a frame: playUsingActionTriggers (the document’s triggers, or ones you pass), playback (your state — off · on · paused), showAtProgress / showAtTime (your position). Set props from more than one group and the highest-priority one wins — showAtProgress → showAtTime → playback → playUsingActionTriggers — and the component warns, naming both props and the winner (PX1501). The template ref is not a mode: it is available in every mode and never changes which one you are in. React, Vue and React Native all resolve this the same way, from one rule in core.

Nothing plays until a prop or option names the driver (playUsingActionTriggers, playback, showAt…). A file embedded with no code around it — the <div data-px-animation-src> tag, the CSS wrapper, a standalone SVG file — has no other possible driver, so there the embedding is that naming and the file plays as saved.

In every mode but playUsingActionTriggers the file’s own triggers are switched off — core’s withoutTriggerDriver replaces them with an empty block, so nothing starts the animation by itself (a note on the control props).

Play using action triggers (playUsingActionTriggers)

Section titled “Play using action triggers (playUsingActionTriggers)”

Example: vue/play-using-action-triggers — pnpm example:docs, then open #vue/play-using-action-triggers.

The simplest mode: the component starts the animation the way the file says it should — on load, on hover, on click — once enough of it is on screen.

<script setup lang="ts">
import { SvgAnimationPlayer } from '@pixodesk/svg-animator-vue';
import animation from './animation.json';
</script>
<template>
<SvgAnimationPlayer :doc="animation" playUsingActionTriggers />
</template>

playUsingActionTriggers alone uses the triggers saved in the document — their event lists and the visibility gate (Triggers — WHEN it plays). Bind an object instead and that is the whole actionTriggers block to use for this mount — a replacement, not a merge: the lists you leave out are simply not listed, never the file’s values. How each action looks stays in the file’s actions, which the actions prop adjusts — see Playback overrides.

<template>
<!-- plays while hovered; when the pointer leaves it plays on to the end of the current pass, then rests -->
<SvgAnimationPlayer :doc="animation"
:playUsingActionTriggers="{ 'playback.start': 'hoverIn', 'playback.stop': 'hoverOut' }"
:actions="{ 'playback.stop': 'animateToEnd' }" />
<!-- a click toggles off → playing → paused → playing (the state rule picks the list) -->
<SvgAnimationPlayer :doc="animation" :playUsingActionTriggers="{ 'playback.start': 'click', 'playback.pause': 'click' }" />
<!-- a click starts it, the next click stops it -->
<SvgAnimationPlayer :doc="animation" :playUsingActionTriggers="{ 'playback.start': 'click', 'playback.stop': 'click' }" />
</template>

Controlled time (showAtProgress / showAtTime)

Section titled “Controlled time (showAtProgress / showAtTime)”

Example: vue/controlled-time — pnpm example:docs, then open #vue/controlled-time.

Use these when your code owns the position — a slider, a scroll offset, a step in a walkthrough. The component renders exactly that frame and never plays on its own.

<script setup lang="ts">
import { ref } from 'vue';
import { SvgAnimationPlayer } from '@pixodesk/svg-animator-vue';
import animation from './animation.json';
const time = ref(0);
</script>
<template>
<SvgAnimationPlayer :doc="animation" :showAtTime="time" />
<input type="range" min="0" max="2000" v-model.number="time" />
<!-- a fixed frame by FRACTION of one iteration — that is `showAtProgress`, not
`showAtTime` (milliseconds, so `:showAtTime="0.5"` would be half a millisecond in) -->
<SvgAnimationPlayer :doc="animation" :showAtProgress="0.5" />
</template>

Both are positions in one iteration of the content window — showAtProgress from 0 to 1, showAtTime from 0 to playback.getDuration() ms — so neither needs an exception for iterations: 'infinite'. Changing the value moves the existing player to the new position — nothing is recreated. The read twins on the template ref are playback.getCurrentProgress() and playback.getCurrentTime().

Example: vue/declarative — pnpm example:docs, then open #vue/declarative.

playback names a position, not a command — a lever your app holds: 'off' (no playback), 'on' (a playback) or 'paused' (the playback, frozen). A change of position presses one of the built-in buttons — off → on is playback.start(), on → paused is playback.pause(), paused → on is playback.resume(), anything → off is playback.stop() — and HOW a start or a stop looks is the file’s actions block, which the actions prop adjusts and which the triggers and the ref honour too (Actions — how a start and a stop look). playback 'off' performs the stop action (actions["playback.stop"]: jumpToStart, animateToStart, jumpToEnd or animateToEnd) — it does not pause; freeze with 'paused'. With the defaults, a ONE-SHOT document (a single pass) plays to the end on 'on' and holds, and snaps back on 'off'; a LOOPING document (iterations > 1 or 'infinite') runs on 'on' and snaps to rest on 'off' — or plays on to the end of the current pass first with :actions="{ 'playback.stop': 'animateToEnd' }". A change always moves from the current frame; a mount with 'on' plays (a one-shot to its end), a mount with 'off' presses nothing, a mount with 'paused' starts the playback frozen. No pair of positions is invalid (play / pause / playState until 1.23 — the booleans had an impossible pair, and the word mixed the request with the readout; the readout is playback.getState()). The rule is shared with the React and React Native components (resolveDeclarativePlayback in core). One-shot verbs (playback.playOnce(), playback.jumpToEnd()) live on the template ref.

<script setup lang="ts">
import { ref } from 'vue';
import { SvgAnimationPlayer } from '@pixodesk/svg-animator-vue';
import animation from './animation.json';
const hovered = ref(false);
const busy = ref(false);
const frozen = ref(false);
</script>
<template>
<!-- the trash-can lid: opens on hover, plays BACK closed on leave, from wherever it is -->
<div @mouseenter="hovered = true" @mouseleave="hovered = false">
<SvgAnimationPlayer :doc="animation" :playback="hovered ? 'on' : 'off'" :actions="{ 'playback.stop': 'animateToStart' }" />
</div>
<!-- a spinner: runs while busy; when busy ends it finishes the current turn, then rests -->
<SvgAnimationPlayer :doc="animation" :playback="busy ? 'on' : 'off'" :actions="{ 'playback.stop': 'animateToEnd' }" />
<!-- the same spinner with a freeze button: 'paused' keeps the playback, frozen -->
<SvgAnimationPlayer :doc="animation" :playback="frozen ? 'paused' : busy ? 'on' : 'off'" />
</template>

The other recipes — overlay art that appears instantly and retreats animated ({ 'playback.start': 'jumpToEnd', 'playback.stop': 'animateToStart' }), a splash that replays on every click ({ 'playback.start': 'jumpToStart' }) — are written out on the React page; the same actions objects work here.

Example: vue/imperative — pnpm example:docs, then open #vue/imperative.

The component exposes the playback API on its template ref, so it is available in every mode:

<script setup lang="ts">
import { ref } from 'vue';
import { SvgAnimationPlayer, type PxAnimationPlayerHandle } from '@pixodesk/svg-animator-vue';
import animation from './animation.json';
const player = ref<PxAnimationPlayerHandle | null>(null);
</script>
<template>
<SvgAnimationPlayer :doc="animation" ref="player" />
<button @click="player?.playback.start()">Start</button>
<button @click="player?.playback.pause()">Pause</button>
<button @click="player?.playback.stop()">Stop</button>
</template>

The ref holds core’s PxAnimationPlayerHandle — the one handle name every framework package exports, so the three cannot drift apart: playback, everything a page does to the animation, the same PxPlaybackControls the web player hands out — every member is listed there. The four buttons — playback.start(), playback.stop(), playback.pause(), playback.resume() — take no arguments: how a start or a stop looks is the file’s actions, and the handle follows it exactly as the triggers and the playback prop do. One spelling per act: there are no play() / pause() aliases (until 1.25 the methods sat flat on the handle).

interface PxAnimationPlayerHandle {
readonly playback: PxPlaybackControls; // start · stop · pause · resume · playOnce — the buttons (each emits
// its event: `playbackStart` …); getState · isActive · isPlaying — the
// state; jumpToStart · jumpToEnd · setCurrentTime · setCurrentProgress ·
// setTotalTime — the playhead; getDuration · getCurrentTime · … ·
// getTotalProgress — content time; getSpeed / setSpeed and the
// …AtSpeed reads — the speed
}

Two clocks. Plain names are content ms inside the iteration: playback.getCurrentTime() is the frame on screen, 0..getDuration(), so a slider for a looping file spans one pass and wraps — the way every animation player shows it. The total… names span the whole playback (“total” = including repeats, GSAP’s words): playback.getTotalDuration() is the slider maximum for a finite playback and null when iterations is 'infinite' (the honest answer, no division to guard); playback.getTotalTime() / playback.getTotalProgress() are the playhead on it. The …AtSpeed names are the same values at the current speed: playback.getDurationAtSpeed() is how long one pass takes, playback.getCurrentTimeAtSpeed() / playback.getTotalTimeAtSpeed() where the playhead is; the file’s timeline.playbackSpeed is the initial speed and playback.setSpeed() replaces it (Two clocks). playback.jumpToStart() is the unconditional jump to the rest pose and reports nothing, like a seek; playback.stop() runs the file’s stop action and emits playbackStop. A call on a ref whose component has unmounted is ignored (PX1504); playback.getState() then says 'destroyed'.

With none of playUsingActionTriggers / showAtProgress / showAtTime / playback set, the first frame renders statically and the ref is your only control.

The same document can play differently in each place you mount it. Three props, the same on every player (Overriding from a player), and the document you passed is never modified:

  • timelineOptions — corrections to the file’s animator.timeline block, minus its actionTriggers and actions: an object shaped like the file — iterations, delay, playbackSpeed, the gate numbers; whoever needs more edits the document.
  • actions — corrections to the file’s timeline.actions: { 'playback.stop': 'animateToStart' } changes how a stop looks and leaves every other setting as the file has it.
  • playUsingActionTriggers — true plays on the file’s triggers; an object is the whole actionTriggers block to use instead (Play using action triggers). To have your code drive instead, pass no control prop at all — the file’s triggers are then switched off and the template ref is the driver.

Merge or replace — the one rule for the three:

timelineOptions, actionsrecursive partials — objects merge key by key, values replace, a key left out changes nothing
a playUsingActionTriggers objectreplaces the file’s actionTriggers as a whole — its lists belong together
<template>
<!-- The file loops twice and plays on load; here it loops forever and waits for playback.start() —
without `playUsingActionTriggers` the file's triggers are switched off, so only your code starts it. -->
<SvgAnimationPlayer :doc="animation" :timeline-options="{ iterations: 'infinite' }" ref="anim" />
<!-- These triggers instead of the file's, and a smooth retreat on hover-out -->
<SvgAnimationPlayer :doc="animation"
:playUsingActionTriggers="{ 'playback.start': 'hoverIn', 'playback.stop': 'hoverOut' }"
:actions="{ 'playback.stop': 'animateToStart' }" />
</template>

There is no delete: a key you leave out keeps the file’s value. To get a default back, set it (:timeline-options="{ delay: 0 }").

actionTriggers and actions are not keys of timelineOptions — only playUsingActionTriggers carries triggers and only actions the action settings, so every key is reachable exactly one way. The out-of-view behavior goes in actions (:actions="{ outOfView: 'continue' }"); the gate numbers inViewThreshold and inViewDebounce are timeline keys and go in timelineOptions (the visibility gate).

To repeat the whole animation, pass :timeline-options="{ iterations: 'infinite' }" — there is no loop prop. A loop inside a file means something narrower: it repeats part of one property’s keyframes (per-property loops).

Full merge rules — including what happens when the override changes the kind of timeline — are in Playback & triggers → How the merge works.

Only doc is required. The file already carries the timing and the triggers you set in the editor; every other prop is optional and, when passed, adjusts the file’s value for this one component.

The React set, minus playerRef / className / style — a template ref and Vue’s attribute inheritance cover those:

const SvgAnimationPlayer: DefineComponent<{
doc: PxDocumentJson; // required — the animation; a .json import passes as-is
// Playback override — deep-merged over the file (Playback overrides above)
timelineOptions?: PxTimelineOptions; // the timing: the `timeline` block minus `actionTriggers` and `actions`
actions?: PxActionsOptions; // how a start and a stop look (+ the gate's outOfView): { 'playback.stop': 'animateToStart' };
// the end of the pass is a timeline key — timelineOptions: { atEnd: 'jumpToStart' }
// Control — the highest-priority one that is set picks the mode (Control modes above)
playUsingActionTriggers?: boolean | PxActionTriggersInput; // true: the file's own triggers; an object: these triggers instead —
// { "playback.start", "playback.stop", "playback.pause", "playback.resume" }, each one event or a list of the
// events that list may name ('load' | 'hoverIn' | 'hoverOut' |
// 'click'); playback.resume is derived when left out
playback?: 'off' | 'on' | 'paused'; // a LEVER (`PxPlayback`): a change of position presses one button — off → on
// playback.start(), on → paused playback.pause(), paused → on playback.resume(), → off playback.stop();
// how each looks is the file's `actions`; a mount with 'off' presses nothing
showAtProgress?: number; // a frame at 0–1 of ONE iteration
showAtTime?: number; // a frame at this ms inside one iteration, 0..getDuration()
// Diagnostics stay PROPS, not events: an event handler always exists, which would silence
// the console fallback. onWarning = it plays, but something was ignored, degraded or misspelled;
// onError = this instance will not play (the API at a glance)
onWarning?: (d: PxDiagnostic) => void;
onError?: (d: PxDiagnostic) => void;
suppressWarnings?: boolean; suppressErrors?: boolean;
}>;

Anything else you put on <SvgAnimationPlayer> — class, style, any attribute — ends up on the <svg> element it renders (standard Vue attribute inheritance). So to set the animation’s size, either put style="width: 300px; height: 300px" on the component itself, or give those dimensions to the element that contains it — the SVG keeps its proportions either way.

EventWhen
readythe player exists and the SVG is rendered — once, asynchronously; the push twin of isReady()
playbackStarta playback began — playback.start(), a playback.start trigger, playback to 'on' / 'paused' from 'off', playback.playOnce(); a returning playback turned around
playbackResumethe playback carried on after a freeze — playback.resume(), a playback.resume trigger, playback back to 'on'
playbackPausethe playback was frozen at the current frame — playback.pause(), a playback.pause trigger, playback 'paused'. Not the visibility gate’s hold, which reports nothing
finishthe animation reached its end — it played all its iterations, or playback.jumpToEnd() was called. Does not fire when playback is stopped early; a loop stopped at a pass end emits playbackStop only
destroythe player was thrown away: the component unmounted, or you passed a different doc and a new player was built for it
playbackStopthe playback was stopped — a playback.stop trigger, playback 'off' or playback.stop(); at once, or on arrival for animateToEnd. A pause is playbackPause, the end finish; playback.jumpToStart() emits nothing

onWarning and onError are props, not events. With an event, Vue would treat the message as handled even when nobody listens, so it would never reach the console. As props, leaving them out means exactly that — and the message goes to the console as usual.

Each diagnostic is { code, kind, data?, message, error? }. code is a number you can switch on — its description is on the codes page, which message links to; data carries the specifics. kind says who can act on it: document (repair the file) · host (fix the page) · platform (the browser could not do it; the player degraded) · usage (fix the props you passed) · internal (report it to us). So you can route rather than just log — surface document problems in a build check, for instance. Once you know what a document has to say and tolerate it, suppressWarnings keeps it out of the console.

<script setup lang="ts">
import { SvgAnimationPlayer } from '@pixodesk/svg-animator-vue';
import animation from './animation.json';
const onDone = () => console.log('finished');
const onPlaybackStop = () => console.log('stopped');
</script>
<template>
<SvgAnimationPlayer :doc="animation" playUsingActionTriggers @finish="onDone" @run-stop="onPlaybackStop" />
</template>

Passing a different doc throws the old player away and builds a new one; the old instance emits destroy on its way out.

Example: vue/css-loader — pnpm example:docs, then open #vue/css-loader.

For a pre-rendered SVG + CSS animation file imported with vite-svg-loader (or any loader that yields a component), this wrapper adds the triggers and the visibility gate. It renders a <div> of its own around your SVG component — that is what SvgCssAnimationPlayer becomes on the page — and starts, pauses or stops the animation by switching the file’s CSS classes on that <div>. Its props are SvgAnimationPlayer’s names, narrowed where a class toggle can do less (they derive from the JSON component’s props and are checked against them):

<script setup>
import { SvgCssAnimationPlayer } from '@pixodesk/svg-animator-vue';
import AnimationSvg from './animation.svg'; // vite-svg-loader
</script>
<template>
<SvgCssAnimationPlayer :playUsingActionTriggers="{ 'playback.start': 'hoverIn', 'playback.pause': 'hoverOut' }" style="width: 400px; height: 400px">
<AnimationSvg />
</SvgCssAnimationPlayer>
</template>

playUsingActionTriggers is the JSON player’s block — the same lists over the same events, read by the same rules (Triggers — WHEN it plays) — and the gate is the same gate. It takes the block only, never true: a script-free SVG imported as a component carries no triggers the component could read. An absent playUsingActionTriggers lists nothing, like an empty block — nothing starts the animation without one; to play on load, say so: :playUsingActionTriggers="{ 'playback.start': 'load' }". The gate numbers go in timelineOptions, as on the JSON component: :timelineOptions="{ inViewThreshold: 1 }".

// The SVG goes in the default slot; every other attribute (class, style, …) lands on the wrapper div.
const SvgCssAnimationPlayer: DefineComponent<{
playUsingActionTriggers?: PxActionTriggersInput; // the BLOCK only: { "playback.start", "playback.stop", "playback.pause", "playback.resume" },
// each one event or a list of the events that list may name ('load' |
// 'hoverIn' | 'hoverOut' | 'click'); playback.resume is derived when left out
timelineOptions?: { inViewThreshold?: number; inViewDebounce?: number };
// the gate numbers only: 0–1 of the SVG on screen before it may run
// (default 0.5; 0 = any visible pixel), and ms it must hold first (150)
actions?: Pick<PxActionsOptions, 'outOfView'>; // only `outOfView` applies: 'pause' (default) | 'continue' |
// 'jumpToStart' while nobody can see it
}>;

Accepted limits of a class toggle — how the moves look stays in the file’s CSS, so here:

  • the start is always continue — a class cannot jump keyframes to their start or end;
  • the stop is always jumpToStart — a class cannot play keyframes backwards or on to their end;
  • whenIdle and atEnd are the file’s own CSS and are not changed from here.

⚠️ Don’t put the SAME SVG file on a page twice. You can have as many <SvgCssAnimationPlayer> on a page as you like, each with a different file. What does not work is the same file twice: the imported component is the file’s markup, element ids included, so two copies share the same ids and their masks and gradients cross over. To show one animation several times, use the JSON component instead — the player gives every copy its own ids (read more).

Only the pure CSS flavor works this way (loaders strip or refuse <script>); flavors with scripts should be inlined as raw HTML, or use JSON.

Everything is spelled out above: SvgAnimationPlayer under Props and Events, the handle under Imperative API, SvgCssAnimationPlayer under CSS-flavor SVGs. Modes, highest priority wins: showAtProgress → showAtTime → playback → playUsingActionTriggers → static — the same rule as React and React Native, conflicts warned the same way; a change to doc’s content (compared deeply) or to an override prop re-creates the player. The callbacks and diagnostics are the shape every player shares — the API at a glance.

The package exports the two components, SvgAnimationPlayer and SvgCssAnimationPlayer, the handle type PxAnimationPlayerHandle with its PxPlaybackControls (what playback is), and the document type PxDocument (for casting a JSON import — TypeScript); nothing else.

Every section above links to its running example in examples/docs-examples. Each example is a small standalone page, and they are all collected in one app: a list of every example down the side, with the selected one running next to it. Run pnpm example:docs from the repository root to open it, then pick an example from the list — or jump straight to one by its address in the URL, like #vue/play-using-action-triggers. Each example has a test that runs on every build.