Export DOM Node To GIF With Gif-encoder And Dom-to-image Libs
I am desperately trying to get a non-animated gif export from a DOM Node, on client-side, but without any success yet. I use for that the lib dom-to-image which, thanks to it's met
Solution 1:
So the creator of gif-encoder lib found the solution, and here it is below. If I may quote him :
Our library outputs the GIF as data, not as an DOM element or similar. To aggregate this content, you can collect the stream into a buffer or pass it along to its target
So the proper code for this is finally :
import domtoimage from 'dom-to-image';
import concat from 'concat-stream';
import GifEncoder from 'gif-encoder';
import JSZip from 'jszip';
const pixelsToGIF = (pixels, width, height) =>
new Promise((resolve, reject) => {
const gif = new GifEncoder(width, height);
gif.pipe(concat(resolve));
gif.writeHeader();
gif.addFrame(pixels);
gif.finish();
gif.on('error', reject);
})
const zip = new JSZip();
domtoimage
.toPixelData(DOMNode, { width, height })
.then(pixels => pixelsToGIF(pixels, 'image.gif', width, height))
.then(gif => zip.file('image.gif', gif))
.catch(e => console.log('couldn\'t export to GIF', e));
Post a Comment for "Export DOM Node To GIF With Gif-encoder And Dom-to-image Libs"