eugeneware / gifencoder

Server side animated gif generation for node.js
Other
472 stars 49 forks source link

Typescript @types #46

Open tomgallagher opened 3 years ago

tomgallagher commented 3 years ago

Hey

Thanks for the library - helped me out of a tight spot.

Getting this error when compiling with Typescript.

node_modules/@types/gifencoder/index.d.ts:22:19 - error TS2304: Cannot find name 'CanvasRenderingContext2D'.

22 addFrame(ctx: CanvasRenderingContext2D): void;

Any thoughts?

Tom

VannaDii commented 2 years ago

@tomgallagher, are you still having this issue or did you find a way around it?

VannaDii commented 2 years ago

For anyone else who stumbles into this when running tsc fails for them:

Update your tsconfig.json to include DOM as a lib. Why does this work? Because CanvasRenderingContext2D is a globally available type in the DOM and including this library allows TypeScript to resolve the type reference.

// ... other config ...

    "lib": [
      "DOM",
      // ... others libs
    ],

// ... other config ...
tomgallagher commented 2 years ago

@GioCirque - I think I just built that file in JS in the end. Can't remember. But you can close the issue. Thanks for reply.

VannaDii commented 2 years ago

I also had to cast a Buffer to any because, it seems, the types are defined for web only, and the addFrame signature only takes a CanvasRenderingContext2D.

kavimaluskam commented 2 years ago

@GioCirque I am trying to use this project in typescript but even the new Gifencoder method failed. Do you any clue how to get that working?

VannaDii commented 2 years ago

You can check out my full, working TypeScript implementation over here. I recently started seeing some interesting outputs so I'm exploring a new implementation. I'll let you know if anything comes from it.

VannaDii commented 2 years ago

!!!WARNING !!! DO NOT assign your GIFEncoder instance to undefined after calling finish(). I've observed this clearing the entire GIF on disk and leaving a nearly empty file with just a GIF89a header inside.

VannaDii commented 2 years ago

@kavimaluskam If you're interested, you can also see how I'm adding images via context (maybe overkill) in the snapshotEvent function and the simple finalization in the maybeFinalizeGif function

kavimaluskam commented 2 years ago

Thank you so much @GioCirque , it definitely helped. 🚀