2016-03-28 169 views
0

所以我推斷,我可以通過添加SVG圖形到畫布:如何從Fabric.js畫布中刪除SVG?

fabric.loadSVGFromURL('img1.svg', function(objects, options) { 
    var obj1 = fabric.util.groupSVGElements(objects, options); 
    canvas.add(obj1); 
}); 

fabric.loadSVGFromURL('img2.svg', function(objects, options) { 
    var obj2 = fabric.util.groupSVGElements(objects, options); 
    canvas.add(obj2); 
}); 

假設我現在要刪除一個或兩個。我怎樣才能做到這一點?

回答

0

fabricjs我使用不同的方法刪除畫布中的對象。此方法刪除畫布中的任何對象。 fabricjs允許設置name每個對象,所以我們可以使用這個屬性來刪除object.like這(使用jQuery這個:)

var canvas = new fabric.Canvas('c'); 
fabric.loadSVGFromURL('test.svg', function(objects, options) { 
    var obj2 = fabric.util.groupSVGElements(objects, options); 
    obj2.name ="test_svg" //<<<<<<< here set name 
    canvas.add(obj2); 
    console.log(canvas.getObjects()) 
}); 

function del(){ 
$.each(canvas._objects, function(index, obj) { 
    if(obj == null) return; 
    $.each(obj, function(attr, value) { 
      //console.log(attr + ' == ' + value); 
      if(value == "test_svg"){ //<<<<< here we call name 
        //alert(index); 
        canvas.remove(canvas._objects[index]); // here we delete that object 
        canvas.renderAll(); 
       } 
      }); 
}); 
} 

我從負載SVG方法測試這和它的worked.so也許這是爲你工作..