From c7df7fa81e9d8b392d0fc2a894f72af7ba54e306 Mon Sep 17 00:00:00 2001 From: Michael Deal Date: Sat, 13 Aug 2016 16:59:17 -0700 Subject: [PATCH] fix issue with mixing transform with path commands Fixes this case: var ctx = new C2S() ctx.save() ctx.translate(10, 10) ctx.fillStyle = 'red' ctx.fillRect(0, 0, 10, 10) ctx.fillStyle = 'blue' ctx.fillRect(10, 0, 10, 10) ctx.fillStyle = 'green' ctx.fillRect(20, 0, 10, 10) ctx.fillStyle = 'pink' ctx.translate(10, 10); ctx.rect(0, 0, 10, 10); ctx.translate(10, 10); ctx.rect(0, 0, 100, 100); ctx.scale(2, 2) ctx.rect(100, 100, 10, 10); ctx.fill() window.open(`data:image/svg+xml;utf8,${ctx.getSerializedSvg()}`) --- canvas2svg.js | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/canvas2svg.js b/canvas2svg.js index 9e03866..2be977d 100644 --- a/canvas2svg.js +++ b/canvas2svg.js @@ -340,13 +340,23 @@ * @private */ ctx.prototype.__applyStyleToCurrentElement = function (type) { + var currentElement = this.__currentElement; + var currentStyleGroup = this.__currentElementsToStyle; + if (currentStyleGroup) { + currentElement.setAttribute(type, ""); + currentElement = currentStyleGroup.element; + currentStyleGroup.children.forEach(function (node) { + node.setAttribute(type, ""); + }) + } + var keys = Object.keys(STYLES), i, style, value, id, regex, matches; - for (i=0; i 0) { + if (this.__currentElement.nodeName === "path") { + this.__currentElementsToStyle || (this.__currentElementsToStyle = { element: parent, children: [] }); + this.__currentElementsToStyle.children.push(this.__currentElement) + this.__applyCurrentDefaultPath(); + } + var group = this.__createElement("g"); parent.appendChild(group); this.__currentElement = group; @@ -548,11 +562,11 @@ * @private */ ctx.prototype.__applyCurrentDefaultPath = function () { - if (this.__currentElement.nodeName === "path") { - var d = this.__currentDefaultPath; - this.__currentElement.setAttribute("d", d); + var currentElement = this.__currentElement; + if (currentElement.nodeName === "path") { + currentElement.setAttribute("d", this.__currentDefaultPath); } else { - throw new Error("Attempted to apply path command to node " + this.__currentElement.nodeName); + console.error("Attempted to apply path command to node", currentElement.nodeName); } };