在svg加载到画布后获取单个对象

时间:2016-01-30 05:26:05

标签: javascript html5-canvas fabricjs

我将svg加载到画布中并将其缩放以适合画布。然后我需要访问单个对象。这可能吗?



var canvas = new fabric.Canvas('c');
var url = 'http://fabricjs.com/assets/107.svg';
fabric.loadSVGFromURL(url, function(objects, options) {
  var svg = fabric.util.groupSVGElements(objects, options);
  svg.scaleToHeight(canvas.height);
  canvas.add(svg);
  svg.center();
  svg.setCoords();
  canvas.renderAll();
  console.log(svg);
});
fabric.loadSVGFromURL(url, function(objects, options) {
    var group = new fabric.Group(objects,options);
    canvas.add(group);
    group.scaleToHeight(canvas.getHeight());
    group.center();
    canvas.renderAll();
});

<script src="http://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.min.js"></script>
<canvas id="c" width="500" height="200"></canvas>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

面料js的一部分: canvas.toSVG()

然后您可以在生成的svg中选择invidual对象。