将 SVG 添加到 FabricJS 画布时出现类型错误(不可迭代)

问题描述 投票:0回答:1
javascript svg canvas fabricjs
1个回答
0
投票

我不确定您使用的是哪个版本,但我已将您的代码添加到可运行的代码片段中,它似乎对我来说工作得很好:

let svg = `<svg viewBox="-8 -8 136 136">
<path stroke="#000000" stroke-width="8" d="m0 51.82677l0 0c0 -28.623135 23.203636 -51.82677 51.82677 -51.82677l0 0c13.745312 0 26.927654 5.4603047 36.64706 15.17971c9.719406 9.719404 15.17971 22.901749 15.17971 36.64706l0 0c0 28.623135 -23.203636 51.82677 -51.82677 51.82677l0 0c-28.623135 0 -51.82677 -23.203636 -51.82677 -51.82677zm25.913385 0l0 0c0 14.311565 11.60182 25.913387 25.913385 25.913387c14.311565 0 25.913387 -11.601822 25.913387 -25.913387c0 -14.311565 -11.601822 -25.913385 -25.913387 -25.913385l0 0c-14.311565 0 -25.913385 11.60182 -25.913385 25.913385z" fill="none"></path>
</svg>`;

let canvas = new fabric.Canvas("canvas");
let path = fabric.loadSVGFromString(svg, function(objects, options) {
  let obj = fabric.util.groupSVGElements(objects, options);

  obj.set({
    left: canvas.width / 2 - obj.width / 2,
    top: canvas.height / 2 - obj.height / 2
  });
  canvas.add(obj).renderAll();
});


canvas.renderAll();
<script src="//cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.6/fabric.js"></script>
<canvas id="canvas" width="800" height="600"></canvas><br/>

© www.soinside.com 2019 - 2024. All rights reserved.