diff --git a/lib/canvas.js b/lib/canvas.js index d008f5aa0..f1d9232e2 100644 --- a/lib/canvas.js +++ b/lib/canvas.js @@ -247,11 +247,6 @@ Canvas.prototype.toDataURL = function(a1, a2, a3){ // ['image/jpeg', qual, fn] -> ['image/jpeg', {quality: qual}, fn] // ['image/jpeg', undefined, fn] -> ['image/jpeg', null, fn] - if (this.width === 0 || this.height === 0) { - // Per spec, if the bitmap has no pixels, return this string: - return "data:,"; - } - var type = 'image/png'; var opts = {}; var fn; @@ -280,6 +275,17 @@ Canvas.prototype.toDataURL = function(a1, a2, a3){ } } + if (this.width === 0 || this.height === 0) { + // Per spec, if the bitmap has no pixels, return this string: + var str = "data:,"; + if (fn) { + setTimeout(function() { + fn(null, str); + }); + } + return str; + } + if ('image/png' === type) { if (fn) { this.toBuffer(function(err, buf){ diff --git a/test/canvas.test.js b/test/canvas.test.js index 1dbd2040c..f29945900 100644 --- a/test/canvas.test.js +++ b/test/canvas.test.js @@ -526,6 +526,14 @@ describe('Canvas', function () { }); }); + it('toDataURL(function (err, str) {...}) is async even with no canvas data', function (done) { + new Canvas().toDataURL(function(err, str){ + assert.ifError(err); + assert.ok('data:,' === str); + done(); + }); + }); + it('toDataURL(0.5, function (err, str) {...}) works and defaults to PNG', function (done) { new Canvas(200,200).toDataURL(0.5, function(err, str){ assert.ifError(err);