Reputation: 3812
Can anybody tell me how can I load an SVG file using fabric js ungrouped. With the following code the SVG file is loading but it is appeared as a single object.
<html>
<head>
<script src="fabric.min.js"></script>
<script src="jquery.min.js"></script>
<script>
$(function (){
var canvas = this.__canvas = new fabric.Canvas('c');
fabric.Object.prototype.transparentCorners = true;
fabric.loadSVGFromURL("xxx.svg", function (objects, options) {
var group = fabric.util.groupSVGElements(objects, options);
canvas.add(group);
canvas.renderAll();
});
});
</script>
</head>
<body>
<canvas id="c" width="800px" height="600px"></canvas>
</body>
</html>
PS: I tried adding the objects one-by-one (using a loop) to the canvas but it was not worked. I also tried this: Load SVG from file to canvas and ungroup it
Upvotes: 2
Views: 4303
Reputation: 31
This is what I did for a current project I am working on to load a svg file ungrouped.
var canvas = new fabric.Canvas('myCanvas');
fabric.loadSVGFromURL('svgfile.svg', function (objects) {
canvas.add.apply(canvas, objects);
canvas.renderAll();
});
Now you can do thing like this to select a object.
canvas.on('object:selected', function (e) {
var activeObject = e.target;
activeObject.setFill('purple');
});
Upvotes: 3
Reputation: 1426
Don't use var group = fabric.util.groupSVGElements(objects, options);
instead you can loop through objects
and you can add them one by one on canvas.
Upvotes: 2