Account for negative bounds in cropping

This commit is contained in:
Niklas von Hertzen 2014-03-04 20:42:34 +02:00
parent d2c3378c3e
commit cfe4137bcc
3 changed files with 12 additions and 8 deletions

View File

@ -56,9 +56,11 @@ function renderDocument(document, options, windowWidth, windowHeight) {
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);
var width = croppedCanvas.width = Math.max(Math.min(0, bounds.left) + bounds.width, 1);
var height = croppedCanvas.height = Math.max(Math.min(0, bounds.top) + bounds.height, 1);
log("Cropping canvas at:", "left:", bounds.left, "top:", bounds.top, "width:", bounds.width, "height:", bounds.height);
log("Resulting crop with width", width, "and height", height);
croppedCanvas.getContext("2d").drawImage(canvas, Math.max(0, bounds.left), Math.max(0, bounds.top), width, height, 0, 0, width, height);
return croppedCanvas;
}

File diff suppressed because one or more lines are too long

View File

@ -47,9 +47,11 @@ function renderDocument(document, options, windowWidth, windowHeight) {
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);
var width = croppedCanvas.width = Math.max(Math.min(0, bounds.left) + bounds.width, 1);
var height = croppedCanvas.height = Math.max(Math.min(0, bounds.top) + bounds.height, 1);
log("Cropping canvas at:", "left:", bounds.left, "top:", bounds.top, "width:", bounds.width, "height:", bounds.height);
log("Resulting crop with width", width, "and height", height);
croppedCanvas.getContext("2d").drawImage(canvas, Math.max(0, bounds.left), Math.max(0, bounds.top), width, height, 0, 0, width, height);
return croppedCanvas;
}