Canvas Helper

v1.0.0

Convert browser canvas content to a Blob with a requested format and quality.

Convert a canvas to a Blob

CanvasHelper.toBlob() wraps HTMLCanvasElement.toBlob() in a promise. The requested MIME type and optional quality are passed directly to the browser.

import { CanvasHelper } from '@oardi/ts-utils';

const webp = await CanvasHelper.toBlob(canvas, 'image/webp', 0.6);

Quality normally ranges from 0 to 1. The browser applies it to supported lossy formats and ignores it or uses its default when the value or format is unsupported. If the canvas API returns null, the promise rejects with an Error.

Browser requirements

toBlob() requires the browser canvas API and must not be called during server-side rendering.

Previous:Helpers

← Browser Helper