Search code examples
javascriptsvgdom-eventssmil

Modifying an SVG animateTransform at end-of-repeat-loop


I'm trying to make an object rotate faster and faster as time goes on, and I'm having trouble having an event fire every time the animation repeats itself. SVG/SMIL's repeatEvent seems to be what I need, but in the following code, onSpinEnd() doesn't appear to be called at all. What am I doing wrong?

    <path id='coloredPart' d='…'>
        <animateTransform
            id='spin'
            attributeName='transform'
            begin='0s'
            dur='5s'
            type='rotate'
            from='0 0 0'
            to='-360 0 0'
            repeatCount='indefinite'
        />
    </path>

    <script>  <![CDATA[
        var spin = document.getElementById('spin');
        var onSpinEnd = function() {
            var intPart = 0;
            intPart = parseInt(spin.getAttribute('dur')[0].slice(0, -1));
            console.log(intPart);
            if (intPart > 1) --intPart;
            spin.setAttribute('dur', intPart.toString() + 's');
        };
        spin.addEventListener('repeatEvent', onSpinEnd, false)
        ]]>   
    </script>

As it turns out, [0].slice(0, -1)) won't give me "16" from "16s"; .slice(0, -1)) will. That line should read:

intPart = parseInt(spin.getAttribute('dur').slice(0, -1));

Solution

  • I'm not sure what the slice is for. This seems to work on Firefox.

    var onSpinEnd = function() {
        var spin = document.getElementById('spin');
        var intPart = 0;
        intPart = parseInt(spin.getAttribute('dur')[0]);
        console.log(intPart);
        if (intPart > 1) --intPart;
        spin.setAttribute('dur', intPart.toString() + 's');
    };
    

    Additionally, as far as I know only Opera and Firefox currently fire SMIL events.