vivus, bringing your SVGs to life

Vivus is a lightweight JavaScript class (with no dependencies) that allows you to animate SVGs, giving them the appearence of being drawn. There are a variety of different animations available, as well as the option to create a custom script to draw your SVG in whatever way you like.

How it looks

Delayed

Every path element is drawn at the same time with a small delay at the start. This is currently the default animation.

Async

Each line is drawn asynchronously. They all start and finish at the same time, hence the name `async`.

OneByOne

Each path element is drawn one after the other. This animation gives the best impression of live drawing.

How it works

To get this effect, the script uses the CSS property strokeDashoffset. This property manages the stroke offset on every line of the SVG. Now, all we have to do is add some JavaScript to update this value progressively and the magic begins.
However, there's a problem with this. The strokeDashoffset property is only available on the path elements. This is an issue because in an SVG there are a lot of elements such as circle, rect, line and polyline which will break the animation. So to fix this, there is another class available in the repo called pathformer. It's made for transforming most objects of your SVG into path elements to be able to use strokeDashoffset and animate your SVGs.

The code is inspired from other repositories. The drawer is inspired from the excellent Codrops post about SVG Drawing Animation (if you didn't know about this website, get ready to have your mind blown). The pathformer is inspired from SVGPathConverter by Waest.

How to use it

As I said, there are no dependencies here. You just need to include the script.

The library is also available via:

// Inline SVG
new Vivus('my-svg-id', {type: 'delayed', duration: 200}, myCallback);

// Dynamic load
new Vivus('my-div-id', {type: 'delayed', duration: 200, file: 'link/to/my.svg'}, myCallback);

The Vivus constructor asks for 3 parameters :

The options object must respect the following structure :

type string
define what kind of animation will be used: delayed, async, oneByOne, scenario or scenario-sync
duration integer
animation duration, in frames
start string
define how to trigger the animation

inViewport once the SVG is in the viewport
manual give you the freedom to call play method to start
autostart make it start right now

delay integer
time between the drawing of first and last path, in frames (only for delayed animations)
file string
link to the SVG to animate. If set, Vivus will create an object tag and append it to the DOM element given to the constructor. Be careful, use the onReady callback before playing with the Vivus instance.
onReady function
function called when the instance is ready to play
dashGap integer
whitespace extra margin between dashes. The default value is 2. Increase it in case of glitches at the initial state of the animation
pathTimingFunction function
timing animation function for each path element of the SVG. The function must accept a value between 0 and 1, then return a number between 0 and 1.
animTimingFunction function
timing animation function for the complete SVG. The function must accept a value between 0 and 1, then return a number between 0 and 1.
forceRender boolean
force the browser to re-render all updated path items. By default, the value is true on IE only. (more details)
selfDestroy boolean
remove all extra styling on the SVG, and leave it as original

To control the animation, there are three methods are available : play, stop and reset. The method play takes one parameter which is the speed. This value can be positive, negative (to go backwards), or under 1 to play slowly. By default the value is 1.

Timing function

To give more freedom, it's possible to override the animation of each path and/or the entire SVG. It works a bit like the CSS animation timing function. But instead of using a cubic-bezier function, it use a simple JavaScript function. It must accept a number as parameter (between 0 to 1), then return a number (also between 0 and 1). It's a hook.

Here an example test to play around with the different properties available.

Type

Path timing function

Anim timing function

Scenarize

This feature allows you to script the animation of your SVG. To do this, the custom values will be set directly in the DOM of the SVG.

Here is an example using scenario-sync.
I would recommend you look at the source code and the readme file for more information.

Thanks for watching.

Made with love a keyboard