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

setTransparent #6

Open
OKNoah opened this issue Oct 8, 2020 · 6 comments
Open

setTransparent #6

OKNoah opened this issue Oct 8, 2020 · 6 comments

Comments

@OKNoah
Copy link

OKNoah commented Oct 8, 2020

Looks like this method doesn't work anymore. Is that because of the new encoder?

@TzunamiDev
Copy link

I have the same problem. For everything else it works fine

@TzunamiDev
Copy link

I just found the solution.

class GIFEncoder extends EventEmitter {
  constructor(width, height, algorithm = 'neuquant', useOptimizer = false, totalFrames = 0) {
    super()
    ...

   this.transparent = null // Change null to true / 1
   ....

}

This appears to be of type boolean and you can assign the value 1 or better yet the value true

image

After writing all the above I realized that none of this is necessary, there is a simple method to change this value without having to touch the library

...
    const encoder = new GIFEncoder(width, height, 'neuquant');
    
    encoder.createReadStream().pipe(fs.createWriteStream('./temp/myimage.gif'));
    
    encoder.start();
    
    encoder.setTransparent(true) // Enable transparency <-----------------
    encoder.setRepeat(0);   // 0 for repeat, -1 for no-repeat
    encoder.setDelay(85);  // frame delay in ms
    encoder.setQuality(10); // image quality. 10 is default.
...

@OKNoah
Copy link
Author

OKNoah commented Oct 24, 2020

@MiguelArenasVillalobos brilliant, i’d been looking closely for that

@pixelsage
Copy link

Still an issue. Doesn't seem to allow me to define what color is actually transparent. Currently turns all black pixels transparent.

@PacaPop
Copy link

PacaPop commented Jan 4, 2022

Same issue here! How to select the transparent color?
What version of the library had this feature working? I think i'm rolling back

@michaljabi
Copy link

I'm using:

import GIFEncoder from 'gif-encoder-2';

/* some code skipped... */

const encoder = new GIFEncoder(width, height, 'neuquant', true, totalFrames );

  • "gif-encoder-2": "^1.0.5",

CODE:

 encoder.setTransparent(true)

IS WRONG.

This is because setTransparent expect you to provide a color in hex to be marked as a transparent inside the GIF.

But here is a "catch":
Method findClosest inside the lib will NOT work properly if you provide this code as e.g: #52ff5d

That is why this CODE:

 encoder.setTransparent('#52ff5d')

IS WRONG.

Explanation: this will mark color to be transparent - but it will mark RGB ( 0, 0, 0 ) - black color to be tranparent. This is because how findClosest works.

---- SOLUTION ----

CODE:

 encoder.setTransparent('0x52ff5d')

IS GOOD 👍

Explanation: this will make findClosest to calculate proper R G B values, because code inside this method looks like this:

var r = (c & 0xff0000) >> 16
var g = (c & 0x00ff00) >> 8
var b = c & 0x0000ff

where c is our "0x52ff5d"

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

No branches or pull requests

5 participants