I am trying to record a gif of drawing on canvas. I am using this library https://github.com/antimatter15/jsgif to save canvas output as animated .GIF
For a proof of concept I am using a pure JS canvas drawing script - http://zipso.net/sketchpad/sketchpad-lines.html
Below you can find the init function - I get a black image and cannot add new frames to gif. How can I fix this? Any advice appreciated.
function init() {
// Get the specific canvas element from the HTML document
canvas = document.getElementById('sketchpad');
// If the browser supports the canvas tag, get the 2d drawing context for this canvas
if (canvas.getContext)
ctx = canvas.getContext('2d');
var encoder = new GIFEncoder();
encoder.setRepeat(0);
encoder.setDelay(100); //go to next frame every n milliseconds
encoder.start();
// Check that we have a valid context to draw on/with before adding event handlers
if (ctx) {
// React to mouse events on the canvas, and mouseup on the entire document
canvas.addEventListener('mousedown', sketchpad_mouseDown, false);
canvas.addEventListener('mousemove', sketchpad_mouseMove, false);
window.addEventListener('mouseup', sketchpad_mouseUp, false);
// React to touch events on the canvas
canvas.addEventListener('touchstart', sketchpad_touchStart, false);
canvas.addEventListener('touchend', sketchpad_touchEnd, false);
canvas.addEventListener('touchmove', sketchpad_touchMove, false);
}
encoder.addFrame(ctx);
encoder.finish();
document.getElementById('image').src = 'data:image/gif;base64,'+encode64(encoder.stream().getData())
}