Fix cropping bug

This commit is contained in:
Niklas von Hertzen 2014-03-05 19:19:24 +02:00
parent bb8c5a973b
commit 85b77ca49f
3 changed files with 17 additions and 13 deletions

View File

@ -56,13 +56,15 @@ function renderDocument(document, options, windowWidth, windowHeight) {
function crop(canvas, bounds) {
var croppedCanvas = document.createElement("canvas");
var left = Math.max(0, bounds.left);
var top = Math.max(0, bounds.top);
var width = croppedCanvas.width = Math.min(left + canvas.width, Math.max(Math.min(0, bounds.left) + bounds.width, 1));
var height = croppedCanvas.height = Math.min(top + canvas.height, Math.max(Math.min(0, bounds.top) + bounds.height, 1));
var x1 = Math.min(canvas.width - 1, Math.max(0, bounds.left));
var x2 = Math.min(canvas.width, Math.max(1, bounds.left + bounds.width));
var y1 = Math.min(canvas.height - 1, Math.max(0, bounds.top));
var y2 = Math.min(canvas.height, Math.max(1, bounds.top + bounds.height));
var width = croppedCanvas.width = x2 - x1;
var height = croppedCanvas.height = y2 - y1;
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, left, top, width, height, 0, 0, width, height);
log("Resulting crop with width", width, "and height", height, " with x", x1, "and y", y1);
croppedCanvas.getContext("2d").drawImage(canvas, x1, y1, width, height, 0, 0, width, height);
return croppedCanvas;
}

File diff suppressed because one or more lines are too long

View File

@ -47,13 +47,15 @@ function renderDocument(document, options, windowWidth, windowHeight) {
function crop(canvas, bounds) {
var croppedCanvas = document.createElement("canvas");
var left = Math.max(0, bounds.left);
var top = Math.max(0, bounds.top);
var width = croppedCanvas.width = Math.min(left + canvas.width, Math.max(Math.min(0, bounds.left) + bounds.width, 1));
var height = croppedCanvas.height = Math.min(top + canvas.height, Math.max(Math.min(0, bounds.top) + bounds.height, 1));
var x1 = Math.min(canvas.width - 1, Math.max(0, bounds.left));
var x2 = Math.min(canvas.width, Math.max(1, bounds.left + bounds.width));
var y1 = Math.min(canvas.height - 1, Math.max(0, bounds.top));
var y2 = Math.min(canvas.height, Math.max(1, bounds.top + bounds.height));
var width = croppedCanvas.width = x2 - x1;
var height = croppedCanvas.height = y2 - y1;
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, left, top, width, height, 0, 0, width, height);
log("Resulting crop with width", width, "and height", height, " with x", x1, "and y", y1);
croppedCanvas.getContext("2d").drawImage(canvas, x1, y1, width, height, 0, 0, width, height);
return croppedCanvas;
}