From efcdc56ccd48cfa51ddcb0d79f1d297c3179ce96 Mon Sep 17 00:00:00 2001 From: fuzhen Date: Wed, 6 Jan 2016 23:27:49 +0800 Subject: [PATCH] fixed a bug with globalAlpha --- canvas2svg.js | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/canvas2svg.js b/canvas2svg.js index e34309a..63d73db 100644 --- a/canvas2svg.js +++ b/canvas2svg.js @@ -141,10 +141,16 @@ apply : "stroke" }, "globalAlpha": { - svgAttr : "opacity", + svgAttr : "stroke-opacity", canvas : 1, svg : 1, - apply : "fill stroke" + apply : "stroke" + }, + "globalAlpha": { + svgAttr : "fill-opacity", + canvas : 1, + svg : 1, + apply : "fill" }, "font":{ //font converts to multiple svg attributes, there is custom logic for this @@ -368,10 +374,24 @@ regex = /rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d?\.?\d*)\s*\)/gi; matches = regex.exec(value); this.__currentElement.setAttribute(style.svgAttr, format("rgb({r},{g},{b})", {r:matches[1], g:matches[2], b:matches[3]})); - this.__currentElement.setAttribute(style.svgAttr+"-opacity", matches[4]); + //should take globalAlpha here + var opacity = matches[4]; + var globalAlpha = this.globalAlpha; + if (globalAlpha != null) { + opacity *= globalAlpha; + } + this.__currentElement.setAttribute(style.svgAttr+"-opacity", opacity); } else { + if (keys[i] === 'globalAlpha') { + if (this.__currentElement.getAttribute(style.svgAttr)) { + //fill-opacity or stroke-opacity has already been set by stroke or fill. + continue; + } + } //otherwise only update attribute if right type, and not svg default this.__currentElement.setAttribute(style.svgAttr, value); + + } } }