Refactoring

This commit is contained in:
Niklas von Hertzen 2013-09-04 19:29:12 +03:00
parent 5f45968154
commit c759600c06

View File

@ -5,6 +5,7 @@ _html2canvas.Renderer.Canvas = function(options) {
safeImages = [], safeImages = [],
testCanvas = document.createElement("canvas"), testCanvas = document.createElement("canvas"),
testctx = testCanvas.getContext("2d"), testctx = testCanvas.getContext("2d"),
Util = _html2canvas.Util,
canvas = options.canvas || doc.createElement('canvas'); canvas = options.canvas || doc.createElement('canvas');
function createShape(ctx, args) { function createShape(ctx, args) {
@ -40,24 +41,28 @@ _html2canvas.Renderer.Canvas = function(options) {
ctx[item.name] = item['arguments']; ctx[item.name] = item['arguments'];
break; break;
case "function": case "function":
if (item.name === "createPattern") { switch(item.name) {
case "createPattern":
if (item['arguments'][0].width > 0 && item['arguments'][0].height > 0) { if (item['arguments'][0].width > 0 && item['arguments'][0].height > 0) {
try { try {
ctx.fillStyle = ctx.createPattern(item['arguments'][0], "repeat"); ctx.fillStyle = ctx.createPattern(item['arguments'][0], "repeat");
} }
catch(e) { catch(e) {
_html2canvas.Util.log("html2canvas: Renderer: Error creating pattern", e.message); Util.log("html2canvas: Renderer: Error creating pattern", e.message);
} }
} }
} else if (item.name === "drawShape") { break;
case "drawShape":
createShape(ctx, item['arguments']); createShape(ctx, item['arguments']);
} else if (item.name === "drawImage") { break;
case "drawImage":
if (item['arguments'][8] > 0 && item['arguments'][7] > 0) { if (item['arguments'][8] > 0 && item['arguments'][7] > 0) {
if (!options.taintTest || (options.taintTest && safeImage(item))) { if (!options.taintTest || (options.taintTest && safeImage(item))) {
ctx.drawImage.apply( ctx, item['arguments'] ); ctx.drawImage.apply( ctx, item['arguments'] );
} }
} }
} else { break;
default:
ctx[item.name].apply(ctx, item['arguments']); ctx[item.name].apply(ctx, item['arguments']);
} }
break; break;
@ -66,7 +71,6 @@ _html2canvas.Renderer.Canvas = function(options) {
return function(zStack, options, document, queue, _html2canvas) { return function(zStack, options, document, queue, _html2canvas) {
var ctx = canvas.getContext("2d"), var ctx = canvas.getContext("2d"),
render = renderItem.bind(null, ctx),
newCanvas, newCanvas,
bounds, bounds,
fstyle; fstyle;
@ -97,13 +101,15 @@ _html2canvas.Renderer.Canvas = function(options) {
} }
if (storageContext.ctx.storage) { if (storageContext.ctx.storage) {
storageContext.ctx.storage.forEach(render); storageContext.ctx.storage.forEach(function(item) {
renderItem(ctx, item);
});
} }
ctx.restore(); ctx.restore();
}); });
_html2canvas.Util.log("html2canvas: Renderer: Canvas renderer done - returning canvas obj"); Util.log("html2canvas: Renderer: Canvas renderer done - returning canvas obj");
if (options.elements.length === 1) { if (options.elements.length === 1) {
if (typeof options.elements[0] === "object" && options.elements[0].nodeName !== "BODY") { if (typeof options.elements[0] === "object" && options.elements[0].nodeName !== "BODY") {