Allow multiple renders to occur simultaneously

This commit is contained in:
Niklas von Hertzen 2014-12-24 17:34:31 +02:00
parent 60368d4670
commit 2f5f9f6e59
4 changed files with 107 additions and 12 deletions

13
dist/html2canvas.js vendored
View File

@ -569,8 +569,10 @@ if (typeof(Object.create) !== "function" || typeof(document.createElement("canva
var html2canvasNodeAttribute = "data-html2canvas-node";
var html2canvasCanvasCloneAttribute = "data-html2canvas-canvas-clone";
var html2canvasCanvasCloneIndex = 0;
var html2canvasCloneIndex = 0;
window.html2canvas = function(nodeList, options) {
var index = html2canvasCloneIndex++;
options = options || {};
if (options.logging) {
window.html2canvas.logging = true;
@ -593,8 +595,8 @@ window.html2canvas = function(nodeList, options) {
}
var node = ((nodeList === undefined) ? [document.documentElement] : ((nodeList.length) ? nodeList : [nodeList]))[0];
node.setAttribute(html2canvasNodeAttribute, "true");
return renderDocument(node.ownerDocument, options, node.ownerDocument.defaultView.innerWidth, node.ownerDocument.defaultView.innerHeight).then(function(canvas) {
node.setAttribute(html2canvasNodeAttribute + index, index);
return renderDocument(node.ownerDocument, options, node.ownerDocument.defaultView.innerWidth, node.ownerDocument.defaultView.innerHeight, index).then(function(canvas) {
if (typeof(options.onrendered) === "function") {
log("options.onrendered is deprecated, html2canvas returns a Promise containing the canvas");
options.onrendered(canvas);
@ -606,11 +608,12 @@ window.html2canvas = function(nodeList, options) {
window.html2canvas.punycode = this.punycode;
window.html2canvas.proxy = {};
function renderDocument(document, options, windowWidth, windowHeight) {
function renderDocument(document, options, windowWidth, windowHeight, html2canvasIndex) {
return createWindowClone(document, document, windowWidth, windowHeight, options).then(function(container) {
log("Document cloned");
var selector = "[" + html2canvasNodeAttribute + "='true']";
document.querySelector(selector).removeAttribute(html2canvasNodeAttribute);
var attributeName = html2canvasNodeAttribute + html2canvasIndex;
var selector = "[" + attributeName + "='" + html2canvasIndex + "']";
document.querySelector(selector).removeAttribute(attributeName);
var clonedWindow = container.contentWindow;
var node = clonedWindow.document.querySelector(selector);
var oncloneHandler = (typeof(options.onclone) === "function") ? Promise.resolve(options.onclone(clonedWindow.document)) : Promise.resolve(true);

File diff suppressed because one or more lines are too long

View File

@ -1,8 +1,10 @@
var html2canvasNodeAttribute = "data-html2canvas-node";
var html2canvasCanvasCloneAttribute = "data-html2canvas-canvas-clone";
var html2canvasCanvasCloneIndex = 0;
var html2canvasCloneIndex = 0;
window.html2canvas = function(nodeList, options) {
var index = html2canvasCloneIndex++;
options = options || {};
if (options.logging) {
window.html2canvas.logging = true;
@ -25,8 +27,8 @@ window.html2canvas = function(nodeList, options) {
}
var node = ((nodeList === undefined) ? [document.documentElement] : ((nodeList.length) ? nodeList : [nodeList]))[0];
node.setAttribute(html2canvasNodeAttribute, "true");
return renderDocument(node.ownerDocument, options, node.ownerDocument.defaultView.innerWidth, node.ownerDocument.defaultView.innerHeight).then(function(canvas) {
node.setAttribute(html2canvasNodeAttribute + index, index);
return renderDocument(node.ownerDocument, options, node.ownerDocument.defaultView.innerWidth, node.ownerDocument.defaultView.innerHeight, index).then(function(canvas) {
if (typeof(options.onrendered) === "function") {
log("options.onrendered is deprecated, html2canvas returns a Promise containing the canvas");
options.onrendered(canvas);
@ -38,11 +40,12 @@ window.html2canvas = function(nodeList, options) {
window.html2canvas.punycode = this.punycode;
window.html2canvas.proxy = {};
function renderDocument(document, options, windowWidth, windowHeight) {
function renderDocument(document, options, windowWidth, windowHeight, html2canvasIndex) {
return createWindowClone(document, document, windowWidth, windowHeight, options).then(function(container) {
log("Document cloned");
var selector = "[" + html2canvasNodeAttribute + "='true']";
document.querySelector(selector).removeAttribute(html2canvasNodeAttribute);
var attributeName = html2canvasNodeAttribute + html2canvasIndex;
var selector = "[" + attributeName + "='" + html2canvasIndex + "']";
document.querySelector(selector).removeAttribute(attributeName);
var clonedWindow = container.contentWindow;
var node = clonedWindow.document.querySelector(selector);
var oncloneHandler = (typeof(options.onclone) === "function") ? Promise.resolve(options.onclone(clonedWindow.document)) : Promise.resolve(true);

View File

@ -0,0 +1,89 @@
<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<link rel="stylesheet" href="lib/mocha.css" />
<script src="../../dist/html2canvas.js"></script>
<script src="../assets/jquery-1.6.2.js"></script>
<script src="lib/expect.js"></script>
<script src="lib/mocha.js"></script>
<style>
#block {
width: 200px;
height: 200px;
}
#green-block {
width: 200px;
height: 200px;
background: green;
}
</style>
</head>
<body>
<div id="mocha"></div>
<div id="block"></div>
<div id="green-block"></div>
<script>mocha.setup('bdd')</script>
<script>
describe("Multiple renders", function() {
it("render correctly", function(done) {
var d = 0;
for (var i = 0; i < 10; i++) {
html2canvas(document.querySelector('#green-block')).then(function (canvas) {
expect(canvas.width).to.equal(200);
expect(canvas.height).to.equal(200);
validCanvasPixels(canvas);
canvas.width = canvas.height = 10;
d++;
if (d === 10) {
done();
}
});
}
});
it("render correctly when non sequential", function(done) {
var d = 0;
for (var i = 0; i < 10; i++) {
html2canvas(document.querySelector('#block'), {onclone: function(document) {
return new Promise(function(resolve) {
document.querySelector('#block').style.backgroundColor = 'green';
setTimeout(function() {
resolve();
}, 100);
});
}}).then(function (canvas) {
expect(canvas.width).to.equal(200);
expect(canvas.height).to.equal(200);
validCanvasPixels(canvas);
canvas.width = canvas.height = 10;
d++;
if (d === 10) {
done();
}
});
}
});
});
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) {
expect().fail("Invalid canvas data");
}
}
}
mocha.checkLeaks();
mocha.globals(['jQuery']);
if (window.mochaPhantomJS) {
mochaPhantomJS.run();
}
else {
mocha.run();
}
</script>
</body>
</html>