簡體   English   中英

fabric js canvas.remove()不刪除對象

[英]fabric js canvas.remove() not removing the object

所以我在嘗試對我的應用程序實現撤消功能的過程中遇到了一個問題,撤消功能可以正常工作,但是全部是因為某些原因canvas.remove()不起作用或object:removed事件不射擊。 為了確保該對象確實存在,請在console.log中確保它確實存在。.但是fabric js不會刪除該對象,也不會觸發該對象: 輸出console.log

canvas.on('object:added',function(){
   h = [];
});
 function undo(){
    if(canvas._objects.length>0){
        console.log(h);
        h.push(canvas._objects.pop());
        var lastItem = h[h.length - 1];
        //Logs the object - check the screenshot
        console.log(lastItem);
        canvas.remove(lastItem);
        canvas.renderAll();
    }
}
 $("#undo").click(function()
{
    undo();
});
 //Not firing even though i removed the object..
  canvas.on('object:removed',function(object)
{
   console.warn(object);
});

如果您要使用canvas.remove()刪除對象,則只會canvas.remove() 'object:removed'事件。

DEMO

 var canvas = new fabric.Canvas('canvas'); canvas.add(new fabric.Circle({ left:50,top:50,radius:50,fill:'red' })) canvas.add(new fabric.Circle({ left:150,top:150,radius:50,fill:'green' })) canvas.add(new fabric.Circle({ left:250,top:250,radius:50,fill:'blue' })) canvas.add(new fabric.Rect({ left:250,top:150,width:100,height:100 })); canvas.requestRenderAll(); function undo(){ var object = canvas.item(canvas.getObjects().length-1); canvas.remove(object); } canvas.on('object:removed',function(object){ console.warn(object); }) 
 canvas{ border:2px solid #000; } 
 <script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script> <button onclick='undo()'>undo</button> <canvas id='canvas' width=500 height=400> 

這將為您工作:

var objects = canvas.getObjects();
for(var i = 0; i < objects.length; i++){
  //console.log(objects[i]);     
  canvas.remove(objects[i]);
}canvas.renderAll();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM