简体   繁体   English

没有轮廓的Fabric.js SVG无法加载

[英]Fabric.js svg without outline not loading

I am having problems with loading svg files with fabric.js version 2.3.5. 我在用Fabric.js版本2.3.5加载svg文件时遇到问题。 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. 过去在fabric.js 1.7.22版中可以正常工作,但在2.0.0版或更高版本上无法使用。 Is this intentional or an issue (bug)? 这是故意的还是问题(错误)? Or do I need to modify the javascript code (see below)? 还是我需要修改javascript代码(请参见下文)?

svg with outline (import works with fabric 2.3.5) 带有轮廓的svg(导入适用于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(导入适用于结构1.7.22,但不适用于2.0.0及更高版本)

<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: 使用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. 一段时间后,我意外地发现groupSVGElements不再有效或不再需要。 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);
    });
}();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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