mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
Add option to only render current view
This commit is contained in:
parent
74cb3466ec
commit
b1c2f03ae9
19
src/core.js
19
src/core.js
@ -11,9 +11,10 @@ window.html2canvas = function(nodeList, options) {
|
|||||||
var node = clonedWindow.document.documentElement;
|
var node = clonedWindow.document.documentElement;
|
||||||
var support = new Support();
|
var support = new Support();
|
||||||
var imageLoader = new ImageLoader(options, support);
|
var imageLoader = new ImageLoader(options, support);
|
||||||
|
var bounds = NodeParser.prototype.getBounds(node);
|
||||||
|
var width = options.type === "view" ? bounds.width : documentWidth();
|
||||||
var renderer = new CanvasRenderer(documentWidth(), documentHeight(), imageLoader);
|
var height = options.type === "view" ? bounds.height : documentHeight();
|
||||||
|
var renderer = new CanvasRenderer(width, height, imageLoader);
|
||||||
var parser = new NodeParser(node, renderer, support, imageLoader, options);
|
var parser = new NodeParser(node, renderer, support, imageLoader, options);
|
||||||
|
|
||||||
window.console.log(parser);
|
window.console.log(parser);
|
||||||
@ -75,11 +76,12 @@ function createWindowClone(ownerDocument, width, height) {
|
|||||||
var style = documentClone.createElement("style");
|
var style = documentClone.createElement("style");
|
||||||
style.innerHTML = "body div.html2canvas-ready-test { background-image:url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7); }";
|
style.innerHTML = "body div.html2canvas-ready-test { background-image:url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7); }";
|
||||||
documentClone.body.appendChild(style);
|
documentClone.body.appendChild(style);
|
||||||
loadedTimer();
|
window.setTimeout(loadedTimer, 10);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function NodeParser(element, renderer, support, imageLoader, options) {
|
function NodeParser(element, renderer, support, imageLoader, options) {
|
||||||
|
log("Starting NodeParser");
|
||||||
this.renderer = renderer;
|
this.renderer = renderer;
|
||||||
this.options = options;
|
this.options = options;
|
||||||
this.range = null;
|
this.range = null;
|
||||||
@ -90,12 +92,16 @@ function NodeParser(element, renderer, support, imageLoader, options) {
|
|||||||
this.nodes = [parent].concat(this.getChildren(parent)).filter(function(container) {
|
this.nodes = [parent].concat(this.getChildren(parent)).filter(function(container) {
|
||||||
return container.visible = container.isElementVisible();
|
return container.visible = container.isElementVisible();
|
||||||
});
|
});
|
||||||
|
log("Fetched nodes");
|
||||||
this.images = imageLoader.fetch(this.nodes.filter(isElement));
|
this.images = imageLoader.fetch(this.nodes.filter(isElement));
|
||||||
|
log("Creating stacking contexts");
|
||||||
this.createStackingContexts();
|
this.createStackingContexts();
|
||||||
|
log("Sorting stacking contexts");
|
||||||
this.sortStackingContexts(this.stack);
|
this.sortStackingContexts(this.stack);
|
||||||
this.images.ready.then(bind(function() {
|
this.images.ready.then(bind(function() {
|
||||||
log("Images loaded, starting parsing");
|
log("Images loaded, starting parsing");
|
||||||
this.parse(this.stack);
|
this.parse(this.stack);
|
||||||
|
log("Finished rendering");
|
||||||
options.onrendered(renderer.canvas);
|
options.onrendered(renderer.canvas);
|
||||||
}, this));
|
}, this));
|
||||||
}
|
}
|
||||||
@ -147,12 +153,13 @@ NodeParser.prototype.parseBounds = function(nodeContainer) {
|
|||||||
NodeParser.prototype.getBounds = function(node) {
|
NodeParser.prototype.getBounds = function(node) {
|
||||||
if (node.getBoundingClientRect) {
|
if (node.getBoundingClientRect) {
|
||||||
var clientRect = node.getBoundingClientRect();
|
var clientRect = node.getBoundingClientRect();
|
||||||
|
var isBody = node.nodeName === "BODY";
|
||||||
return {
|
return {
|
||||||
top: clientRect.top,
|
top: clientRect.top,
|
||||||
bottom: clientRect.bottom || (clientRect.top + clientRect.height),
|
bottom: clientRect.bottom || (clientRect.top + clientRect.height),
|
||||||
left: clientRect.left,
|
left: clientRect.left,
|
||||||
width: node.offsetWidth,
|
width: isBody ? node.scrollWidth : node.offsetWidth,
|
||||||
height: node.offsetHeight
|
height: isBody ? node.scrollHeight : node.offsetHeight
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
|
@ -5,6 +5,7 @@ function CanvasRenderer(width, height) {
|
|||||||
this.canvas.height = height;
|
this.canvas.height = height;
|
||||||
this.ctx = this.canvas.getContext("2d");
|
this.ctx = this.canvas.getContext("2d");
|
||||||
this.ctx.textBaseline = "bottom";
|
this.ctx.textBaseline = "bottom";
|
||||||
|
log("Initialized CanvasRenderer");
|
||||||
}
|
}
|
||||||
|
|
||||||
CanvasRenderer.prototype = Object.create(Renderer.prototype);
|
CanvasRenderer.prototype = Object.create(Renderer.prototype);
|
||||||
|
Loading…
Reference in New Issue
Block a user