Efficient JavaScript GIF Parser and Decoder

2.1.2 · active · verified Sun Apr 19

gifuct-js is a lean and efficient JavaScript library specifically designed for parsing and decoding GIF files. It aims to overcome the inefficiencies and complexities found in older GIF processing libraries by providing a streamlined API focused solely on extracting raw frame data. The current stable version is 2.1.2, last updated in November 2021. The library prioritizes performance, making it suitable for resource-constrained environments like mobile hybrid applications, as exemplified by its original development for the Ruffle project. It operates by consuming GIF files as `Uint8Array` buffers and leverages `js-binary-schema-parser` internally for robust parsing. A key differentiator is its 'decode-only' approach; unlike many alternatives, it deliberately omits any built-in drawing or rendering logic, empowering developers to integrate GIF data with their preferred rendering engines (e.g., Canvas, WebGL). While it can optionally generate canvas-ready `Uint8ClampedArray` patches, it leaves full control over animation and display to the implementer. The project maintains a stable release cadence, with updates primarily focusing on parsing accuracy and performance enhancements rather than frequent new feature introductions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to fetch a GIF from a URL, parse its `ArrayBuffer`, and decompress its frames (with patches) for further processing.

import { parseGIF, decompressFrames } from 'gifuct-js';

const gifURL = 'https://i.imgur.com/DrxJ5d8.gif'; // Replace with your GIF URL

async function loadAndDecompressGif(url) {
  try {
    const resp = await fetch(url);
    if (!resp.ok) {
      throw new Error(`HTTP error! status: ${resp.status}`);
    }
    const arrayBuffer = await resp.arrayBuffer();
    const gif = parseGIF(arrayBuffer);
    // Set buildPatch to true to get canvas-ready Uint8ClampedArray patches
    const frames = decompressFrames(gif, true);

    console.log('GIF parsed successfully. Number of frames:', frames.length);
    // Example of accessing frame data
    if (frames.length > 0) {
      const firstFrame = frames[0];
      console.log('First frame dimensions:', firstFrame.dims);
      console.log('First frame delay (ms):', firstFrame.delay);
      // firstFrame.patch is a Uint8ClampedArray if buildPatch was true
      // firstFrame.pixels is an array of color table indices
    }
    return frames;
  } catch (error) {
    console.error('Failed to load or decompress GIF:', error);
    return [];
  }
}

loadAndDecompressGif(gifURL).then(frames => {
  // Now you can work with the decompressed frames, e.g., draw them to a canvas
  // For full rendering example, refer to the library's demo.
});

view raw JSON →