Created
December 15, 2014 16:36
-
-
Save antiboredom/129fd2311dec0046603e to your computer and use it in GitHub Desktop.
A simple example showing how to save animated gifs from p5.js sketches, using https://github.com/jnordberg/gif.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src="gif.js"></script> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.11/p5.min.js"></script> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.11/addons/p5.dom.js"></script> | |
<script src="sketch.js"></script> | |
</head> | |
<body> | |
<p>First, allow camera access.<p><p>Then click once to start recording, and another time finish recording and make a gif.</p> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var capture; | |
var recording = false; | |
var c; | |
var gif; | |
function setup() { | |
c = createCanvas(320, 240); | |
capture = createCapture(VIDEO); | |
capture.size(320, 240); | |
capture.hide(); | |
setupGif(); | |
} | |
function draw() { | |
background(255); | |
image(capture, 0, 0, 320, 240); | |
if (recording && frameCount % 3 == 0) { | |
gif.addFrame(c.elt, {delay: 1, copy: true}); | |
} | |
} | |
function mousePressed() { | |
recording = !recording; | |
if (!recording) { | |
gif.render(); | |
} | |
} | |
function setupGif() { | |
gif = new GIF({ | |
workers: 2, | |
quality: 40 | |
}); | |
gif.on('finished', function(blob) { | |
window.open(URL.createObjectURL(blob)); | |
setupGif(); | |
}); | |
} |
Thanks for this, just got this working with only a few tweaks. First I had to add the width and the height to the constructor for GIF. Then I could not get the window.open thing to work so I had to make a link to the ObjectURL instead. It means I have to click a link to get the gif to save, but that is fine with me! Thanks again and I hope this helps anyone trying to get this working!
@Torwegia any chance of sharing how you did the link to save the gif?
bump, I'm having an issue where gif.on('finished') never triggers.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To get this working, make sure to download gif.js and gif.worker.js via https://github.com/jnordberg/gif.js.