简体   繁体   中英

Fabric.js svg without outline not loading

I am having problems with loading svg files with fabric.js version 2.3.5. It seems that you cannot import shapes without an outline. See example below. This used to work fine in fabric.js version 1.7.22, but fails work with version 2.0.0 and higher. Is this intentional or an issue (bug)? Or do I need to modify the javascript code (see below)?

svg with outline (import works with fabric 2.3.5)

<svg width="100px" height="100px" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs/>
  <g>
    <path stroke="none" fill="#808080" d="M120 80 L120 160 40 160 40 80 120 80"/>
    <path fill="none" stroke="#ff0000" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" d="M120 80 L120 160 40 160 40 80 120 80"/>
  </g>
</svg>

svg without outline (import works with fabric 1.7.22, but not with 2.0.0 and higher)

<svg width="100px" height="100px" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs/>
  <g>
    <path stroke="none" fill="#808080" d="M120 80 L120 160 40 160 40 80 120 80"/>
  </g>
</svg>

Javascript:

var load_imgs=function() {
    fabric.loadSVGFromURL('mySvg.svg', function (objects, options) {
        var obj = fabric.util.groupSVGElements(objects, options);  
        var logo = new fabric.Group(obj.getObjects(), {
            left:20,
            top: 10,
            originX: 'left', 
            originY: 'top'
        });
       logo.scaleToHeight(50);      
       canvas.add(logo);
    });
}();

After some time, I accidently discovered that the groupSVGElements is no longer valid or needed. So a correct working code is:

var load_imgs=function() {
    fabric.loadSVGFromURL('mySvg.svg', function (objects, options) {
        var logo = new fabric.Group(objects, {
            left:20,
            top: 10,
            originX: 'left', 
            originY: 'top'
        });
       logo.scaleToHeight(50);      
       canvas.add(logo);
    });
}();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM