Skip to content

Commit

Permalink
feat: Add method toBlob
Browse files Browse the repository at this point in the history
  • Loading branch information
zhe-he committed Jan 13, 2025
1 parent 577a807 commit 09a71f5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [next]

- feat(): Add method toBlob. [#3283](https://github.com/fabricjs/fabric.js/issues/3283)

## [6.5.4]

- docs() perf(): Reorder caching conditions for most common scenario and docs fixes. [#10366](https://github.com/fabricjs/fabric.js/pull/10366)
Expand Down
18 changes: 17 additions & 1 deletion src/canvas/StaticCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from '../util/animation/AnimationFrameProvider';
import { runningAnimations } from '../util/animation/AnimationRegistry';
import { uid } from '../util/internals/uid';
import { createCanvasElementFor, toDataURL } from '../util/misc/dom';
import { createCanvasElementFor, toBlob, toDataURL } from '../util/misc/dom';
import { invertTransform, transformPoint } from '../util/misc/matrix';
import type { EnlivenObjectOptions } from '../util/misc/objectEnlive';
import {
Expand Down Expand Up @@ -1393,6 +1393,22 @@ export class StaticCanvas<
quality,
);
}
toBlob(options = {} as TDataUrlOptions): Promise<Blob | null> {
const {
format = 'png',
quality = 1,
multiplier = 1,
enableRetinaScaling = false,
} = options;
const finalMultiplier =
multiplier * (enableRetinaScaling ? this.getRetinaScaling() : 1);

return toBlob(
this.toCanvasElement(finalMultiplier, options),
format,
quality,
);
}

/**
* Create a new HTMLCanvas element painted with the current canvas content.
Expand Down
8 changes: 8 additions & 0 deletions src/shapes/Object/Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
createCanvasElement,
createCanvasElementFor,
toDataURL,
toBlob,
} from '../../util/misc/dom';
import { invertTransform, qrDecompose } from '../../util/misc/matrix';
import { enlivenObjectEnlivables } from '../../util/misc/objectEnlive';
Expand Down Expand Up @@ -1403,6 +1404,13 @@ export class FabricObject<
options.quality || 1,
);
}
toBlob(options: toDataURLOptions = {}) {
return toBlob(
this.toCanvasElement(options),
options.format || 'png',
options.quality || 1,
);
}

/**
* Returns true if any of the specified types is identical to the type of an instance
Expand Down
1 change: 1 addition & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export {
createImage,
copyCanvasElement,
toDataURL,
toBlob,
} from './misc/dom';
export { toFixed } from './misc/toFixed';
export {
Expand Down
9 changes: 9 additions & 0 deletions src/util/misc/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,12 @@ export const isHTMLCanvas = (
): canvas is HTMLCanvasElement => {
return !!canvas && (canvas as HTMLCanvasElement).getContext !== undefined;
};

export const toBlob = (
canvasEl: HTMLCanvasElement,
format: ImageFormat,
quality: number,
) =>
new Promise((resolve, _) => {
canvasEl.toBlob(resolve, `image/${format}`, quality);
}) as Promise<Blob | null>;

0 comments on commit 09a71f5

Please sign in to comment.