Skip to content

Commit

Permalink
Merge branch 'master' into fix-classlist-access
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur authored Jan 26, 2025
2 parents ec96d72 + d1b07cc commit 7691561
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## [next]

- fix(): Allow for node-canvas images to work with the FabricImage class by making classList optional. [#10412](https://github.com/fabricjs/fabric.js/pull/10412)
- fix(): Allow for brush subclassing moving some properties from private to protected. [#10416](https://github.com/fabricjs/fabric.js/pull/10416)
- feat(): Add method toBlob. [#3283](https://github.com/fabricjs/fabric.js/issues/3283)

## [6.5.4]

Expand Down
6 changes: 3 additions & 3 deletions src/brushes/PencilBrush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export class PencilBrush extends BaseBrush {
*/
straightLineKey: ModifierKey | undefined | null = 'shiftKey';

private declare _points: Point[];
private declare _hasStraightLine: boolean;
private declare oldEnd?: Point;
protected declare _points: Point[];
protected declare _hasStraightLine: boolean;
protected declare oldEnd?: Point;

constructor(canvas: Canvas) {
super(canvas);
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>;

0 comments on commit 7691561

Please sign in to comment.