How to use events with bodymovin.js
Asked Answered
H

2

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

Hispaniola answered 10/2, 2017 at 9:13 Comment(0)
H
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');
}
Hispaniola answered 10/2, 2017 at 9:20 Comment(0)
N
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);
  }

});
Nicolis answered 21/7, 2017 at 11:28 Comment(2)
I have given examples to get started with bodymovin events 1. DOM Loaded event. 2. complete event 3. enterFrame event 4. mouseEnter event.Nicolis
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?Reexamine

© 2022 - 2024 — McMap. All rights reserved.