Dan Devine Dan Devine - 2 months ago 19
Javascript Question

Stagger js svg animation

I'm using vivus.js to animate an svg which is repeated multple times (as part of a php loop).

i'm looking for a way to stagger the animation, so that svg id 'hexx--0' animates first and then when complete, svg id 'hexx--1' animates and so on. The ID number is being incremented in the php loop.

I've attempted to literally chain these in a function that fires when the first animation finishes, like so:

new Vivus('hexx--0', {
type: 'oneByOne',
duration: 100
}, function() {

new Vivus('hexx--1', {
type: 'oneByOne',
duration: 100,

});

});


which works, but a) means the svg is visible before the animation fires and b) is messy and unmaintainable across a loop which could have an arbitrary amount of svgs.

Is it possible to increment the IDs in JavaScript, and generate the new Vivus in a loop? Below is a fiddle which replicates the issue, note that the html is being generated via a php loop in the real case.



//all animating at same time

new Vivus('hexx--0', {
type: 'oneByOne',
duration: 100,

});

new Vivus('hexx--1', {
type: 'oneByOne',
duration: 100,

});

//attempt at staggering

new Vivus('hexx--2', {
type: 'oneByOne',
duration: 100
}, function() {

new Vivus('hexx--3', {
type: 'oneByOne',
duration: 100,

});

});

svg {
stroke: #000000;
}

<body>
<svg version="1.1" id="hexx--0" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="236.275px" height="265.632px" viewBox="0 0 236.275 265.632" enable-background="new 0 0 236.275 265.632" xml:space="preserve">
<g>
<path fill="none" stroke-miterlimit="10" d="M118.046,1.007c-3.865,0-7.692,1.056-11.377,3.167
C73.449,23.21,40.564,42.238,12.778,58.341c-7.8,4.52-11.758,11.436-11.763,20.553c-0.019,35.954-0.021,71.906,0.002,107.86
c0.006,9.091,3.974,15.999,11.798,20.531c33.368,19.336,64.021,37.025,93.71,54.08c7.575,4.354,15.698,4.335,23.328-0.054
c30.902-17.775,61.802-35.635,93.377-53.891c7.991-4.621,12.043-11.68,12.042-20.98l-0.001-38.245l0.002-30.855
c0.003-12.791,0.006-25.582-0.009-38.373c-0.012-9.109-4.005-16.067-11.868-20.68c-6.162-3.615-12.562-7.301-18.208-10.551
l-22.8-13.16c-17.397-10.045-34.793-20.09-52.204-30.108C126.174,2.161,122.089,1.007,118.046,1.007" />
</g>
</svg>
<svg version="1.1" id="hexx--1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="236.275px" height="265.632px" viewBox="0 0 236.275 265.632" enable-background="new 0 0 236.275 265.632" xml:space="preserve">
<g>
<path fill="none" stroke-miterlimit="10" d="M118.046,1.007c-3.865,0-7.692,1.056-11.377,3.167
C73.449,23.21,40.564,42.238,12.778,58.341c-7.8,4.52-11.758,11.436-11.763,20.553c-0.019,35.954-0.021,71.906,0.002,107.86
c0.006,9.091,3.974,15.999,11.798,20.531c33.368,19.336,64.021,37.025,93.71,54.08c7.575,4.354,15.698,4.335,23.328-0.054
c30.902-17.775,61.802-35.635,93.377-53.891c7.991-4.621,12.043-11.68,12.042-20.98l-0.001-38.245l0.002-30.855
c0.003-12.791,0.006-25.582-0.009-38.373c-0.012-9.109-4.005-16.067-11.868-20.68c-6.162-3.615-12.562-7.301-18.208-10.551
l-22.8-13.16c-17.397-10.045-34.793-20.09-52.204-30.108C126.174,2.161,122.089,1.007,118.046,1.007" />
</g>
</svg>
<svg version="1.1" id="hexx--2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="236.275px" height="265.632px" viewBox="0 0 236.275 265.632" enable-background="new 0 0 236.275 265.632" xml:space="preserve">
<g>
<path fill="none" stroke-miterlimit="10" d="M118.046,1.007c-3.865,0-7.692,1.056-11.377,3.167
C73.449,23.21,40.564,42.238,12.778,58.341c-7.8,4.52-11.758,11.436-11.763,20.553c-0.019,35.954-0.021,71.906,0.002,107.86
c0.006,9.091,3.974,15.999,11.798,20.531c33.368,19.336,64.021,37.025,93.71,54.08c7.575,4.354,15.698,4.335,23.328-0.054
c30.902-17.775,61.802-35.635,93.377-53.891c7.991-4.621,12.043-11.68,12.042-20.98l-0.001-38.245l0.002-30.855
c0.003-12.791,0.006-25.582-0.009-38.373c-0.012-9.109-4.005-16.067-11.868-20.68c-6.162-3.615-12.562-7.301-18.208-10.551
l-22.8-13.16c-17.397-10.045-34.793-20.09-52.204-30.108C126.174,2.161,122.089,1.007,118.046,1.007" />
</g>
</svg>
<svg version="1.1" id="hexx--3" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="236.275px" height="265.632px" viewBox="0 0 236.275 265.632" enable-background="new 0 0 236.275 265.632" xml:space="preserve">
<g>
<path fill="none" stroke-miterlimit="10" d="M118.046,1.007c-3.865,0-7.692,1.056-11.377,3.167
C73.449,23.21,40.564,42.238,12.778,58.341c-7.8,4.52-11.758,11.436-11.763,20.553c-0.019,35.954-0.021,71.906,0.002,107.86
c0.006,9.091,3.974,15.999,11.798,20.531c33.368,19.336,64.021,37.025,93.71,54.08c7.575,4.354,15.698,4.335,23.328-0.054
c30.902-17.775,61.802-35.635,93.377-53.891c7.991-4.621,12.043-11.68,12.042-20.98l-0.001-38.245l0.002-30.855
c0.003-12.791,0.006-25.582-0.009-38.373c-0.012-9.109-4.005-16.067-11.868-20.68c-6.162-3.615-12.562-7.301-18.208-10.551
l-22.8-13.16c-17.397-10.045-34.793-20.09-52.204-30.108C126.174,2.161,122.089,1.007,118.046,1.007" />
</g>
</svg>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vivus/0.3.1/vivus.js"></script>




Answer

I eventually figured this out, having secured a tumbleweed badge for my troubles.

Step 1: in JavaScript, increment the ids with a for loop:

    //increment the ids
    var i;
    for (i = 0; i < 7; i++) {

        new Vivus('hexx--' + i, {
            type: 'scenario-sync'
        });
    }

I changed the type to 'scenario-sync' which means you can set the duration and delay using html data attributes.

Step 2: in PHP I added the data attributes to the path like so:

path data-delay="<?php echo $delay ?>" data-duration="<?php echo $duration ?>"

and incremented the values in a php loop accordingly.