vizmatic 0.1.36 → 0.1.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -443,7 +443,7 @@ export const watermark = (
443
443
  )
444
444
  ```
445
445
 
446
- Prefer `defineAnimation` when values should move, resize, or fade continuously. Timeline array runs sequentially; every property in one `tween` moves in parallel. `parallel` adds independent property tracks for staggered or overlapping motion. Renderer samples absolute time deterministically and includes exact terminal state. Keep `create(theme)` as static fallback for PNG, docs previews, and reduced-motion users.
446
+ Prefer `defineAnimation` when values should move, resize, or fade continuously. Timeline array runs sequentially; every property in one `tween` moves in parallel. `parallel` adds independent property tracks for staggered or overlapping motion. Renderer samples absolute time deterministically and includes exact initial and terminal states. Keep `create(theme)` as static fallback for PNG, docs previews, and reduced-motion users.
447
447
 
448
448
  ```tsx
449
449
  import { defineAnimation, hold, keyframe, tween } from "vizmatic"
@@ -475,7 +475,7 @@ await renderAnimationGif(createAnimation("dark"), {
475
475
  })
476
476
  ```
477
477
 
478
- `createScenes(theme)` and `renderAnimatedGif(AnimatedScene[])` remain supported for intentional scene cuts and pixel crossfades. GIF output uses centisecond timing, at most 256 colors per frame, and one-bit transparency. Use PNG/SVG for smooth alpha edges.
478
+ `createScenes(theme)` and `renderAnimatedGif(AnimatedScene[])` remain supported for intentional scene cuts and pixel crossfades. `fps` controls their transition sampling as well as typed timelines. GIF output uses one shared 256-color palette, centisecond timing, and one-bit transparency. Prefer 10, 20, 25, or 50 FPS and align tween or transition durations to frame intervals, such as multiples of 50 ms at 20 FPS. Other timings alternate centisecond delays to preserve total duration. Use PNG/SVG for smooth alpha edges.
479
479
 
480
480
  ## Development
481
481
 
package/dist/cli.js CHANGED
@@ -7227,10 +7227,10 @@ function frameDelays(duration, count) {
7227
7227
  function sampleAnimationFrames(animation, options = {}) {
7228
7228
  const fps = options.fps ?? animation.fps;
7229
7229
  assertFps(fps);
7230
- const count = Math.max(1, Math.floor(animation.duration / 1e3 * fps));
7230
+ const count = Math.max(2, Math.floor(animation.duration / 1e3 * fps));
7231
7231
  const delays = frameDelays(animation.duration, count);
7232
7232
  return delays.map((delay, index) => {
7233
- const time = animation.duration * ((index + 1) / count);
7233
+ const time = animation.duration * (index / (count - 1));
7234
7234
  const evaluated = evaluateAnimation(animation, time);
7235
7235
  return {
7236
7236
  state: evaluated.state,
@@ -7262,7 +7262,9 @@ function resolveGifenc(api) {
7262
7262
  throw new Error("gifenc exports could not be resolved");
7263
7263
  }
7264
7264
  var { GIFEncoder, applyPalette, quantize } = resolveGifenc(gifenc);
7265
- async function renderToPixels(element, width, height, scale, background) {
7265
+ var MAX_PALETTE_SAMPLE_PIXELS = 1e6;
7266
+ var MAX_TIMELINE_PALETTE_FRAMES = 24;
7267
+ async function renderToPixels(element, width, height, scale, theme, background) {
7266
7268
  const fonts = await getFonts();
7267
7269
  const svg = await withRenderContext({ background }, () => satori(element, {
7268
7270
  width,
@@ -7272,7 +7274,7 @@ async function renderToPixels(element, width, height, scale, background) {
7272
7274
  }));
7273
7275
  const resvg = new Resvg2(svg, {
7274
7276
  fitTo: { mode: "width", value: width * scale },
7275
- background: "rgba(0, 0, 0, 0)"
7277
+ background: rasterBackground(background, theme)
7276
7278
  });
7277
7279
  const rendered = resvg.render();
7278
7280
  return {
@@ -7324,6 +7326,10 @@ function colorForBackground(background, theme) {
7324
7326
  const [r, g, b] = parsed.type === "hsl" ? hslToRgb(parsed.values[0], parsed.values[1], parsed.values[2]) : [parsed.values[0], parsed.values[1], parsed.values[2]];
7325
7327
  return { r, g, b, a: Math.round(parsed.alpha * 255) };
7326
7328
  }
7329
+ function rasterBackground(background, theme) {
7330
+ const { r, g, b, a } = colorForBackground(background, theme);
7331
+ return `rgba(${r}, ${g}, ${b}, ${a / 255})`;
7332
+ }
7327
7333
  function backgroundPixels(width, height, theme, background) {
7328
7334
  const buffer = new Uint8Array(width * height * 4);
7329
7335
  const color = colorForBackground(background, theme);
@@ -7335,27 +7341,71 @@ function backgroundPixels(width, height, theme, background) {
7335
7341
  }
7336
7342
  return buffer;
7337
7343
  }
7344
+ function samplePixels(pixels, limit) {
7345
+ const pixelCount = pixels.length / 4;
7346
+ if (pixelCount <= limit) return pixels.slice();
7347
+ const sample = new Uint8Array(limit * 4);
7348
+ for (let index = 0; index < limit; index += 1) {
7349
+ const sourcePixel = Math.floor((index + 0.5) * pixelCount / limit);
7350
+ sample.set(pixels.subarray(sourcePixel * 4, sourcePixel * 4 + 4), index * 4);
7351
+ }
7352
+ return sample;
7353
+ }
7354
+ function sharedPalette(frames) {
7355
+ if (frames.length === 0) throw new Error("No GIF frames to quantize");
7356
+ const pixelsPerFrame = Math.max(1, Math.floor(MAX_PALETTE_SAMPLE_PIXELS / frames.length));
7357
+ const samples = frames.map((frame) => samplePixels(frame.pixels, pixelsPerFrame));
7358
+ const length = samples.reduce((total, sample) => total + sample.length, 0);
7359
+ const combined = new Uint8Array(length);
7360
+ let offset = 0;
7361
+ for (const sample of samples) {
7362
+ combined.set(sample, offset);
7363
+ offset += sample.length;
7364
+ }
7365
+ return quantize(combined, 256, { format: "rgba4444", oneBitAlpha: true });
7366
+ }
7338
7367
  function encodeGif(frames, loop) {
7339
7368
  if (frames.length === 0) throw new Error("No GIF frames to encode");
7340
7369
  const firstFrame = frames[0];
7341
7370
  const gif = GIFEncoder();
7371
+ const palette = sharedPalette(frames);
7372
+ let first = true;
7342
7373
  for (const frame of frames) {
7343
7374
  if (frame.width !== firstFrame.width || frame.height !== firstFrame.height) {
7344
7375
  throw new Error("All GIF frames must have matching dimensions");
7345
7376
  }
7346
- writeGifFrame(gif, frame, loop);
7377
+ writeGifFrame(gif, frame, loop, palette, first);
7378
+ first = false;
7347
7379
  }
7348
7380
  gif.finish();
7349
7381
  return gif.bytes();
7350
7382
  }
7351
- var TRANSITION_FPS = 15;
7383
+ var DEFAULT_TRANSITION_FPS = 20;
7352
7384
  var MIN_TRANSITION_FRAMES = 3;
7385
+ function sceneTransitionFps(fps) {
7386
+ const resolved = fps ?? DEFAULT_TRANSITION_FPS;
7387
+ if (!Number.isInteger(resolved) || resolved < 1 || resolved > 50) {
7388
+ throw new Error("Animation fps must be an integer from 1 to 50");
7389
+ }
7390
+ return resolved;
7391
+ }
7392
+ function transitionFrameDelays(duration, count) {
7393
+ const totalCentiseconds = Math.max(count * 2, Math.round(duration / 10));
7394
+ let previous = 0;
7395
+ return Array.from({ length: count }, (_, index) => {
7396
+ const cumulative = Math.round((index + 1) / count * totalCentiseconds);
7397
+ const delay = (cumulative - previous) * 10;
7398
+ previous = cumulative;
7399
+ return delay;
7400
+ });
7401
+ }
7353
7402
  async function scenesToFrames(scenes, options) {
7354
7403
  if (scenes.length === 0) throw new Error("Animated GIF requires at least one scene");
7404
+ const fps = sceneTransitionFps(options.fps);
7355
7405
  const renderedScenes = await Promise.all(scenes.map((scene) => {
7356
7406
  const watermark = options.watermark ?? options.brand;
7357
7407
  const element = watermark ? wrapWithWatermark(scene.element, options.width, options.height, options.theme, watermark) : scene.element;
7358
- return renderToPixels(element, options.width, options.height, options.scale, options.background);
7408
+ return renderToPixels(element, options.width, options.height, options.scale, options.theme, options.background);
7359
7409
  }));
7360
7410
  const frames = [];
7361
7411
  const scaledWidth = renderedScenes[0].width;
@@ -7373,13 +7423,13 @@ async function scenesToFrames(scenes, options) {
7373
7423
  };
7374
7424
  const transitionFrames = Math.max(
7375
7425
  MIN_TRANSITION_FRAMES,
7376
- Math.round(transitionDuration / 1e3 * TRANSITION_FPS)
7426
+ Math.round(transitionDuration / 1e3 * fps)
7377
7427
  );
7378
- const frameDelay = transitionDuration / transitionFrames;
7428
+ const frameDelays2 = transitionFrameDelays(transitionDuration, transitionFrames);
7379
7429
  for (let frameIndex = 0; frameIndex < transitionFrames; frameIndex += 1) {
7380
7430
  const amount = (frameIndex + 1) / transitionFrames;
7381
7431
  const pixels = transition === "fade" ? blendPixels(previous.pixels, current.pixels, amount) : blendPixels(backgroundPixels(scaledWidth, scaledHeight, options.theme, options.background), current.pixels, amount);
7382
- frames.push({ pixels, delay: frameDelay, width: scaledWidth, height: scaledHeight });
7432
+ frames.push({ pixels, delay: frameDelays2[frameIndex], width: scaledWidth, height: scaledHeight });
7383
7433
  }
7384
7434
  }
7385
7435
  frames.push({
@@ -7419,23 +7469,32 @@ function pixelsEqual(left, right) {
7419
7469
  }
7420
7470
  return true;
7421
7471
  }
7422
- function writeGifFrame(gif, frame, loop) {
7423
- const palette = quantize(frame.pixels, 256, { format: "rgba4444", oneBitAlpha: true });
7472
+ function writeGifFrame(gif, frame, loop, palette, first) {
7424
7473
  const index = applyPalette(frame.pixels, palette, "rgba4444");
7425
7474
  const transparentIndex = palette.findIndex((color) => color[3] === 0);
7426
7475
  let remainingDelay = frame.delay;
7476
+ let includePalette = first;
7427
7477
  do {
7428
7478
  const delay = Math.min(655350, remainingDelay);
7429
7479
  gif.writeFrame(index, frame.width, frame.height, {
7430
- palette,
7480
+ ...includePalette ? { palette } : {},
7431
7481
  delay: Math.max(20, delay),
7432
7482
  repeat: loop,
7433
7483
  transparent: transparentIndex >= 0,
7434
7484
  transparentIndex
7435
7485
  });
7486
+ includePalette = false;
7436
7487
  remainingDelay -= delay;
7437
7488
  } while (remainingDelay > 0);
7438
7489
  }
7490
+ function representativeFrameIndexes(frameCount) {
7491
+ const count = Math.min(frameCount, MAX_TIMELINE_PALETTE_FRAMES);
7492
+ if (count === frameCount) return Array.from({ length: count }, (_, index) => index);
7493
+ return Array.from(
7494
+ { length: count },
7495
+ (_, index) => Math.round(index * (frameCount - 1) / (count - 1))
7496
+ );
7497
+ }
7439
7498
  async function renderAnimationGif(animation, options) {
7440
7499
  await renderAnimationGifWithOutput(animation, options);
7441
7500
  }
@@ -7448,9 +7507,33 @@ async function renderAnimationGifWithOutput(animation, options) {
7448
7507
  background: options.background ?? "theme"
7449
7508
  };
7450
7509
  const frames = sampleAnimationFrames(animation, { fps: options.fps });
7510
+ const paletteFrames = [];
7511
+ const paletteIndexes = representativeFrameIndexes(frames.length);
7512
+ const palettePixelsPerFrame = Math.max(1, Math.floor(MAX_PALETTE_SAMPLE_PIXELS / paletteIndexes.length));
7513
+ for (const index of paletteIndexes) {
7514
+ const sampled = frames[index];
7515
+ const rawElement = animation.render(sampled.state, sampled.frame);
7516
+ const watermark = normalized.watermark ?? normalized.brand;
7517
+ const element = watermark ? wrapWithWatermark(rawElement, normalized.width, normalized.height, normalized.theme, watermark) : rawElement;
7518
+ const rendered = await renderToPixels(
7519
+ element,
7520
+ normalized.width,
7521
+ normalized.height,
7522
+ normalized.scale,
7523
+ normalized.theme,
7524
+ normalized.background
7525
+ );
7526
+ paletteFrames.push({
7527
+ ...rendered,
7528
+ pixels: samplePixels(rendered.pixels, palettePixelsPerFrame),
7529
+ delay: sampled.frame.delay
7530
+ });
7531
+ }
7532
+ const palette = sharedPalette(paletteFrames);
7451
7533
  const gif = GIFEncoder();
7452
7534
  let pending;
7453
7535
  let dimensions;
7536
+ let first = true;
7454
7537
  for (const sampled of frames) {
7455
7538
  const rawElement = animation.render(sampled.state, sampled.frame);
7456
7539
  const watermark = normalized.watermark ?? normalized.brand;
@@ -7460,6 +7543,7 @@ async function renderAnimationGifWithOutput(animation, options) {
7460
7543
  normalized.width,
7461
7544
  normalized.height,
7462
7545
  normalized.scale,
7546
+ normalized.theme,
7463
7547
  normalized.background
7464
7548
  );
7465
7549
  dimensions ??= { width: rendered.width, height: rendered.height };
@@ -7470,11 +7554,14 @@ async function renderAnimationGifWithOutput(animation, options) {
7470
7554
  pending.delay += sampled.frame.delay;
7471
7555
  continue;
7472
7556
  }
7473
- if (pending) writeGifFrame(gif, pending, normalized.loop);
7557
+ if (pending) {
7558
+ writeGifFrame(gif, pending, normalized.loop, palette, first);
7559
+ first = false;
7560
+ }
7474
7561
  pending = { ...rendered, delay: sampled.frame.delay };
7475
7562
  }
7476
7563
  if (!pending || !dimensions) throw new Error("Animated GIF requires at least one frame");
7477
- writeGifFrame(gif, pending, normalized.loop);
7564
+ writeGifFrame(gif, pending, normalized.loop, palette, first);
7478
7565
  gif.finish();
7479
7566
  await mkdir2(dirname2(options.outputPath), { recursive: true });
7480
7567
  await writeFile2(options.outputPath, gif.bytes());
package/dist/index.cjs CHANGED
@@ -7240,10 +7240,10 @@ function frameDelays(duration, count) {
7240
7240
  function sampleAnimationFrames(animation, options = {}) {
7241
7241
  const fps = options.fps ?? animation.fps;
7242
7242
  assertFps(fps);
7243
- const count = Math.max(1, Math.floor(animation.duration / 1e3 * fps));
7243
+ const count = Math.max(2, Math.floor(animation.duration / 1e3 * fps));
7244
7244
  const delays = frameDelays(animation.duration, count);
7245
7245
  return delays.map((delay, index) => {
7246
- const time = animation.duration * ((index + 1) / count);
7246
+ const time = animation.duration * (index / (count - 1));
7247
7247
  const evaluated = evaluateAnimation(animation, time);
7248
7248
  return {
7249
7249
  state: evaluated.state,
@@ -7275,7 +7275,9 @@ function resolveGifenc(api) {
7275
7275
  throw new Error("gifenc exports could not be resolved");
7276
7276
  }
7277
7277
  var { GIFEncoder, applyPalette, quantize } = resolveGifenc(gifenc);
7278
- async function renderToPixels(element, width, height, scale, background) {
7278
+ var MAX_PALETTE_SAMPLE_PIXELS = 1e6;
7279
+ var MAX_TIMELINE_PALETTE_FRAMES = 24;
7280
+ async function renderToPixels(element, width, height, scale, theme, background) {
7279
7281
  const fonts = await getFonts();
7280
7282
  const svg = await withRenderContext({ background }, () => satori(element, {
7281
7283
  width,
@@ -7285,7 +7287,7 @@ async function renderToPixels(element, width, height, scale, background) {
7285
7287
  }));
7286
7288
  const resvg = new import_resvg_js2.Resvg(svg, {
7287
7289
  fitTo: { mode: "width", value: width * scale },
7288
- background: "rgba(0, 0, 0, 0)"
7290
+ background: rasterBackground(background, theme)
7289
7291
  });
7290
7292
  const rendered = resvg.render();
7291
7293
  return {
@@ -7337,6 +7339,10 @@ function colorForBackground(background, theme) {
7337
7339
  const [r, g, b] = parsed.type === "hsl" ? hslToRgb(parsed.values[0], parsed.values[1], parsed.values[2]) : [parsed.values[0], parsed.values[1], parsed.values[2]];
7338
7340
  return { r, g, b, a: Math.round(parsed.alpha * 255) };
7339
7341
  }
7342
+ function rasterBackground(background, theme) {
7343
+ const { r, g, b, a } = colorForBackground(background, theme);
7344
+ return `rgba(${r}, ${g}, ${b}, ${a / 255})`;
7345
+ }
7340
7346
  function backgroundPixels(width, height, theme, background) {
7341
7347
  const buffer = new Uint8Array(width * height * 4);
7342
7348
  const color = colorForBackground(background, theme);
@@ -7348,27 +7354,71 @@ function backgroundPixels(width, height, theme, background) {
7348
7354
  }
7349
7355
  return buffer;
7350
7356
  }
7357
+ function samplePixels(pixels, limit) {
7358
+ const pixelCount = pixels.length / 4;
7359
+ if (pixelCount <= limit) return pixels.slice();
7360
+ const sample = new Uint8Array(limit * 4);
7361
+ for (let index = 0; index < limit; index += 1) {
7362
+ const sourcePixel = Math.floor((index + 0.5) * pixelCount / limit);
7363
+ sample.set(pixels.subarray(sourcePixel * 4, sourcePixel * 4 + 4), index * 4);
7364
+ }
7365
+ return sample;
7366
+ }
7367
+ function sharedPalette(frames) {
7368
+ if (frames.length === 0) throw new Error("No GIF frames to quantize");
7369
+ const pixelsPerFrame = Math.max(1, Math.floor(MAX_PALETTE_SAMPLE_PIXELS / frames.length));
7370
+ const samples = frames.map((frame) => samplePixels(frame.pixels, pixelsPerFrame));
7371
+ const length = samples.reduce((total, sample) => total + sample.length, 0);
7372
+ const combined = new Uint8Array(length);
7373
+ let offset = 0;
7374
+ for (const sample of samples) {
7375
+ combined.set(sample, offset);
7376
+ offset += sample.length;
7377
+ }
7378
+ return quantize(combined, 256, { format: "rgba4444", oneBitAlpha: true });
7379
+ }
7351
7380
  function encodeGif(frames, loop) {
7352
7381
  if (frames.length === 0) throw new Error("No GIF frames to encode");
7353
7382
  const firstFrame = frames[0];
7354
7383
  const gif = GIFEncoder();
7384
+ const palette = sharedPalette(frames);
7385
+ let first = true;
7355
7386
  for (const frame of frames) {
7356
7387
  if (frame.width !== firstFrame.width || frame.height !== firstFrame.height) {
7357
7388
  throw new Error("All GIF frames must have matching dimensions");
7358
7389
  }
7359
- writeGifFrame(gif, frame, loop);
7390
+ writeGifFrame(gif, frame, loop, palette, first);
7391
+ first = false;
7360
7392
  }
7361
7393
  gif.finish();
7362
7394
  return gif.bytes();
7363
7395
  }
7364
- var TRANSITION_FPS = 15;
7396
+ var DEFAULT_TRANSITION_FPS = 20;
7365
7397
  var MIN_TRANSITION_FRAMES = 3;
7398
+ function sceneTransitionFps(fps) {
7399
+ const resolved = fps ?? DEFAULT_TRANSITION_FPS;
7400
+ if (!Number.isInteger(resolved) || resolved < 1 || resolved > 50) {
7401
+ throw new Error("Animation fps must be an integer from 1 to 50");
7402
+ }
7403
+ return resolved;
7404
+ }
7405
+ function transitionFrameDelays(duration, count) {
7406
+ const totalCentiseconds = Math.max(count * 2, Math.round(duration / 10));
7407
+ let previous = 0;
7408
+ return Array.from({ length: count }, (_, index) => {
7409
+ const cumulative = Math.round((index + 1) / count * totalCentiseconds);
7410
+ const delay = (cumulative - previous) * 10;
7411
+ previous = cumulative;
7412
+ return delay;
7413
+ });
7414
+ }
7366
7415
  async function scenesToFrames(scenes, options) {
7367
7416
  if (scenes.length === 0) throw new Error("Animated GIF requires at least one scene");
7417
+ const fps = sceneTransitionFps(options.fps);
7368
7418
  const renderedScenes = await Promise.all(scenes.map((scene) => {
7369
7419
  const watermark = options.watermark ?? options.brand;
7370
7420
  const element = watermark ? wrapWithWatermark(scene.element, options.width, options.height, options.theme, watermark) : scene.element;
7371
- return renderToPixels(element, options.width, options.height, options.scale, options.background);
7421
+ return renderToPixels(element, options.width, options.height, options.scale, options.theme, options.background);
7372
7422
  }));
7373
7423
  const frames = [];
7374
7424
  const scaledWidth = renderedScenes[0].width;
@@ -7386,13 +7436,13 @@ async function scenesToFrames(scenes, options) {
7386
7436
  };
7387
7437
  const transitionFrames = Math.max(
7388
7438
  MIN_TRANSITION_FRAMES,
7389
- Math.round(transitionDuration / 1e3 * TRANSITION_FPS)
7439
+ Math.round(transitionDuration / 1e3 * fps)
7390
7440
  );
7391
- const frameDelay = transitionDuration / transitionFrames;
7441
+ const frameDelays2 = transitionFrameDelays(transitionDuration, transitionFrames);
7392
7442
  for (let frameIndex = 0; frameIndex < transitionFrames; frameIndex += 1) {
7393
7443
  const amount = (frameIndex + 1) / transitionFrames;
7394
7444
  const pixels = transition === "fade" ? blendPixels(previous.pixels, current.pixels, amount) : blendPixels(backgroundPixels(scaledWidth, scaledHeight, options.theme, options.background), current.pixels, amount);
7395
- frames.push({ pixels, delay: frameDelay, width: scaledWidth, height: scaledHeight });
7445
+ frames.push({ pixels, delay: frameDelays2[frameIndex], width: scaledWidth, height: scaledHeight });
7396
7446
  }
7397
7447
  }
7398
7448
  frames.push({
@@ -7432,23 +7482,32 @@ function pixelsEqual(left, right) {
7432
7482
  }
7433
7483
  return true;
7434
7484
  }
7435
- function writeGifFrame(gif, frame, loop) {
7436
- const palette = quantize(frame.pixels, 256, { format: "rgba4444", oneBitAlpha: true });
7485
+ function writeGifFrame(gif, frame, loop, palette, first) {
7437
7486
  const index = applyPalette(frame.pixels, palette, "rgba4444");
7438
7487
  const transparentIndex = palette.findIndex((color) => color[3] === 0);
7439
7488
  let remainingDelay = frame.delay;
7489
+ let includePalette = first;
7440
7490
  do {
7441
7491
  const delay = Math.min(655350, remainingDelay);
7442
7492
  gif.writeFrame(index, frame.width, frame.height, {
7443
- palette,
7493
+ ...includePalette ? { palette } : {},
7444
7494
  delay: Math.max(20, delay),
7445
7495
  repeat: loop,
7446
7496
  transparent: transparentIndex >= 0,
7447
7497
  transparentIndex
7448
7498
  });
7499
+ includePalette = false;
7449
7500
  remainingDelay -= delay;
7450
7501
  } while (remainingDelay > 0);
7451
7502
  }
7503
+ function representativeFrameIndexes(frameCount) {
7504
+ const count = Math.min(frameCount, MAX_TIMELINE_PALETTE_FRAMES);
7505
+ if (count === frameCount) return Array.from({ length: count }, (_, index) => index);
7506
+ return Array.from(
7507
+ { length: count },
7508
+ (_, index) => Math.round(index * (frameCount - 1) / (count - 1))
7509
+ );
7510
+ }
7452
7511
  async function renderAnimationGif(animation, options) {
7453
7512
  await renderAnimationGifWithOutput(animation, options);
7454
7513
  }
@@ -7461,9 +7520,33 @@ async function renderAnimationGifWithOutput(animation, options) {
7461
7520
  background: options.background ?? "theme"
7462
7521
  };
7463
7522
  const frames = sampleAnimationFrames(animation, { fps: options.fps });
7523
+ const paletteFrames = [];
7524
+ const paletteIndexes = representativeFrameIndexes(frames.length);
7525
+ const palettePixelsPerFrame = Math.max(1, Math.floor(MAX_PALETTE_SAMPLE_PIXELS / paletteIndexes.length));
7526
+ for (const index of paletteIndexes) {
7527
+ const sampled = frames[index];
7528
+ const rawElement = animation.render(sampled.state, sampled.frame);
7529
+ const watermark = normalized.watermark ?? normalized.brand;
7530
+ const element = watermark ? wrapWithWatermark(rawElement, normalized.width, normalized.height, normalized.theme, watermark) : rawElement;
7531
+ const rendered = await renderToPixels(
7532
+ element,
7533
+ normalized.width,
7534
+ normalized.height,
7535
+ normalized.scale,
7536
+ normalized.theme,
7537
+ normalized.background
7538
+ );
7539
+ paletteFrames.push({
7540
+ ...rendered,
7541
+ pixels: samplePixels(rendered.pixels, palettePixelsPerFrame),
7542
+ delay: sampled.frame.delay
7543
+ });
7544
+ }
7545
+ const palette = sharedPalette(paletteFrames);
7464
7546
  const gif = GIFEncoder();
7465
7547
  let pending;
7466
7548
  let dimensions;
7549
+ let first = true;
7467
7550
  for (const sampled of frames) {
7468
7551
  const rawElement = animation.render(sampled.state, sampled.frame);
7469
7552
  const watermark = normalized.watermark ?? normalized.brand;
@@ -7473,6 +7556,7 @@ async function renderAnimationGifWithOutput(animation, options) {
7473
7556
  normalized.width,
7474
7557
  normalized.height,
7475
7558
  normalized.scale,
7559
+ normalized.theme,
7476
7560
  normalized.background
7477
7561
  );
7478
7562
  dimensions ??= { width: rendered.width, height: rendered.height };
@@ -7483,11 +7567,14 @@ async function renderAnimationGifWithOutput(animation, options) {
7483
7567
  pending.delay += sampled.frame.delay;
7484
7568
  continue;
7485
7569
  }
7486
- if (pending) writeGifFrame(gif, pending, normalized.loop);
7570
+ if (pending) {
7571
+ writeGifFrame(gif, pending, normalized.loop, palette, first);
7572
+ first = false;
7573
+ }
7487
7574
  pending = { ...rendered, delay: sampled.frame.delay };
7488
7575
  }
7489
7576
  if (!pending || !dimensions) throw new Error("Animated GIF requires at least one frame");
7490
- writeGifFrame(gif, pending, normalized.loop);
7577
+ writeGifFrame(gif, pending, normalized.loop, palette, first);
7491
7578
  gif.finish();
7492
7579
  await (0, import_promises2.mkdir)((0, import_path2.dirname)(options.outputPath), { recursive: true });
7493
7580
  await (0, import_promises2.writeFile)(options.outputPath, gif.bytes());