Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add method toBlob #10399

Merged
merged 4 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 12 additions & 0 deletions src/canvas/StaticCanvas.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { StaticCanvas } from './StaticCanvas';

describe('StaticCanvas', () => {
it('toBlob', async () => {
const canvas = new StaticCanvas(undefined, { width: 300, height: 300 });
const blob = await canvas.toBlob({
multiplier: 3,
});
expect(blob).toBeInstanceOf(Blob);
expect(blob?.type).toBe('image/png');
});
});
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
4 changes: 2 additions & 2 deletions src/color/color.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ describe('test Color.fromHsla for color', () => {
stringToParse: 'hsl( -450, 50%, 50%, .5)',
expectedSource: [127, 64, 191, 0.5],
},
{
{
name: 'fromHsla with Saturation 0',
stringToParse: 'hsla(0, 0%, 50%, 1)',
expectedSource: [128, 128, 128, 1],
Expand Down Expand Up @@ -324,4 +324,4 @@ describe('parsing colors for color', () => {
expect(colorUppercase).toBeInstanceOf(Color);
expect(colorUppercase.getSource()).toEqual(expectedSource);
});
});
});
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
22 changes: 22 additions & 0 deletions src/util/misc/dom.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { toBlob, createCanvasElement } from './dom';

describe('DOM utils', () => {
it('toBlob without format', async () => {
const canvas = createCanvasElement();
const blob = await toBlob(canvas);
expect(blob).toBeInstanceOf(Blob);
expect(blob?.type).toEqual('image/png');
});
it('toBlob png', async () => {
const canvas = createCanvasElement();
const blob = await toBlob(canvas, 'png');
expect(blob).toBeInstanceOf(Blob);
expect(blob?.type).toEqual('image/png');
});
it('toBlob jpeg', async () => {
const canvas = createCanvasElement();
const blob = await toBlob(canvas, 'jpeg');
expect(blob).toBeInstanceOf(Blob);
expect(blob?.type).toEqual('image/jpeg');
});
});
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>;
Loading