refactored image rendering

This commit is contained in:
MoyuScript 2012-12-30 01:06:11 +02:00
parent bbdd29725a
commit 89e901d000

View File

@ -470,6 +470,27 @@ _html2canvas.Parse = function (images, options) {
return parentZ; return parentZ;
} }
function renderImage(ctx, element, image, bounds, borders) {
var paddingLeft = getCSSInt(element, 'paddingLeft'),
paddingTop = getCSSInt(element, 'paddingTop'),
paddingRight = getCSSInt(element, 'paddingRight'),
paddingBottom = getCSSInt(element, 'paddingBottom');
drawImage(
ctx,
image,
0, //sx
0, //sy
image.width, //sw
image.height, //sh
bounds.left + paddingLeft + borders[3].width, //dx
bounds.top + paddingTop + borders[0].width, // dy
bounds.width - (borders[1].width + borders[3].width + paddingLeft + paddingRight), //dw
bounds.height - (borders[0].width + borders[2].width + paddingTop + paddingBottom) //dh
);
}
function renderBorders(el, ctx, bounds, clip){ function renderBorders(el, ctx, bounds, clip){
var x = bounds.left, var x = bounds.left,
y = bounds.top, y = bounds.top,
@ -647,7 +668,7 @@ _html2canvas.Parse = function (images, options) {
body.removeChild(valueWrap); body.removeChild(valueWrap);
} }
function renderImage (ctx) { function drawImage (ctx) {
ctx.drawImage.apply(ctx, Array.prototype.slice.call(arguments, 1)); ctx.drawImage.apply(ctx, Array.prototype.slice.call(arguments, 1));
numDraws+=1; numDraws+=1;
} }
@ -656,7 +677,7 @@ _html2canvas.Parse = function (images, options) {
var sourceX = (elx - x > 0) ? elx-x :0, var sourceX = (elx - x > 0) ? elx-x :0,
sourceY= (ely - y > 0) ? ely-y : 0; sourceY= (ely - y > 0) ? ely-y : 0;
renderImage( drawImage(
ctx, ctx,
image, image,
Math.floor(sourceX), // source X Math.floor(sourceX), // source X
@ -764,7 +785,7 @@ _html2canvas.Parse = function (images, options) {
} }
if (bgh>0 && bgw > 0){ if (bgh>0 && bgw > 0){
renderImage( drawImage(
ctx, ctx,
image, image,
bgsx, // source X : 0 bgsx, // source X : 0
@ -897,28 +918,9 @@ _html2canvas.Parse = function (images, options) {
switch(el.nodeName){ switch(el.nodeName){
case "IMG": case "IMG":
imgSrc = el.getAttribute('src'); imgSrc = el.getAttribute('src');
image = loadImage(imgSrc); image = loadImage(el.getAttribute('src'));
if (image) { if (image) {
renderImage(ctx, el, image, bounds, borders);
paddingLeft = getCSSInt(el, 'paddingLeft');
paddingTop = getCSSInt(el, 'paddingTop');
paddingRight = getCSSInt(el, 'paddingRight');
paddingBottom = getCSSInt(el, 'paddingBottom');
renderImage(
ctx,
image,
0, //sx
0, //sy
image.width, //sw
image.height, //sh
x + paddingLeft + borders[3].width, //dx
y + paddingTop + borders[0].width, // dy
bounds.width - (borders[1].width + borders[3].width + paddingLeft + paddingRight), //dw
bounds.height - (borders[0].width + borders[2].width + paddingTop + paddingBottom) //dh
);
} else { } else {
h2clog("html2canvas: Error loading <img>:" + imgSrc); h2clog("html2canvas: Error loading <img>:" + imgSrc);
} }
@ -944,22 +946,7 @@ _html2canvas.Parse = function (images, options) {
renderListItem(el, stack, backgroundBounds); renderListItem(el, stack, backgroundBounds);
break; break;
case "CANVAS": case "CANVAS":
paddingLeft = getCSSInt(el, 'paddingLeft'); renderImage(ctx, el, el, bounds, borders);
paddingTop = getCSSInt(el, 'paddingTop');
paddingRight = getCSSInt(el, 'paddingRight');
paddingBottom = getCSSInt(el, 'paddingBottom');
renderImage(
ctx,
el,
0,
0,
el.width,
el.height,
x + paddingLeft + borders[3].width,
y + paddingTop + borders[0].width,
bounds.width - (borders[1].width + borders[3].width + paddingLeft + paddingRight),
bounds.height - (borders[0].width + borders[2].width + paddingTop + paddingBottom)
);
break; break;
} }