Correctly crop content

This commit is contained in:
Niklas von Hertzen 2014-03-03 21:19:28 +02:00
parent 95f4bcea0a
commit d2c3378c3e
3 changed files with 20 additions and 23 deletions

View File

@ -49,11 +49,19 @@ function renderDocument(document, options, windowWidth, windowHeight) {
if (options.removeContainer) { if (options.removeContainer) {
container.parentNode.removeChild(container); container.parentNode.removeChild(container);
} }
return renderer.canvas; return (options.type !== "view" && (node === clonedWindow.document.body || node === clonedWindow.document.documentElement)) ? renderer.canvas : crop(renderer.canvas, bounds);
}); });
}); });
} }
function crop(canvas, bounds) {
var croppedCanvas = document.createElement("canvas");
croppedCanvas.width = bounds.width;
croppedCanvas.height = bounds.height;
croppedCanvas.getContext("2d").drawImage(canvas, bounds.left, bounds.top, bounds.width, bounds.height, 0, 0, bounds.width, bounds.height);
return croppedCanvas;
}
function documentWidth () { function documentWidth () {
return Math.max( return Math.max(
Math.max(document.body.scrollWidth, document.documentElement.scrollWidth), Math.max(document.body.scrollWidth, document.documentElement.scrollWidth),
@ -1689,7 +1697,6 @@ StackingContext.prototype.getParentStack = function(context) {
function Support(document) { function Support(document) {
this.rangeBounds = this.testRangeBounds(document); this.rangeBounds = this.testRangeBounds(document);
this.cors = this.testCORS(); this.cors = this.testCORS();
this.nativeRendering = this.testNativeRendering(document);
} }
Support.prototype.testRangeBounds = function(document) { Support.prototype.testRangeBounds = function(document) {
@ -1721,24 +1728,6 @@ Support.prototype.testCORS = function() {
return typeof((new Image()).crossOrigin) !== "undefined"; return typeof((new Image()).crossOrigin) !== "undefined";
}; };
Support.prototype.testNativeRendering = function(document) {
var NS = "http://www.w3.org/2000/svg";
var svg = document.createElementNS(NS, "svg");
var canvas = document.createElement("canvas");
svg.setAttributeNS(NS, "width", "100");
svg.setAttributeNS(NS, "height", "100");
var div = document.createElement("div");
var foreignObject = document.createElementNS(NS, "foreignObject");
foreignObject.setAttributeNS(NS, "width", "100%");
foreignObject.setAttributeNS(NS, "height", "100%");
foreignObject.appendChild(document.documentElement.cloneNode(true));
svg.appendChild(foreignObject);
div.appendChild(svg);
var ctx = canvas.getContext("2d");
document.body.appendChild(canvas);
};
function TextContainer(node, parent) { function TextContainer(node, parent) {
NodeContainer.call(this, node, parent); NodeContainer.call(this, node, parent);
} }

File diff suppressed because one or more lines are too long

View File

@ -40,11 +40,19 @@ function renderDocument(document, options, windowWidth, windowHeight) {
if (options.removeContainer) { if (options.removeContainer) {
container.parentNode.removeChild(container); container.parentNode.removeChild(container);
} }
return renderer.canvas; return (options.type !== "view" && (node === clonedWindow.document.body || node === clonedWindow.document.documentElement)) ? renderer.canvas : crop(renderer.canvas, bounds);
}); });
}); });
} }
function crop(canvas, bounds) {
var croppedCanvas = document.createElement("canvas");
croppedCanvas.width = bounds.width;
croppedCanvas.height = bounds.height;
croppedCanvas.getContext("2d").drawImage(canvas, bounds.left, bounds.top, bounds.width, bounds.height, 0, 0, bounds.width, bounds.height);
return croppedCanvas;
}
function documentWidth () { function documentWidth () {
return Math.max( return Math.max(
Math.max(document.body.scrollWidth, document.documentElement.scrollWidth), Math.max(document.body.scrollWidth, document.documentElement.scrollWidth),