Skip to content

Commit cfe4137

Browse files
committed
Account for negative bounds in cropping
1 parent d2c3378 commit cfe4137

3 files changed

Lines changed: 12 additions & 8 deletions

File tree

build/html2canvas.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ function renderDocument(document, options, windowWidth, windowHeight) {
5656

5757
function crop(canvas, bounds) {
5858
var croppedCanvas = document.createElement("canvas");
59-
croppedCanvas.width = bounds.width;
60-
croppedCanvas.height = bounds.height;
61-
croppedCanvas.getContext("2d").drawImage(canvas, bounds.left, bounds.top, bounds.width, bounds.height, 0, 0, bounds.width, bounds.height);
59+
var width = croppedCanvas.width = Math.max(Math.min(0, bounds.left) + bounds.width, 1);
60+
var height = croppedCanvas.height = Math.max(Math.min(0, bounds.top) + bounds.height, 1);
61+
log("Cropping canvas at:", "left:", bounds.left, "top:", bounds.top, "width:", bounds.width, "height:", bounds.height);
62+
log("Resulting crop with width", width, "and height", height);
63+
croppedCanvas.getContext("2d").drawImage(canvas, Math.max(0, bounds.left), Math.max(0, bounds.top), width, height, 0, 0, width, height);
6264
return croppedCanvas;
6365
}
6466

0 commit comments

Comments
 (0)