title: Converting svg to canvas and get the base64 String (canvg)
body: <p>We use in Java a native method with canvg <a href="https://code.google.com/p/canvg/" rel="nofollow">https://code.google.com/p/canvg/</a> to convert a SVG String to canvas to a png image. By that we need a renderCallback, because Images in the SVG String need time to be rendered, otherwise we get a wrong construct. Our code is: </p>
  
  <pre><code>private native String PNGExport(String SVGString)/*-{
  
      var canvas = $doc.createElement('canvas');
      canvas.id = "testcanvas";
  
      var img;
      $wnd.canvg(canvas, SVGString, {
          renderCallback : function() {
              img = canvas.toDataURL("image/png");
              var window = $wnd.open();
              window.document.write('&lt;img src="' + img + '"/&gt;');
          },
      });
      return img;
  
  }-*/;
  </code></pre>
  
  <p>So a tab containing the correct png image will be opened in the browser. But is there any possibility to get this String img = canvas.toDataURL("image/png"); working outside of the callback? The only thing I can imagine is to call a Java method inside of the function(), which gets the img var as parameter. I hope there is a better solution.</p>