因此,在尝试对我的应用程序实现撤销功能的过程中,我遇到了一个问题,撤销功能起作用了,但是,要么是canvas.remove()由于某种原因而不能工作,要么是对象:删除事件不触发。为了确保该对象确实存在,我console.log它并且足够确定它确实存在。但是fabric js并没有删除对象,也没有激发对象:删除(显然)

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);
});发布于 2019-06-12 07:41:14
这对你是有用的:
var objects = canvas.getObjects();
for(var i = 0; i < objects.length; i++){
//console.log(objects[i]);
canvas.remove(objects[i]);
}canvas.renderAll();https://stackoverflow.com/questions/48834551
复制相似问题