Skip to content

Commit 4a6fa5f

Browse files
committed
feat: Add Canvas#{toBlob,toBlobAsync} for browser compatibility
1 parent 7e1c8f6 commit 4a6fa5f

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/canvas.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,7 @@ class Canvas {
13201320
}
13211321

13221322
/**
1323+
* <warn>This is for Node.js usage only, HTMLCanvasElement does not support this</warn>
13231324
* Render the canvas into a buffer.
13241325
* @param {any[]} args The render's options.
13251326
* @returns {Buffer}
@@ -1329,6 +1330,7 @@ class Canvas {
13291330
}
13301331

13311332
/**
1333+
* <warn>This is for Node.js usage only, HTMLCanvasElement does not support this</warn>
13321334
* Render the canvas into a buffer using a Promise.
13331335
* @returns {Promise<Buffer>}
13341336
*/
@@ -1362,6 +1364,30 @@ class Canvas {
13621364
}));
13631365
}
13641366

1367+
/**
1368+
* <warn>This is for web usage only, node-canvas does not support this</warn>
1369+
* Render the canvas into a Blob object representing the image contained in the canvas
1370+
* @param {Function} callback A callback function with the resulting `Blob` object as a single argument.
1371+
* @param {string} [mimeType] A string indicating the image format. The default type is `image/png`.
1372+
* @param {number} [qualityArgument] A number between 0 and 1 indicating image quality if the requested type is `image/jpeg` or `image/webp`.
1373+
* @returns {void}
1374+
* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob
1375+
*/
1376+
toBlob(...args) {
1377+
return this.canvas.toBlob(...args);
1378+
}
1379+
1380+
/**
1381+
* <warn>This is for web usage only, node-canvas does not support this</warn>
1382+
* Render the canvas into a Blob object representing the image contained in the canvas
1383+
* @param {string} [mimeType] A string indicating the image format. The default type is `image/png`.
1384+
* @param {number} [qualityArgument] A number between 0 and 1 indicating image quality if the requested type is `image/jpeg` or `image/webp`.
1385+
* @returns {Promise<Blob>}
1386+
*/
1387+
toBlobAsync(...args) {
1388+
return new Promise((resolve) => this.canvas.toBlob(resolve, ...args));
1389+
}
1390+
13651391
/**
13661392
* Resolves an Image or Buffer
13671393
* @param {(Image|Buffer)} imageOrBuffer An Image instance or a buffer

typings/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ declare module 'canvas-constructor' {
115115
public toBufferAsync(): Promise<Buffer>;
116116
public toDataURL(type: string, ...args: any[]): string;
117117
public toDataURLAsync(type: string): Promise<string>;
118+
public toBlob(callback: (blob: any) => void, mimeType?: string, qualityArgument?: number): void;
119+
public toBlobAsync(mimeType?: string, qualityArgument?: number): Promise<any>;
118120
public translate(dx: number, dy: number): this;
119121

120122
private _resolveImage(imageOrBuffer: BufferOrImage): Image;

0 commit comments

Comments
 (0)