Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading and displaying svg

how do you load a .svg into a fabricjs.html page?

my fabric fiddle attempts to load a http .svg file, but I have, of course, tried local versions. I have used .svg files generated from adobe illustrator and fabric itself.

fabric.loadSVGFromURL( gvs_svgSrc, 
                       function ( argo ) 
                         { //alert( "load handler : " + argo ) ; 
                           // ... returns: #<fabric.Rect>
                           lvo_SVG = argo.set({ left: 250, top: 200, angle: 0 });
                           lvo_SVG = argo.scale(0.25);
                           gvo_canvas.add( lvo_SVG );
                           gvo_canvas.renderAll();
                         });
like image 854
dsdsdsdsd Avatar asked Mar 08 '13 21:03

dsdsdsdsd


2 Answers

Here's how you load SVG from an external .svg

Here's code (be sure you link to your own fabricJS .js):

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript" src="fabric.min.js"></script>

<style>
    body{ background-color: ivory; }
    canvas{border: 1px solid red; }
</style>

<script>
    $(function(){

        var canvas = new fabric.Canvas('canvas');
        var group = [];

        fabric.loadSVGFromURL("http://fabricjs.com/assets/1.svg",function(objects,options) {

            var loadedObjects = new fabric.Group(group);

            loadedObjects.set({
                    left: 100,
                    top: 100,
                    width:175,
                    height:175
            });

            canvas.add(loadedObjects);
            canvas.renderAll();

        },function(item, object) {
                object.set('id',item.getAttribute('id'));
                group.push(object);
        });


    }); // end $(function(){});
</script>

</head>

<body>
    <canvas id="canvas" width="300" height="300"></canvas> 
</body>
</html>
like image 82
markE Avatar answered Sep 30 '22 20:09

markE


fabric.loadSVGFromURL('http://example.com/_bg.svg',function(objects, options){
  var svgData = fabric.util.groupSVGElements(objects, options);
  svgData.top = 30;
  svgData.left = 50;
  canvas.add(svgData);
});
  1. Load .svg file in canvas from a valid .svg URL using fabric
  2. Load svg file in canvas trough fabric
like image 42
Milan Krushna Avatar answered Sep 30 '22 18:09

Milan Krushna