twolfson / gif-encoder

Streaming GIF encoder
The Unlicense
87 stars 10 forks source link

How to Keep origin gif transparent #19

Closed IveHD closed 3 years ago

IveHD commented 3 years ago

I am trying to split a gif to multi frames, then write some words on it by canvas, finally combine them to a new gif. But when origin gif contains transparent, then new gif is not with transparent no longer. How should I fix it? It seems like that setTransparent() is not work for it.

twolfson commented 3 years ago

It's been a while but I believe setTransparent is used by calling setTransparent(0xFF00FF) (or whatever color you'd like) before the desired addFrame() call (or analyzePixels()/writeImageInfo() if you want to get that low level)

(don't need to do it every call, unless color changes that often)

https://github.com/twolfson/gif-encoder/blob/0.7.2/lib/GIFEncoder.js#L165-L174

https://github.com/twolfson/gif-encoder/blob/0.7.2/lib/GIFEncoder.js#L216-L229

https://github.com/twolfson/gif-encoder/blob/0.7.2/lib/GIFEncoder.js#L309-L318

https://github.com/twolfson/gif-encoder/blob/0.7.2/lib/GIFEncoder.js#L352-L385

https://github.com/twolfson/gif-encoder/blob/0.7.2/lib/GIFEncoder.js#L194-L214