From 7279d2b20766554f660b431ce595342adcefec8b Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Wed, 22 Feb 2017 23:41:23 +0700 Subject: [PATCH 1/4] Add test for dataURL with callback always returning image data async --- test/canvas.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/canvas.test.js b/test/canvas.test.js index 72bf6be..006e776 100644 --- a/test/canvas.test.js +++ b/test/canvas.test.js @@ -519,6 +519,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); From f6d8214017508ea6eadb1fcd7491caaf99c3366b Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Wed, 22 Feb 2017 23:45:14 +0700 Subject: [PATCH 2/4] Return the no pixel string async if a callback was passed in --- lib/canvas.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/canvas.js b/lib/canvas.js index 8272c9b..6a7de5c 100644 --- a/lib/canvas.js +++ b/lib/canvas.js @@ -231,11 +231,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; @@ -264,6 +259,11 @@ 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: + return fn ? fn(null, "data:,") : "data:,"; + } + if ('image/png' === type) { if (fn) { this.toBuffer(function(err, buf){ From 3bd0dffea82ecb3bc79fc936cb4c08ec991c0e1a Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Thu, 23 Feb 2017 00:18:51 +0700 Subject: [PATCH 3/4] Make sure callback isn't executed synchronously --- lib/canvas.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/canvas.js b/lib/canvas.js index 6a7de5c..11aab6f 100644 --- a/lib/canvas.js +++ b/lib/canvas.js @@ -261,7 +261,13 @@ 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: - return fn ? fn(null, "data:,") : "data:,"; + if (fn) { + setTimeout(function() { + fn(null, "data:,"); + }); + return; + } + return "data:,"; } if ('image/png' === type) { From 818a26a43a8a5b1867e20d584b0a4423f9f6868b Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Thu, 23 Feb 2017 00:30:00 +0700 Subject: [PATCH 4/4] Return no canvas data string synchronously for backwards compatibility --- lib/canvas.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/canvas.js b/lib/canvas.js index 11aab6f..efb873d 100644 --- a/lib/canvas.js +++ b/lib/canvas.js @@ -261,13 +261,13 @@ 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, "data:,"); + fn(null, str); }); - return; } - return "data:,"; + return str; } if ('image/png' === type) {