这就是我将图像加载到画布上的方式:
$(".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,-40,60,-40,60],["c",0,0,-40,-30,-40,- 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,-40,60,-40,60],["c",0,0,-40,-30,-40,-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,-40,60,-40,60],["c",0,0,-40,-30,-40,-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,-40,60,-40,60],["c",0,0 ,-40,-30,-40,-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,-40,60,-40,60],["c",0,0 ,-40,-30,-40,-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,-40,60,-40,60 ],["c",0,0,-40,-30,-40,-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,-40,60,-40,60 ],["c",0,0,-40,-30,-40,-60],["c",0,-30,40,-30,40,0]],"pathOffset":{ "x":0,"y":0}}]}],"背景":""}y":0}}]}],"背景":""}y":0}}]}],"背景":""}
如果我检查canvas.item(0).sourcePath
它会给我heart.svg
怎么了?
谢谢