jnordberg / gif.js

JavaScript GIF encoding library
http://jnordberg.github.io/gif.js/
MIT License
4.74k stars 668 forks source link

How to trigger "finished" event #69

Closed fleduc closed 7 years ago

fleduc commented 7 years ago

My implementation of gif creator is controlled by an other process loop. I have declared gif this way:

    var gif = new GIF({ workers: 2, quality: 10, debug: true, width: canvas.width, height: canvas.height});
      gif.on('finished', function(blob) {
        window.open(URL.createObjectURL(blob));
      });
    gif.render();

I'm processing all frames (around 300) and add them to gif:

gif.addFrame(context, {copy: true});

Then when I'm finished, I nee to have my Gif animation opened in an other window, but don't know how trigger the "finished" event.

Any help please

1j01 commented 7 years ago

You should only call gif.render once you're done adding frames. Then you should receive a finished event when it's done.

1j01 commented 7 years ago

window.open might need to be called during a user action such as a click though, so as not to be blocked. Try adding a console.log to see if you are actually getting the finished event. You could go with an in-page popup, or you could add a link to the page with target="_blank" or download="creation.gif"

fleduc commented 7 years ago

Thanks!

I didn't catch that this render was to be executed only when all frames were added, since I'm working with three.js which render's must be called on every frame... ;-)

Thanks again