Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

array of images #12

Open
fabulousduck opened this issue Nov 10, 2016 · 3 comments
Open

array of images #12

fabulousduck opened this issue Nov 10, 2016 · 3 comments
Labels

Comments

@fabulousduck
Copy link

is it possible to hand the encoder multiple local images and convert them to a gif ?

thx

@heikkipora
Copy link
Collaborator

@fabulousduck Could you please elaborate what you mean with "multiple local images" ?
Also, is this question still relevant to you?

@fabulousduck
Copy link
Author

It isn't relevant to me, but maybe it is to others, so i will elaborate.

Currently if i want to make a gif of multiple images, i would need to map over an array and add each frame individually.
wouldn't it be much nicer to just have a function called
encoder.addFrames(['img1.jpg','img2.jpg'])
instead of

    return Promise.map(base64Images, function addFrameToEncoder(base64Image){
        return encoder.addFrame(gif, base64Image,ctx,width,height)
    })

?

@yasso1am
Copy link

yasso1am commented Apr 8, 2019

const generateGif = (imagePathsArray) => {
    const encoder = new GIFEncoder(195, 180);
    const canvas = createCanvas(195, 180);
    const ctx = canvas.getContext('2d');
    const img = new CanvasImage();
        img.onload = () => ctx.drawImage(img, 0, 0)
        img.onerror = err => { throw err }
        encoder.createReadStream().pipe(fs.createWriteStream('myanimated.gif'))
        encoder.start();
        encoder.setRepeat(0);
        encoder.setDelay(500);
        encoder.setQuality(10);
            for (let path of imagePathsArray ) {
                try {
                    img.src = `${path}.png`
                    encoder.addFrame(ctx)
                    if (path === imagePathsArray[imagePathsArray.length - 1 ]) {
                        encoder.finish()
                    }
                } catch (err) {
                    if (err) {
                        console.log(err)
                        return
                    }
                }
            }
}

This is what I'm doing to generate GIFs from an array of image paths.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants