gif.js is a client-side JavaScript library designed for encoding GIFs directly in the browser. It leverages modern browser APIs like Web Workers and Typed Arrays to perform image processing in the background, aiming for fast performance without blocking the main thread. Despite its original aim for efficiency, the project's last stable version is 0.2.0, released over a decade ago (published in 2016, last commit 2017). This indicates it has a virtually non-existent release cadence and is no longer actively maintained. Its key differentiators at the time were its browser-centric approach with Web Worker offloading, which was a significant performance advantage for client-side GIF generation compared to synchronous methods. It accepts image elements, canvas elements, or canvas contexts as frames. However, newer alternatives often offer better performance via WebAssembly and more modern API designs.
npm install gif.jsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize GIF.js, add frames from an image element, a canvas element, and directly from a canvas rendering context. It then renders the GIF and displays it on the page.
Ensure `gif.worker.js` (from the `dist/` folder) is deployed to the same directory as `gif.js` on your web server, or specify its exact URL using the `workerScript` option in the `GIF` constructor: `new GIF({ workerScript: '/path/to/gif.worker.js' })`.For new projects, consider modern alternatives that are actively maintained and offer better performance, broader browser support, and module compatibility. For existing projects, be aware of potential limitations and lack of security patches.
Verify target browser compatibility for Web Workers, File API, and Typed Arrays. For broader support, consider using a polyfill library or a different GIF encoding solution that targets older browser versions.
Always use `gif.addFrame(ctx, { copy: true, delay: 500 });` when providing a canvas context to ensure a snapshot of the pixel data is taken at the time of `addFrame` call.Ensure you have `<script src="dist/gif.js"></script>` in your HTML file, typically before your own scripts that try to use `GIF`. Verify the path to `gif.js` is correct.
Place `gif.worker.js` (from the `dist/` folder of the package) in the same directory as your `gif.js` file, or explicitly configure its path using the `workerScript` option when initializing `GIF`: `new GIF({ workerScript: '/path/to/gif.worker.js' })`.Ensure all image elements are fully loaded before adding them as frames. If using a canvas context, always add `copy: true` to `addFrame` options: `gif.addFrame(ctx, { copy: true })`. Also, check the `width` and `height` options; if `null`, they are determined by the first frame, which might be an issue if the first frame is invalid or not yet ready.No dependency data recorded yet.