Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c759600

Browse files
committed
Refactoring
1 parent 5f45968 commit c759600

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

src/renderers/Canvas.js

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ _html2canvas.Renderer.Canvas = function(options) {
55
safeImages = [],
66
testCanvas = document.createElement("canvas"),
77
testctx = testCanvas.getContext("2d"),
8+
Util = _html2canvas.Util,
89
canvas = options.canvas || doc.createElement('canvas');
910

1011
function createShape(ctx, args) {
@@ -40,33 +41,36 @@ _html2canvas.Renderer.Canvas = function(options) {
4041
ctx[item.name] = item['arguments'];
4142
break;
4243
case "function":
43-
if (item.name === "createPattern") {
44-
if (item['arguments'][0].width > 0 && item['arguments'][0].height > 0) {
45-
try {
46-
ctx.fillStyle = ctx.createPattern(item['arguments'][0], "repeat");
44+
switch(item.name) {
45+
case "createPattern":
46+
if (item['arguments'][0].width > 0 && item['arguments'][0].height > 0) {
47+
try {
48+
ctx.fillStyle = ctx.createPattern(item['arguments'][0], "repeat");
49+
}
50+
catch(e) {
51+
Util.log("html2canvas: Renderer: Error creating pattern", e.message);
52+
}
4753
}
48-
catch(e) {
49-
_html2canvas.Util.log("html2canvas: Renderer: Error creating pattern", e.message);
54+
break;
55+
case "drawShape":
56+
createShape(ctx, item['arguments']);
57+
break;
58+
case "drawImage":
59+
if (item['arguments'][8] > 0 && item['arguments'][7] > 0) {
60+
if (!options.taintTest || (options.taintTest && safeImage(item))) {
61+
ctx.drawImage.apply( ctx, item['arguments'] );
62+
}
5063
}
51-
}
52-
} else if (item.name === "drawShape") {
53-
createShape(ctx, item['arguments']);
54-
} else if (item.name === "drawImage") {
55-
if (item['arguments'][8] > 0 && item['arguments'][7] > 0) {
56-
if (!options.taintTest || (options.taintTest && safeImage(item))) {
57-
ctx.drawImage.apply( ctx, item['arguments'] );
58-
}
59-
}
60-
} else {
61-
ctx[item.name].apply(ctx, item['arguments']);
64+
break;
65+
default:
66+
ctx[item.name].apply(ctx, item['arguments']);
6267
}
6368
break;
6469
}
6570
}
6671

6772
return function(zStack, options, document, queue, _html2canvas) {
6873
var ctx = canvas.getContext("2d"),
69-
render = renderItem.bind(null, ctx),
7074
newCanvas,
7175
bounds,
7276
fstyle;
@@ -97,13 +101,15 @@ _html2canvas.Renderer.Canvas = function(options) {
97101
}
98102

99103
if (storageContext.ctx.storage) {
100-
storageContext.ctx.storage.forEach(render);
104+
storageContext.ctx.storage.forEach(function(item) {
105+
renderItem(ctx, item);
106+
});
101107
}
102108

103109
ctx.restore();
104110
});
105111

106-
_html2canvas.Util.log("html2canvas: Renderer: Canvas renderer done - returning canvas obj");
112+
Util.log("html2canvas: Renderer: Canvas renderer done - returning canvas obj");
107113

108114
if (options.elements.length === 1) {
109115
if (typeof options.elements[0] === "object" && options.elements[0].nodeName !== "BODY") {

0 commit comments

Comments
 (0)