Created
January 19, 2018 15:04
-
-
Save dmitryshelomanov/32b78fd38b6385938d8a9f769afc13a0 to your computer and use it in GitHub Desktop.
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
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