Skip to content

Instantly share code, notes, and snippets.

@dmitryshelomanov
Created January 19, 2018 15:04
Show Gist options
  • Save dmitryshelomanov/32b78fd38b6385938d8a9f769afc13a0 to your computer and use it in GitHub Desktop.
Save dmitryshelomanov/32b78fd38b6385938d8a9f769afc13a0 to your computer and use it in GitHub Desktop.
module.exports = function wrapGif(imgData, pathReadyGif) {
const { w, h, data, repeat } = imgData
const gif = new GIFEncoder(w, h)
const stream = fs.createWriteStream(pathReadyGif)
let counter = 0
gif.pipe(stream)
gif.setQuality(10)
gif.setDelay(0)
gif.writeHeader()
gif.setRepeat(-1)
return new Promise((res, rej) => {
function addToGif() {
debug('add to gif with counter - ', counter)
getPixels(replaceOriginalToGif(data[counter].path), (error, pixels) => {
if (error) return rej()
gif.setDelay(data[counter].delay)
gif.addFrame(pixels.data)
gif.read()
if (counter === data.length - 1) {
gif.finish()
return res()
}
counter++
addToGif()
})
}
addToGif()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment