Can't load SVG from URL

2019-08-23 06:26发布

问题:

I want to load different shapes in FabricJS based Canvas using loadSVGFromURL(), but can't. Documentation is also not complete on this. I just want a complete example. I can load it from string, but string creates spaces which creates problems when rendering. Here is my code:

var canvas = new fabric.Canvas('canvas');
        fabric.loadSVGFromURL('BlueFlower.svg', function (objects) {
            var SVG = fabric.util.groupSVGElements(objects, options);
            canvas.add(SVG).centerObject(SVG).renderAll();
            SVG.setCoords();
        });

回答1:

I have done an example on jsfiddle, that I create two objects and I load an svg image from url, you can take a look.

Here is an example

The snippet for load svg is this:

var site_url = 'http://fabricjs.com/assets/1.svg';

fabric.loadSVGFromURL(site_url, function(objects) {
  var group = new fabric.PathGroup(objects, {
    left: 165,
    top: 100,
    width: 295,
    height: 211
  });
  canvas.add(group);
  canvas.renderAll();
});


标签: fabricjs