Browse Source

Merge pull request #879 from LinusU/async-bug

Backport #874 to v1.x
v1.x
Linus Unnebäck 8 years ago
committed by GitHub
parent
commit
26fefc401c
  1. 16
      lib/canvas.js
  2. 8
      test/canvas.test.js

16
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){

8
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);

Loading…
Cancel
Save