From f3c2661cb9df00fda7427ff0c59a0b4e761dbb57 Mon Sep 17 00:00:00 2001 From: Jacz Date: Thu, 24 May 2018 01:44:39 +1000 Subject: [PATCH] best attempt at toDataURL --- src/canvas.js | 23 +++++++++++++++++++++++ typings/index.d.ts | 2 ++ 2 files changed, 25 insertions(+) diff --git a/src/canvas.js b/src/canvas.js index ae329609..33ffc0cd 100644 --- a/src/canvas.js +++ b/src/canvas.js @@ -1211,6 +1211,29 @@ class CanvasConstructor { })); } + /** + * Render the canvas into a Data URL. + * @param {string} type the standard MIME type for the image format to return. If you do not specify this parameter, the default value is PNG. + * @param {any[]} args Extra arguments + * @returns {string} + * @see https://github.com/Automattic/node-canvas#canvastodataurl-sync-and-async + */ + toDataURL(type, ...args) { + return this.canvas.toDataURL(type, ...args); + } + + /** + * Render the canvas into a Data URL using a Promise. + * @param {string} type the standard MIME type for the image format to return. If you do not specify this parameter, the default value is PNG. + * @returns {Promise} + */ + toDataURLAsync(type) { + return new Promise((resolve, reject) => this.canvas.toDataURL(type, (err, url) => { + if (err) reject(err); + else resolve(url); + })); + } + static getCanvas() { return Canvas; } diff --git a/typings/index.d.ts b/typings/index.d.ts index e822b1d8..86f9d32b 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -101,6 +101,8 @@ declare module 'canvas-constructor' { public toBuffer(options?: Object): Buffer; public toBufferAsync(): Promise; + public toDataURL(type: string, ...args: any[]): string; + public toDataURLAsync(type: string): Promise; public static getCanvas(): NodeCanvas; public static registerFont(path: string, family: string | fontFaceType);