From c7e06e5633dc92f2a55af53ef29d0dbcdefd57c0 Mon Sep 17 00:00:00 2001 From: MoyuScript Date: Thu, 3 Jan 2013 17:09:23 -0600 Subject: [PATCH] simply resize images to background-size --- src/Parse.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Parse.js b/src/Parse.js index 9b863fb..13cef56 100644 --- a/src/Parse.js +++ b/src/Parse.js @@ -779,26 +779,28 @@ _html2canvas.Parse = function (images, options) { backgroundPosition = _html2canvas.Util.BackgroundPosition(el, bounds, image, imageIndex, backgroundSize), backgroundRepeat = getCSS(el, "backgroundRepeat").split(","); + image = resizeImage(image, backgroundSize); + backgroundRepeat = backgroundRepeat[imageIndex] || backgroundRepeat[0]; switch (backgroundRepeat) { case "repeat-x": backgroundRepeatShape(ctx, image, backgroundPosition, bounds, - bounds.left, bounds.top + backgroundPosition.top, 99999, backgroundSize.height); + bounds.left, bounds.top + backgroundPosition.top, 99999, image.height); break; case "repeat-y": backgroundRepeatShape(ctx, image, backgroundPosition, bounds, - bounds.left + backgroundPosition.left, bounds.top, backgroundSize.width, 99999); + bounds.left + backgroundPosition.left, bounds.top, image.width, 99999); break; case "no-repeat": backgroundRepeatShape(ctx, image, backgroundPosition, bounds, - bounds.left + backgroundPosition.left, bounds.top + backgroundPosition.top, backgroundSize.width, backgroundSize.height); + bounds.left + backgroundPosition.left, bounds.top + backgroundPosition.top, image.width, image.height); break; default: - renderBackgroundRepeat(ctx, image, backgroundPosition, { top: bounds.top, left: bounds.left, width: backgroundSize.width, height: backgroundSize.height }); + renderBackgroundRepeat(ctx, image, backgroundPosition, { top: bounds.top, left: bounds.left, width: image.width, height: image.height }); break; } } @@ -830,6 +832,18 @@ _html2canvas.Parse = function (images, options) { } } + function resizeImage(image, bounds) { + if(image.width === bounds.width && image.height === bounds.height) + return image; + + var ctx, canvas = doc.createElement('canvas'); + canvas.width = bounds.width; + canvas.height = bounds.height; + ctx = canvas.getContext("2d"); + drawImage(ctx, image, 0, 0, image.width, image.height, 0, 0, bounds.width, bounds.height ); + return canvas; + } + function setOpacity(ctx, element, parentStack) { var opacity = getCSS(element, "opacity") * ((parentStack) ? parentStack.opacity : 1); ctx.setVariable("globalAlpha", opacity);