PLAYER API · REFERENCE

Drive it from your code.

An SVG exported with JavaScript carries a small player. Your page’s script can play it, pause it, seek, reverse, change its speed and listen to what it does. Try every call on the live animation below: each one shows the code that does it.

Sunrise badgeMADE WITH SVGCINEMAGood morning
0:00.00 of 0:00.00 paused
const player = document.getElementById("hero-svg").player;

Playback

Seek

)

Settings: set(name, value)

1 100

Listening: on() / off()

Properties, live

Events keyframe 0

    This is the editor’s sample scene, exported with the embedded player and Starts › On load. Every button calls the player exactly as your script would.

    Getting the player

    The player is the SVG element’s player property (also cinPlayer). The SVG’s id is in the code the Export window gives you (Embed in your site); it starts with your Generated IDs prefix and ends in -svg.

    Pasted inline

    const player = document.getElementById("cin-svg").player;

    In an <object>

    const obj = document.getElementById("anim");
    obj.addEventListener("load", () => {
      const player = obj.contentDocument.getElementById("cin-svg").player;
    });

    External player

    // the player arrives once its file has loaded
    const svg = document.getElementById("cin-svg");
    const start = (player) => player.play();
    svg.player ? start(svg.player)
      : svg.addEventListener("cinready", (e) => start(e.detail));
    // SVGs your script adds later:
    CinPlayer.boot(svg);

    Every method returns the player, so calls chain: player.seek(0).set("speed", 2).play(). To start it only from your script, set Starts to Programmatic. The embed code for React, Next.js, Vue, Svelte and Angular is on the Export page and in the Export window.

    Methods

    Method Fires What it does
    play() play Plays from the current point. Once it has ended, from the start.
    pause() pause Pauses where it is.
    toggle(), togglePlay() play or pause Pauses if it is playing, else plays.
    restart() restart Back to the start, in its own direction, and plays.
    reverse() reverse Flips the direction and plays from where it is.
    rollback() reverse Plays backwards to the start, then stops there (what Mouse out › Play backwards uses).
    stop() stop Stops and jumps to the first frame.
    seek(percent) keyframe Jumps to a point, 0–100 of the whole length (all its iterations).
    seekTo(ms) keyframe Jumps to a time in milliseconds.
    seekBy(ms) keyframe Moves by milliseconds: negative goes back.
    set(name, value) none Changes a setting while it runs: see Settings.
    setRate(speed) none The same as set("speed", speed).
    ready(callback) none Calls callback(player) once the player is ready (right after the current script).
    on(names, fn, prepend) none Listens to one or more events: see Events.
    onEnd(fn) none The same as on("end", fn).
    off(name, fn) none Stops listening: that function, or every listener of that event when fn is left out.
    destruct() none Stops it for good: removes its listeners and its start trigger’s, and shows the first frame. Call it when your page removes the animation.

    Settings: set(name, value)

    They start as the Export window set them (Playback) and change only this page’s copy, not the file.

    Name Values What it does
    speed 0.01–100 (1 = normal) How fast it plays: 2 is twice as fast.
    fps 1–100 The most frames it draws per second. Lower it for a deliberately choppy look, or to save power.
    iterations 0 or Infinity (forever), or a whole number How many times it plays before it ends.
    direction 1 forwards, -1 backwards Which way it plays now.
    alternate true, false Every other loop plays backwards, so it goes back and forth.
    fill 1 stay on the last frame, -1 back to the first What it shows once it has ended.

    Properties

    Read-only; times are in milliseconds.

    Property Type What it says
    currentTime number Where it is, from the start of the first iteration.
    duration number One loop’s length.
    totalTime number All its iterations (one loop when it plays forever).
    state string "playing", "paused", "ended", "rollback" or "destroyed".
    isPlaying boolean It is playing (rolling back counts).
    isReversed boolean It plays backwards now.
    isRollingBack boolean It is playing a rollback().
    hasEnded boolean It reached its end and stopped.
    isInfinite boolean It plays forever.
    isAlternate boolean It goes back and forth.
    isBackwards boolean Once it ends it goes back to the first frame (fill is -1).
    speed, fps, iterations number The settings as they are now.
    direction, fill 1 or -1 The settings as they are now.

    Events

    on(names, fn, prepend) listens: names is one event or several ("play pause" or "play,pause"); prepend true runs fn before the listeners already there. fn gets the time in milliseconds, with this as the player.

    player.on("end", (ms) => console.log("finished at", ms));
    player.on("play pause", function () { console.log(this.state); });
    const onFrame = (ms) => progress.value = ms / player.totalTime;
    player.on("keyframe", onFrame);
    player.off("keyframe", onFrame);   // that listener
    player.off("end");                 // every "end" listener
    Event When
    play play() (and toggle() when paused), and its start trigger
    pause pause(), toggle() when playing, and Mouse out › Pause
    restart restart()
    reverse reverse() and rollback()
    stop stop()
    end It reached its end by itself (not when it plays forever)
    keyframe Every frame it draws, and after each seek

    Good to know

    • No player in an <img>. Browsers run no scripts in an SVG shown as an image or a CSS background: use it inline, or in an <object> or <embed>. A CSS-only export has no player.
    • Less motion. When the visitor asks their system for less motion and Less motion is set to a first or last frame, the player holds that frame and its start trigger doesn’t run; your calls still work.
    • Linked to scroll drives the player from the page’s scroll: calling it too would fight the scroll.
    • Try it in the editor. The controls under the Export window’s Animated SVG preview call these methods on your own animation and show the code. The player API, step by step →