DEV Community

abhaysingla05
abhaysingla05

Posted on

Fitting animated stickers under 512 KB in the browser with Rust and WebAssembly

Discord's custom asset limits are notoriously tight: 320×320 px under 512 KB (APNG or PNG) for stickers, and 128×128 px under 256 KB (PNG or GIF) for emojis.

Standard image compressors usually fail here for two reasons:

  1. They optimize for visual quality, not a hard byte ceiling. Running oxipng or gifsicle once might shrink a 1.8 MB animation to 720 KB—still rejected on upload.
  2. APNG and GIF have completely different compression bottlenecks. An APNG stores full 32-bit RGBA frames unless you quantize each frame's palette or use lossy alpha/color reduction; a GIF is already capped at 256 colors per frame, so bloat comes from frame count, inter-frame disposal, and dithering noise.

Solving it client-side with Rust + WebAssembly

Instead of uploading user assets to a backend queue, you can run a bounded search loop directly in the browser using Rust compiled to WebAssembly:

  1. Normalize dimensions & frame rate first. Cap stickers to 320×320 (and emojis to 128×128) while preserving aspect ratio, and drop redundant frames (e.g., 50 fps → 20–25 fps) when the source exceeds a reasonable frame budget.
  2. Palette quantization per target format. Using a median-cut or NeuQuant-style quantizer in Rust (like color_quant or imagequant), you can reduce RGBA frames to 256, 128, or 64 colors and run zlib/deflate compression on the resulting indexed frames.
  3. Binary search over quality knobs. Rather than guessing parameters, run a fast 4–6 step search over palette size, dithering strength, and frame-skipping until output.len() <= 512 * 1024 (or 256 * 1024 for emojis). Because everything runs in WASM memory, each iteration takes tens of milliseconds with zero network round-trips.

I built this pipeline into EmoteSize—specifically the Discord Sticker Maker—so creators can drop in a GIF, WebP, or video clip and get a guaranteed-under-limit APNG/GIF without their files ever leaving the browser.

If you're building similar media tooling in the browser, keeping the quantization + deflate loop in Rust/WASM makes hitting strict platform byte limits deterministic and instant.

Top comments (0)