如何停用Fabric.js中的所有对象

时间:2014-01-15 07:23:46

标签: javascript canvas fabricjs

如何在保存图像之前停用Fabric.js中的所有对象?

function rasterize() {
  if (!myscript.Canvas.supports('toDataURL')) {
    alert('This browser doesn\'t provide means to serialize canvas to an image');
  }
  else {
    /*i want to deactivate all object here*/
    window.open(canvas.toDataURL('png'));
  }
}

5 个答案:

答案 0 :(得分:9)

canvas.deactivateAll(); =>没有事件被解雇 http://fabricjs.com/docs/fabric.Canvas.html#deactivateAll

canvas.deactivateAllWithDispatch(); =>如果activeObject存在事件'之前:选择:清除'和'选择:清除'被触发。 http://fabricjs.com/docs/fabric.Canvas.html#deactivateAllWithDispatch

答案 1 :(得分:0)

当我发现这个问题时,我一直在寻找:

/* Make sure that touch / click events do nothing */
canvas.deactivateAll();
canvas.selection = false;
canvas.forEachObject(function(o) {
    o.selectable = false;
});

答案 2 :(得分:0)

canvas.deactivateAll();
canvas.renderAll();

只有这两行也同样不需要将canvas.selection设为false。它用于禁用组选择。

答案 3 :(得分:0)

对我有用的东西(面料2.7):

this.canvas.forEachObject(object => {
    object.selectable = false;
    object.evented = false;
});

答案 4 :(得分:0)

从2.0.0版开始,deactivateAll()函数已由discardActiveObject()取代

source