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 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 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(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bump, I'm having an issue where gif.on('finished') never triggers.