From b60fc931b51d0842b8ef5c08940f27cdb09fa0dd Mon Sep 17 00:00:00 2001 From: Niklas von Hertzen Date: Sat, 3 Mar 2012 16:39:52 +0200 Subject: [PATCH] moved backgroundPosition to utils --- src/Core.js | 63 +++++++++++++++++++++++++++++++++++++++++++++++++--- src/Parse.js | 58 ++--------------------------------------------- 2 files changed, 62 insertions(+), 59 deletions(-) diff --git a/src/Core.js b/src/Core.js index 534e113..b503349 100644 --- a/src/Core.js +++ b/src/Core.js @@ -125,6 +125,63 @@ _html2canvas.Util.getCSS = function (el, attribute) { }; + +_html2canvas.Util.BackgroundPosition = function ( el, bounds, image ) { + // TODO add support for multi image backgrounds + + var bgposition = (function( bgp ){ + + if (bgp !== undefined) { + return (bgp.split(",")[0] || "0 0").split(" "); + } else { + // Older IE uses -x and -y + return [ _html2canvas.Util.getCSS( el, "backgroundPositionX" ), _html2canvas.Util.getCSS( el, "backgroundPositionY" ) ]; + } + + + })( _html2canvas.Util.getCSS( el, "backgroundPosition" ) ), + topPos, + left, + percentage, + val; + + if (bgposition.length === 1){ + val = bgposition; + + bgposition = []; + + bgposition[0] = val; + bgposition[1] = val; + } + + + + if (bgposition[0].toString().indexOf("%") !== -1){ + percentage = (parseFloat(bgposition[0])/100); + left = ((bounds.width * percentage)-(image.width*percentage)); + + }else{ + left = parseInt(bgposition[0],10); + } + + if (bgposition[1].toString().indexOf("%") !== -1){ + + percentage = (parseFloat(bgposition[1])/100); + topPos = ((bounds.height * percentage)-(image.height*percentage)); + }else{ + topPos = parseInt(bgposition[1],10); + } + + + + + return { + top: topPos, + left: left + }; + +}; + _html2canvas.Util.Extend = function (options, defaults) { var key; for (key in options) { @@ -139,10 +196,10 @@ _html2canvas.Util.Children = function(el) { // $(el).contents() !== el.childNodes, Opera / IE have issues with that var children; try { - children = $(el).contents(); + children = $(el).contents(); } catch (ex) { - h2clog("html2canvas.Util.Children failed with exception: " + ex.message); - children = []; + h2clog("html2canvas.Util.Children failed with exception: " + ex.message); + children = []; } return children; }; diff --git a/src/Parse.js b/src/Parse.js index da0e4b5..ce5d9b4 100644 --- a/src/Parse.js +++ b/src/Parse.js @@ -699,61 +699,7 @@ _html2canvas.Parse = function ( images, options ) { - function getBackgroundPosition(el, bounds, image){ - // TODO add support for multi image backgrounds - - var bgposition = (function( bgp ){ - - if (bgp !== undefined) { - return (bgp.split(",")[0] || "0 0").split(" "); - } else { - // Older IE uses -x and -y - return [ getCSS(el, "backgroundPositionX"), getCSS(el, "backgroundPositionY") ]; - } - - - })( getCSS(el, "backgroundPosition") ), - topPos, - left, - percentage, - val; - - if (bgposition.length === 1){ - val = bgposition; - - bgposition = []; - - bgposition[0] = val; - bgposition[1] = val; - } - - - - if (bgposition[0].toString().indexOf("%") !== -1){ - percentage = (parseFloat(bgposition[0])/100); - left = ((bounds.width * percentage)-(image.width*percentage)); - - }else{ - left = parseInt(bgposition[0],10); - } - - if (bgposition[1].toString().indexOf("%") !== -1){ - - percentage = (parseFloat(bgposition[1])/100); - topPos = ((bounds.height * percentage)-(image.height*percentage)); - }else{ - topPos = parseInt(bgposition[1],10); - } - - - - - return { - top: topPos, - left: left - }; - - } + function renderImage (ctx, image, sx, sy, sw, sh, dx, dy, dw, dh) { ctx.drawImage( @@ -875,7 +821,7 @@ _html2canvas.Parse = function ( images, options ) { image = loadImage( background_image ); - bgp = getBackgroundPosition(el, bounds, image); + bgp = _html2canvas.Util.BackgroundPosition(el, bounds, image); if ( image ){