8

Apologies in advance for my ignorance as I suspect this is a very easy question to answer, but I'm stuck.

I'm using bodymovin to play an svg animation on a website. I want to use the onComplete event to trigger a function when the animation completes, but I can't figure out how to code it.

I've tried

 $("#bodymovin").on("onComplete", function(){
        console.log('test completed');
    });

And

 document.getElementById("bodymovin").addEventListener("complete", doalert);

    function doalert() {
        console.log('test completed');
    }

Bodymovin docs - https://github.com/bodymovin/bodymovin#events

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Luke Seall
  • 525
  • 1
  • 4
  • 21

2 Answers2

13

I figured it out. Here is the entire code

var anim;

var animData = {
    container: document.getElementById('bodymovin'),
    renderer: 'svg',
    loop: false,
    autoplay: true,
    rendererSettings: {
        progressiveLoad:false
    },
    path: 'thelogo.json'
};

anim = bodymovin.loadAnimation(animData);

anim.addEventListener('complete',doalert);

function doalert() {
    console.log('test completed');
}
Aidan Ewen
  • 13,049
  • 8
  • 63
  • 88
Luke Seall
  • 525
  • 1
  • 4
  • 21
3
var animData = {
    container: document.getElementById('bodymovin'),
    renderer: 'svg',
    loop: true,
    autoplay: true,
    path: 'folder_path/data.json'
};

var anim = bodymovin.loadAnimation(animData);

// function for DOM Loading 
anim.addEventListener('DOMLoaded', function(e) { 
    console.log('DOM loaded'); 
});

// function for Completion of animation   
anim.addEventListener('complete', test_complete);

function test_complete() {
    console.log('test completed');
}

// function for certain frame 
anim.addEventListener('enterFrame',enterframe);

function enterframe() {
    console.log('In Frame');
}

//function for Mouse Enter for bodymovin 

anim.addEventListener('DOMLoaded', function(e) {

  var elem = document.getElementById('bodymovin');

  elem.addEventListener('mouseover', mouseElem)

  function mouseElem() {
    anim.goToAndStop(1, true);
  }

});
  • I have given examples to get started with bodymovin events 1. DOM Loaded event. 2. complete event 3. enterFrame event 4. mouseEnter event. – Amrut Dagade Jul 21 '17 at 12:09
  • 1
    Could you elaborate more on the 'enterFrame' event? It sounds like you can listen for when a specific frame in the animation is displayed. Is that the case? – Trev14 Sep 04 '17 at 03:37