gifencoder icon indicating copy to clipboard operation
gifencoder copied to clipboard

Typescript @types

Open tomgallagher opened this issue 5 years ago • 9 comments

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

tomgallagher avatar Dec 09 '20 13:12 tomgallagher

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

VannaDii avatar Dec 08 '21 15:12 VannaDii

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 ...

VannaDii avatar Dec 08 '21 15:12 VannaDii

@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.

tomgallagher avatar Dec 08 '21 15:12 tomgallagher

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.

VannaDii avatar Dec 08 '21 16:12 VannaDii

@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?

kavimaluskam avatar Dec 10 '21 21:12 kavimaluskam

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 avatar Dec 10 '21 22:12 VannaDii

!!!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 avatar Dec 10 '21 22:12 VannaDii

@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

VannaDii avatar Dec 10 '21 22:12 VannaDii

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

kavimaluskam avatar Dec 13 '21 12:12 kavimaluskam