From 8a8d081561832fbd7835689cda848987a8056ddf Mon Sep 17 00:00:00 2001 From: Fu Zhen Date: Thu, 26 Nov 2015 22:31:35 +0800 Subject: [PATCH] when drawImage a empty C2S context Null pointer error will be thrown when ctx.drawImage(ctx) with an empty C2S context with no childNodes. These code may fix the problem. --- canvas2svg.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/canvas2svg.js b/canvas2svg.js index 0964b39..db95bab 100644 --- a/canvas2svg.js +++ b/canvas2svg.js @@ -1038,17 +1038,21 @@ //canvas2svg mock canvas context. In the future we may want to clone nodes instead. //also I'm currently ignoring dw, dh, sw, sh, sx, sy for a mock context. svg = image.getSvg(); - defs = svg.childNodes[0]; - while(defs.childNodes.length) { - id = defs.childNodes[0].getAttribute("id"); - this.__ids[id] = id; - this.__defs.appendChild(defs.childNodes[0]); + if (svg.childNodes && svg.childNodes.length > 1) { + defs = svg.childNodes[0]; + while(defs.childNodes.length) { + id = defs.childNodes[0].getAttribute("id"); + this.__ids[id] = id; + this.__defs.appendChild(defs.childNodes[0]); + } + group = svg.childNodes[1]; + if (group) { + parent.appendChild(group); + this.__currentElement = group; + this.translate(dx, dy); + this.__currentElement = currentElement; + } } - group = svg.childNodes[1]; - parent.appendChild(group); - this.__currentElement = group; - this.translate(dx, dy); - this.__currentElement = currentElement; } else if(image.nodeName === "CANVAS" || image.nodeName === "IMG") { //canvas or image svgImage = this.__createElement("image");