First time user of FabricJs and I’m trying to load these SVG files using FabricJS as shown in their examples
JavaScript
x
6
1
var canvas = new fabric.Canvas('c');
2
3
var img = fabric.Image.fromURL('http://instathumbs.com.s3.amazonaws.com/assets/items/Image/Graphics/blueprint.svg?1', function(item) {
4
canvas.add(item);
5
canvas.renderAll();
6
}, {crossOrigin: "anonymous"});
JavaScript
1
11
11
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.0.0-rc.1/fabric.js"></script>
2
3
<div>
4
Original Image:
5
<img src="http://instathumbs.com.s3.amazonaws.com/assets/items/Image/Graphics/blueprint.svg" width="150" height="150" />
6
</div>
7
8
<div style="background: red">
9
Canvas:
10
<canvas id="c" width="500" height="500"></canvas>
11
</div>
If you run the code snippet you will see that only the top part of the image is loaded instead of the entire SVG. I tried doing this with the other method where I create a new Image()
and then use new Fabric.Image(img..
with same results?
My question is how do I show the entire image inside the resizer? Is this a bug in FabricJS or there is something I’m doing wrong.
I’ve tried it with dozens of different SVG and it’s always the case.
Advertisement
Answer
JavaScript
1
10
10
1
var canvas = new fabric.Canvas('c');
2
var site_url = 'https://instathumbs.com.s3.amazonaws.com/assets/items/Image/Graphics/blueprint.svg?1';
3
fabric.loadSVGFromURL(site_url, function(objects, options) {
4
5
var obj = fabric.util.groupSVGElements(objects, options);
6
obj.set({
7
left:50,top:50,scaleX:4,scaleY:4
8
})
9
canvas.add(obj).renderAll();
10
});
JavaScript
1
11
11
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.0.0-rc.1/fabric.js"></script>
2
3
<div>
4
Original Image:
5
<img src="http://instathumbs.com.s3.amazonaws.com/assets/items/Image/Graphics/blueprint.svg" width="150" height="150" />
6
</div>
7
8
<div style="background: red">
9
Canvas:
10
<canvas id="c" width="500" height="500"></canvas>
11
</div>
You need to use loadSVGFromURL