mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
Limit crop to the maximum size of outputted canvas
This commit is contained in:
parent
47a35b241a
commit
de21379f36
@ -56,11 +56,13 @@ function renderDocument(document, options, windowWidth, windowHeight) {
|
||||
|
||||
function crop(canvas, bounds) {
|
||||
var croppedCanvas = document.createElement("canvas");
|
||||
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);
|
||||
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));
|
||||
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);
|
||||
croppedCanvas.getContext("2d").drawImage(canvas, left, top, width, height, 0, 0, width, height);
|
||||
return croppedCanvas;
|
||||
}
|
||||
|
||||
|
2
build/html2canvas.min.js
vendored
2
build/html2canvas.min.js
vendored
File diff suppressed because one or more lines are too long
@ -47,11 +47,13 @@ function renderDocument(document, options, windowWidth, windowHeight) {
|
||||
|
||||
function crop(canvas, bounds) {
|
||||
var croppedCanvas = document.createElement("canvas");
|
||||
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);
|
||||
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));
|
||||
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);
|
||||
croppedCanvas.getContext("2d").drawImage(canvas, left, top, width, height, 0, 0, width, height);
|
||||
return croppedCanvas;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user