0
votes

I have this svg

<div style="width: 84px; height: 78px; position: absolute; margin: 0px auto; left: 88px; top: 67px;">
    <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 138 138" style="position: relative; left: 0px; top: 0px; width: 100%; height: 100%; enable-background:new 0 0 138 138;">
                  <defs>
                     <filter id="blurFilter1" y="-10" height="40" x="-10" width="150">
                      <feOffset in="SourceAlpha" dx="3" dy="3" result="offset2"></feOffset>
                      <feGaussianBlur in="offset2" stdDeviation="3" result="blur2"></feGaussianBlur>
                      <feMerge>
                        <feMergeNode in="blur2"></feMergeNode>
                        <feMergeNode in="SourceGraphic"></feMergeNode>
                      </feMerge>
                    </filter>
                  </defs>
                  <g filter="url(#blurFilter1)">
                    <rect class="rrfNode" x="0" y="0" width="130" height="130" fill="rgb(235,129,48)" rx="15" ry="15" stroke="black" stroke-width="0.5"></rect>
                    <text style="font-size:25px; font-family: 'Lora', serif;" x="50%" y="50%" stroke="none" text-anchor="middle" fill="white" dy=".3em">C-Corp1</text>

                    <rect x="12" y="15" width="110" height="30" rx="15" ry="15" stroke-width="0.5" stroke-opacity="0" fill="rgb(255,255,0)" fill-opacity="0.8" visibility="visible"></rect>

                    <text style="font-size:25px; font-family: 'Lora', serif;" y="25%" x="50%" stroke="none" text-anchor="middle" fill="red" dy=".3em" visibility="visible">New</text>                                           
                  </g>
    </svg>
</div>

I want to draw this svg on canvas at the same position on the canvas

<canvas id="myCanvas1" width="935" height="768"></canvas>

I am using the the canvg library

I have create a fiddle for this at

https://jsfiddle.net/jnwmudwc/1/. 

This clearly shows that the source svg should attain the same position when drawn on the canvas , but it does not .

It is being drawn at the wrong position even when the coordinates are used in the drawingContext.drawSvg() method are the same as the source svg.

I have tried quite a few strategies , but they have not worked . Please suggest a workable solution to this problem.

1

1 Answers

1
votes

Ok after going through a lot of pain I have a solution . I would like to share this so that others donot have to face the same troubles as I have.

Replacing drawingContext.drawSvg() with drawingContext.drawImage() solved this issue .

        var nd = $("#node1").find("svg")[0].outerHTML;

        var img = new Image();
        img.src = "data:image/svg+xml;base64,"+btoa(nd);

        img.onload = function(){
            WPBContext.drawImage(img, x, y , w, h);
        }

As you can see that I am converting the svg contained in a div to base64 encoded string and then calling the drawImage() function .

I would like to say that though this solved my issue , but when the svg is something other than geometric shapes or when the svg has multiple colors those svgs are rendered on the canvas in colour black.

I am trying to find a solution to this as well in the mean time I hope some of you can point out what is going wrong with this .

I have not been able to update the jsfiddle to reflect the change , but others can feel free to make the neccessary changes so that it helps others.