parallax/jsPDF · error · Error

Must supply either a local or global palette.

Error message

Must supply either a local or global palette.

What it means

Thrown by GifWriter.addFrame when neither a local palette (opts.palette) nor a global palette (gopts.palette passed to the GifWriter constructor) is available. Every frame needs a color table to decode its indices; addFrame falls back from local to global and errors if both are absent.

Source

Thrown at src/libs/omggif.js:161

    // canvas width/height, I imagine?
    if (x < 0 || y < 0 || x > 65535 || y > 65535)
      throw new Error("x/y invalid.");

    if (w <= 0 || h <= 0 || w > 65535 || h > 65535)
      throw new Error("Width/Height invalid.");

    if (indexed_pixels.length < w * h)
      throw new Error("Not enough pixels for the frame size.");

    var using_local_palette = true;
    var palette = opts.palette;
    if (palette === undefined || palette === null) {
      using_local_palette = false;
      palette = global_palette;
    }

    if (palette === undefined || palette === null)
      throw new Error("Must supply either a local or global palette.");

    var num_colors = check_palette_and_num_colors(palette);

    // Compute the min_code_size (power of 2), destroying num_colors.
    var min_code_size = 0;
    while ((num_colors >>= 1)) ++min_code_size;
    num_colors = 1 << min_code_size; // Now we can easily get it back.

    var delay = opts.delay === undefined ? 0 : opts.delay;

    // From the spec:
    //     0 -   No disposal specified. The decoder is
    //           not required to take any action.
    //     1 -   Do not dispose. The graphic is to be left
    //           in place.
    //     2 -   Restore to background color. The area used by the
    //           graphic must be restored to the background color.
    //     3 -   Restore to previous. The decoder is required to

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Supply gopts.palette when creating the GifWriter for a shared global palette.
  2. Or supply opts.palette on every addFrame call for per-frame palettes.
  3. Centralize palette resolution so every frame path provides one.

Example fix

// before
var gw = new GifWriter(buf, w, h, {});
gw.addFrame(0, 0, w, h, pixels); // no palette anywhere
// after
var gw = new GifWriter(buf, w, h, { palette: globalPal });
gw.addFrame(0, 0, w, h, pixels);
Defensive patterns

Strategy: validation

Validate before calling

function ensurePalette(gopts, frameOpts){ return frameOpts.palette || gopts.palette || null; }
var pal = ensurePalette(gopts, opts); if (pal) gw.addFrame(x, y, w, h, pixels, pal ? { palette: pal } : opts);

Type guard

function hasPaletteAvailable(gopts, opts){ return !!(opts && opts.palette) || !!(gopts && gopts.palette); }

Try / catch

try { gw.addFrame(x, y, w, h, pixels, opts); } catch (e) { if (/local or global palette/.test(e.message)) { opts.palette = defaultPalette; gw.addFrame(x, y, w, h, pixels, opts); } else throw e; }

Prevention

When it happens

Trigger: Constructing GifWriter without gopts.palette and then calling addFrame without opts.palette; passing opts.palette = null explicitly; clearing the global palette reference.

Common situations: Single-frame GIFs where the author expected the palette to be inferred (omggif does not infer); animation code that only sets palettes per-frame on some frames.

Related errors


AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13). Data as JSON: /api/errors/971fb97308f7e59e. Report an issue: GitHub.