Skip to content

Commit ee65bb2

Browse files
committed
Implemented toDataURL (#1).
1 parent 491773e commit ee65bb2

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lib/canvas.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,18 @@ Canvas.prototype.toPng = function Canvas_toPng(keywords) {
118118
return png.getBuffer();
119119
};
120120

121+
/**
122+
* Renders the canvas as a data URI. Only type 'image/png' is supported.
123+
* @returns {string}
124+
* @public
125+
*/
126+
Canvas.prototype.toDataURL = function Canvas_toDataURL() {
127+
if (this.width <= 0 || this.height <= 0) {
128+
return "data:,";
129+
}
130+
131+
var png = this.toPng();
132+
return "data:image/png;base64," + png.toString("base64");
133+
};
134+
121135
module.exports = Canvas;

0 commit comments

Comments
 (0)