moved backgroundPosition to utils

This commit is contained in:
Niklas von Hertzen 2012-03-03 16:39:52 +02:00
parent 8affbc3db5
commit b60fc931b5
2 changed files with 62 additions and 59 deletions

View File

@ -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) {

View File

@ -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 ){