!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t||self).canvasToSvg=e()}(this,(function(){function t(t,e){var i,r=Object.keys(e);for(i=0;i0){"path"===this.__currentElement.nodeName&&(this.__currentElementsToStyle||(this.__currentElementsToStyle={element:e,children:[]}),this.__currentElementsToStyle.children.push(this.__currentElement),this.__applyCurrentDefaultPath());var i=this.__createElement("g");e.appendChild(i),this.__currentElement=i}if(this.__currentElement){var r=this.__currentElement.getAttribute("transform");r?r+=" ":r="",this.__currentElement.setAttribute("transform",r+=t)}}}},c.scale=function(e,i){void 0===i&&(i=e),this.__addTransform(t("scale({x},{y})",{x:e,y:i}))},c.rotate=function(e){var i=180*e/Math.PI;this.__addTransform(t("rotate({angle},{cx},{cy})",{angle:i,cx:0,cy:0}))},c.translate=function(e,i){this.__addTransform(t("translate({x},{y})",{x:e,y:i}))},c.transform=function(e,i,r,s,n,a){this.__addTransform(t("matrix({a},{b},{c},{d},{e},{f})",{a:e,b:i,c:r,d:s,e:n,f:a}))},c.beginPath=function(){var t,e;this.__currentDefaultPath="",this.__currentPosition={},t=this.__createElement("path",{},!0),(e=this.__closestGroupOrSvg())&&(e.appendChild(t),this.__currentElement=t)},c.__applyCurrentDefaultPath=function(){var t=this.__currentElement;t&&("path"===t.nodeName?t.setAttribute("d",this.__currentDefaultPath):console.error("Attempted to apply path command to node",t.nodeName))},c.__addPathCommand=function(t){this.__currentDefaultPath+=" ",this.__currentDefaultPath+=t},c.moveTo=function(e,i){this.__currentElement&&("path"!==this.__currentElement.nodeName&&this.beginPath(),this.__currentPosition={x:e,y:i},this.__addPathCommand(t("M {x} {y}",{x:e,y:i})))},c.closePath=function(){this.__currentDefaultPath&&this.__addPathCommand("Z")},c.lineTo=function(e,i){this.__currentPosition={x:e,y:i},this.__currentDefaultPath.indexOf("M")>-1?this.__addPathCommand(t("L {x} {y}",{x:e,y:i})):this.__addPathCommand(t("M {x} {y}",{x:e,y:i}))},c.bezierCurveTo=function(e,i,r,s,n,a){this.__currentPosition={x:n,y:a},this.__addPathCommand(t("C {cp1x} {cp1y} {cp2x} {cp2y} {x} {y}",{cp1x:e,cp1y:i,cp2x:r,cp2y:s,x:n,y:a}))},c.quadraticCurveTo=function(e,i,r,s){this.__currentPosition={x:r,y:s},this.__addPathCommand(t("Q {cpx} {cpy} {x} {y}",{cpx:e,cpy:i,x:r,y:s}))},c.arcTo=function(t,e,i,r,n){var a=this.__currentPosition&&this.__currentPosition.x,o=this.__currentPosition&&this.__currentPosition.y;if(void 0!==a&&void 0!==o){if(n<0)throw new Error("IndexSizeError: The radius provided ("+n+") is negative.");if(a===t&&o===e||t===i&&e===r||0===n)this.lineTo(t,e);else{var h=s([a-t,o-e]),l=s([i-t,r-e]);if(h[0]*l[1]!=h[1]*l[0]){var _=Math.acos(Math.abs(h[0]*l[0]+h[1]*l[1])),c=s([h[0]+l[0],h[1]+l[1]]),u=n/Math.sin(_/2),d=t+u*c[0],p=e+u*c[1],g=[-h[1],h[0]],m=[l[1],-l[0]],f=function(t){var e=t[0];return t[1]>=0?Math.acos(e):-Math.acos(e)},v=f(g),b=f(m);this.lineTo(d+g[0]*n,p+g[1]*n),this.arc(d,p,n,v,b)}else this.lineTo(t,e)}}},c.stroke=function(){this.__currentElement&&("path"===this.__currentElement.nodeName&&this.__currentElement.setAttribute("paint-order","fill stroke markers"),this.__applyCurrentDefaultPath(),this.__applyStyleToElement("stroke"))},c.fill=function(t){this.__currentElement&&("path"===this.__currentElement.nodeName&&(this.__currentElement.setAttribute("paint-order","stroke fill markers"),"evenodd"===t&&this.__currentElement.setAttribute("fill-rule","evenodd")),this.__applyCurrentDefaultPath(),this.__applyStyleToElement("fill"))},c.rect=function(t,e,i,r){this.__currentElement&&("path"!==this.__currentElement.nodeName&&this.beginPath(),this.moveTo(t,e),this.lineTo(t+i,e),this.lineTo(t+i,e+r),this.lineTo(t,e+r),this.lineTo(t,e),this.closePath())},c.fillRect=function(t,e,i,r){var s;s=this.__createElement("rect",{x:t,y:e,width:i,height:r},!0),this.__closestGroupOrSvg().appendChild(s),this.__currentElement=s,this.__applyStyleToElement("fill")},c.strokeRect=function(t,e,i,r){var s;s=this.__createElement("rect",{x:t,y:e,width:i,height:r},!0),this.__closestGroupOrSvg().appendChild(s),this.__currentElement=s,this.__applyStyleToElement("stroke")},c.__clearCanvas=function(){var t=this.__closestGroupOrSvg();if(t){for(var e=t.getAttribute("transform"),i=this.__root.childNodes[1],r=i.childNodes,s=r.length-1;s>=0;s--)r[s]&&i.removeChild(r[s]);this.__currentElement=i,this.__groupStack=[],e&&this.__addTransform(e)}},c.clearRect=function(t,e,i,r){if(0!==t||0!==e||i!==this.width||r!==this.height){var s,n=this.__closestGroupOrSvg();s=this.__createElement("rect",{x:t,y:e,width:i,height:r,fill:"#FFFFFF"},!0),n.appendChild(s)}else this.__clearCanvas()},c.createLinearGradient=function(t,i,r,s){var n=this.__createElement("linearGradient",{id:e(this.__ids),x1:t+"px",x2:r+"px",y1:i+"px",y2:s+"px",gradientUnits:"userSpaceOnUse"},!1);return this.__defs.appendChild(n),new o(n,this)},c.createRadialGradient=function(t,i,r,s,n,a){var h=this.__createElement("radialGradient",{id:e(this.__ids),cx:s+"px",cy:n+"px",r:a+"px",fx:t+"px",fy:i+"px",gradientUnits:"userSpaceOnUse"},!1);return this.__defs.appendChild(h),new o(h,this)},c.__parseFont=function(){var t=/^\s*(?=(?:(?:[-a-z]+\s*){0,2}(italic|oblique))?)(?=(?:(?:[-a-z]+\s*){0,2}(small-caps))?)(?=(?:(?:[-a-z]+\s*){0,2}(bold(?:er)?|lighter|[1-9]00))?)(?:(?:normal|\1|\2|\3)\s*){0,3}((?:xx?-)?(?:small|large)|medium|smaller|larger|[.\d]+(?:\%|in|[cem]m|ex|p[ctx]))(?:\s*\/\s*(normal|[.\d]+(?:\%|in|[cem]m|ex|p[ctx])))?\s*([-_,\'\"\sa-z0-9]+?)\s*$/i.exec(this.font);if(t){var e={style:t[1]||"normal",size:t[4]||"10px",family:t[6]||"sans-serif",weight:t[3]||"normal",decoration:t[2]||"normal",href:null};return"underline"===this.__fontUnderline&&(e.decoration="underline"),this.__fontHref&&(e.href=this.__fontHref),e}},c.__wrapTextLink=function(t,e){if(t.href){var i=this.__createElement("a");return i.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href",t.href),i.appendChild(e),i}return e},c.__applyText=function(t,e,s,n){var a=this.__parseFont(),o=this.__closestGroupOrSvg();if(a){var h,l,_=this.__createElement("text",{"font-family":a.family,"font-size":a.size,"font-style":a.style,"font-weight":a.weight,"text-decoration":a.decoration,x:e,y:s,"text-anchor":(l=this.textAlign,i[l]||i.start),"dominant-baseline":(h=this.textBaseline,r[h]||r.alphabetic)},!0);_.appendChild(this.__document.createTextNode(t)),this.__currentElement=_,this.__applyStyleToElement(n),o.appendChild(this.__wrapTextLink(a,_))}},c.fillText=function(t,e,i){this.__applyText(t,e,i,"fill")},c.strokeText=function(t,e,i){this.__applyText(t,e,i,"stroke")},c.measureText=function(t){return this.__ctx.font=this.font,this.__ctx.measureText(t)},c.arc=function(e,i,r,s,n,a){if(s!==n){(s%=2*Math.PI)==(n%=2*Math.PI)&&(n=(n+2*Math.PI-.001*(a?-1:1))%(2*Math.PI));var o,h=e+r*Math.cos(n),l=i+r*Math.sin(n),_=e+r*Math.cos(s),c=i+r*Math.sin(s),u=a?0:1,d=n-s;d<0&&(d+=2*Math.PI),o=a?d>Math.PI?0:1:d>Math.PI?1:0,this.lineTo(_,c),this.__addPathCommand(t("A {rx} {ry} {xAxisRotation} {largeArcFlag} {sweepFlag} {endX} {endY}",{rx:r,ry:r,xAxisRotation:0,largeArcFlag:o,sweepFlag:u,endX:h,endY:l})),this.__currentPosition={x:h,y:l}}},c.clip=function(){var i=this.__closestGroupOrSvg(),r=this.__createElement("clipPath"),s=e(this.__ids),n=this.__createElement("g");i&&this.__currentElement&&(this.__applyCurrentDefaultPath(),i.removeChild(this.__currentElement),r.setAttribute("id",s),r.appendChild(this.__currentElement),this.__defs.appendChild(r),i.setAttribute("clip-path",t("url(#{id})",{id:s})),i.appendChild(n),this.__currentElement=n)},c.drawImage=function(){var t,e,i,r,s,n,a,o,h,l,c,u,d,p,g=Array.prototype.slice.call(arguments),m=g[0],f=0,v=0;if(3===g.length)t=g[1],e=g[2],i=s=m.width,r=n=m.height;else if(5===g.length)t=g[1],e=g[2],i=g[3],r=g[4],s=m.width,n=m.height;else{if(9!==g.length)throw new Error("Invalid number of arguments passed to drawImage: "+arguments.length);f=g[1],v=g[2],s=g[3],n=g[4],t=g[5],e=g[6],i=g[7],r=g[8]}a=this.__closestGroupOrSvg();var b="translate("+t+", "+e+")";if(m instanceof _){if((o=m.getSvg().cloneNode(!0)).childNodes&&o.childNodes.length>1){for(h=o.childNodes[0];h.childNodes.length;)p=h.childNodes[0].getAttribute("id"),this.__ids[p]=p,this.__defs.appendChild(h.childNodes[0]);if(l=o.childNodes[1]){var w=l.getAttribute("transform");l.setAttribute("transform",w?w+" "+b:b),a.appendChild(l)}}}else if("CANVAS"===m.nodeName||"IMG"===m.nodeName){if((c=this.__createElement("image")).setAttribute("width",i),c.setAttribute("height",r),c.setAttribute("preserveAspectRatio","none"),f||v||s!==m.width||n!==m.height){if((u=this.__document.createElement("canvas")).width=i,u.height=r,!(d=u.getContext("2d")))return;d.drawImage(m,f,v,s,n,0,0,i,r),m=u}c.setAttribute("transform",b),c.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href","CANVAS"===m.nodeName?m.toDataURL():m.getAttribute("src")),a.appendChild(c)}},c.createPatternSVG=function(t,i){var r=this.__document.createElementNS("http://www.w3.org/2000/svg","pattern"),s=e(this.__ids);if(r.setAttribute("id",s),r.setAttribute("width",t.width),r.setAttribute("height",t.height),r.setAttribute("patternUnits","userSpaceOnUse"),t.angle&&r.setAttribute("patternTransform","rotate("+t.angle+")"),t.path&&t.style){var n=this.__document.createElementNS("http://www.w3.org/2000/svg","path");n.setAttribute("d",t.path),n.setAttribute("style",t.style),n.setAttribute("fill",t.fill),r.appendChild(n),this.__defs.appendChild(r)}return new h(r,this)},c.createPattern=function(t,i){var r,s=this.__document.createElementNS("http://www.w3.org/2000/svg","pattern"),n=e(this.__ids);return s.setAttribute("id",n),s.setAttribute("width",t.width),s.setAttribute("height",t.height),s.setAttribute("patternUnits","userSpaceOnUse"),"CANVAS"===t.nodeName||"IMG"===t.nodeName?((r=this.__document.createElementNS("http://www.w3.org/2000/svg","image")).setAttribute("width",t.width),r.setAttribute("height",t.height),r.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href","CANVAS"===t.nodeName?t.toDataURL():t.getAttribute("src")),s.appendChild(r),this.__defs.appendChild(s)):t instanceof _&&(s.appendChild(t.__root.childNodes[1]),this.__defs.appendChild(s)),new h(s,this)},c.setLineDash=function(t){this.lineDash=t&&t.length>0?t.join(","):null},_}();return _}));