2

这就是我将图像加载到画布上的方式:

$(".img-link").on("click", function(e) {
    e.preventDefault();
    var url = $(this).attr('href'),
        extension = url.substr((~-url.lastIndexOf(".") >>> 0) + 2);

    if(extension !== 'svg') {
        fabric.Image.fromURL( url, function(oImg) {
            var ow = oImg.getWidth(),
                oh = oImg.getHeight();

            oImg.lockUniScaling = true;
            oImg.set({'left': ow/2, 'top': oh/2});
            canvas.add(oImg);
        });
    } else {
        var group = [];
        fabric.loadSVGFromURL(url, function(objects,options) {
        var loadedObjects = new fabric.Group(group);
        loadedObjects.set({
                left: 100,
                top: 100,
                sourcePath: url
        });
        canvas.add(loadedObjects);
        canvas.renderAll();
        },function(item, object) {
                object.set('id',item.getAttribute('id'));
                group.push(object);
        });
    }
});

console.log(JSON.stringify(canvas.toDatalessJSON()))给出以下输出:

{"objects":[{"type":"group","originX":"center","originY":"center","left":100,"top":100,"width":200," height":200,"fill":"rgb(0,0,0)","overlayFill":null,"stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt ","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity": 1,"shadow":null,"visible":true,"clipTo":null,"objects":[{"type":"path","originX":"center","originY":"center", “左”:0,top":0,"width":200,"height":200,"fill":"#aa0000","overlayFill":null,"stroke":null,"strokeWidth":1,"strokeDashArray":null, "strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":假,"不透明度":1,"阴影":null,"可见":true,"clipTo":null,"路径":[["M",91.5,73],["c",0,-30 ,40,-30,40,0],["c",0,30,-4​​0,60,-40,60],["c",0,0,-40,-30,-4​​0,- 60],["c",0,-30,40,-30,40,0]],"pathOffset":{"x":0,"y":0}}]}],"背景": ""}:200,"fill":"#aa0000","overlayFill":null,"stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter ","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null, "visible":true,"clipTo":null,"path":[["M",91.5,73],["c",0,-30,40,-30,40,0],["c ",0,30,-4​​0,60,-40,60],["c",0,0,-40,-30,-4​​0,-60],["c",0,-30,40 ,-30,40,0]],"pathOffset":{"x":0,"y":0}}]}],"background":""}:200,"fill":"#aa0000","overlayFill":null,"stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter ","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null, "visible":true,"clipTo":null,"path":[["M",91.5,73],["c",0,-30,40,-30,40,0],["c ",0,30,-4​​0,60,-40,60],["c",0,0,-40,-30,-4​​0,-60],["c",0,-30,40 ,-30,40,0]],"pathOffset":{"x":0,"y":0}}]}],"background":""}strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0 ,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"path":[["M",91.5,73 ],["c",0,-30,40,-30,40,0],["c",0,30,-4​​0,60,-40,60],["c",0,0 ,-40,-30,-4​​0,-60],["c",0,-30,40,-30,40,0]],"pathOffset":{"x":0,"y": 0}}]}],"背景":""}strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0 ,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"path":[["M",91.5,73 ],["c",0,-30,40,-30,40,0],["c",0,30,-4​​0,60,-40,60],["c",0,0 ,-40,-30,-4​​0,-60],["c",0,-30,40,-30,40,0]],"pathOffset":{"x":0,"y": 0}}]}],"背景":""}scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"path" :[["M",91.5,73],["c",0,-30,40,-30,40,0],["c",0,30,-4​​0,60,-40,60 ],["c",0,0,-40,-30,-4​​0,-60],["c",0,-30,40,-30,40,0]],"pathOffset":{ "x":0,"y":0}}]}],"背景":""}scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"path" :[["M",91.5,73],["c",0,-30,40,-30,40,0],["c",0,30,-4​​0,60,-40,60 ],["c",0,0,-40,-30,-4​​0,-60],["c",0,-30,40,-30,40,0]],"pathOffset":{ "x":0,"y":0}}]}],"背景":""}y":0}}]}],"背景":""}y":0}}]}],"背景":""}

如果我检查canvas.item(0).sourcePath它会给我heart.svg

怎么了?

谢谢

4

2 回答 2

4

您正在使用fabric.Group而不是fabric.Pathor fabric.PathGroupfabric.PathGroup如果您正在调用,则仅fabric.Path使用 sourcePath url 替换路径定义canvas.toDatalessJSON()。将该代码用于您的 svg 元素:

fabric.loadSVGFromURL(url, function(objects, options) {
   // Group elements to fabric.PathGroup (more than 1 elements) or
   // to fabric.Path
   var loadedObject = fabric.util.groupSVGElements(objects, options);
   // Set sourcePath
   loadedObject.set('sourcePath', url);

   canvas.add(loadedObject);
});

在这里你可以看到一个 jsfiddle:http: //jsfiddle.net/Kienz/526wC/

在您的 jsfiddle 中,您使用的是 fabric.Image 而不是 fabric.Path。

于 2013-10-24T07:54:27.847 回答
1

看起来您sourcePathfabric.Groupobject 而不是fabric.Path. 如果将sourcePath属性设置为路径对象,它应该可以工作。

于 2013-10-22T12:00:16.853 回答