Fix background rendering regression #496

This commit is contained in:
Niklas von Hertzen 2015-01-10 20:53:06 +02:00
parent 9b372a4399
commit 7a58ad019f
5 changed files with 57 additions and 4 deletions

4
dist/html2canvas.js vendored
View File

@ -949,6 +949,8 @@ Color.prototype.namedColor = function(value) {
return !!color;
};
Color.prototype.isColor = true;
// JSON.stringify([].slice.call($$('.named-color-table tr'), 1).map(function(row) { return [row.childNodes[3].textContent, row.childNodes[5].textContent.trim().split(",").map(Number)] }).reduce(function(data, row) {data[row[0]] = row[1]; return data}, {}))
var colors = {
"aliceblue": [240, 248, 255],
@ -3182,7 +3184,7 @@ function CanvasRenderer(width, height) {
CanvasRenderer.prototype = Object.create(Renderer.prototype);
CanvasRenderer.prototype.setFillStyle = function(fillStyle) {
this.ctx.fillStyle = typeof(fillStyle) === "object" ? fillStyle.toString() : fillStyle;
this.ctx.fillStyle = typeof(fillStyle) === "object" && !!fillStyle.isColor ? fillStyle.toString() : fillStyle;
return this.ctx;
};

File diff suppressed because one or more lines are too long

View File

@ -114,6 +114,8 @@ Color.prototype.namedColor = function(value) {
return !!color;
};
Color.prototype.isColor = true;
// JSON.stringify([].slice.call($$('.named-color-table tr'), 1).map(function(row) { return [row.childNodes[3].textContent, row.childNodes[5].textContent.trim().split(",").map(Number)] }).reduce(function(data, row) {data[row[0]] = row[1]; return data}, {}))
var colors = {
"aliceblue": [240, 248, 255],

View File

@ -15,7 +15,7 @@ function CanvasRenderer(width, height) {
CanvasRenderer.prototype = Object.create(Renderer.prototype);
CanvasRenderer.prototype.setFillStyle = function(fillStyle) {
this.ctx.fillStyle = typeof(fillStyle) === "object" ? fillStyle.toString() : fillStyle;
this.ctx.fillStyle = typeof(fillStyle) === "object" && !!fillStyle.isColor ? fillStyle.toString() : fillStyle;
return this.ctx;
};

View File

@ -17,6 +17,17 @@
height: 200px;
background: green;
}
#background-block {
width: 200px;
height: 200px;
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNgaGD4DwAChAGAJVtEDQAAAABJRU5ErkJggg==) red;
}
#gradient-block {
width: 200px;
height: 200px;
background-image: -webkit-linear-gradient(top, #008000, #008000);
background-image: linear-gradient(to bottom, #008000, #008000);
}
</style>
</head>
<body>
@ -24,6 +35,8 @@
<script>mocha.setup('bdd')</script>
<div id="block"></div>
<div id="green-block"></div>
<div id="background-block"></div>
<div id="gradient-block"></div>
<script>
describe("options.background", function() {
it("with hexcolor", function(done) {
@ -60,11 +73,47 @@
});
});
describe('element background', function() {
it('with background-color', function(done) {
html2canvas(document.querySelector("#green-block")).then(function(canvas) {
expect(canvas.width).to.equal(200);
expect(canvas.height).to.equal(200);
validCanvasPixels(canvas);
done();
}).catch(function(error) {
done(error);
});
});
it('with background-image', function(done) {
html2canvas(document.querySelector("#background-block")).then(function(canvas) {
expect(canvas.width).to.equal(200);
expect(canvas.height).to.equal(200);
validCanvasPixels(canvas);
done();
}).catch(function(error) {
done(error);
});
});
it('with gradient background-image', function(done) {
html2canvas(document.querySelector("#gradient-block")).then(function(canvas) {
expect(canvas.width).to.equal(200);
expect(canvas.height).to.equal(200);
validCanvasPixels(canvas);
done();
}).catch(function(error) {
done(error);
});
});
});
function validCanvasPixels(canvas) {
var ctx = canvas.getContext("2d");
var data = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
for (var i = 0, len = data.length; i < len; i+=4) {
if (data[i] !== 0 || data[i+1] !== 128 || data[i+2] !== 0 || data[i+3] !== 255) {
console.log(i,data[i], data[i+1], data[i+2], data[i+3]);
expect().fail("Invalid canvas data");
}
}