javascript 如何使画布中的对象无法选择?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11619626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make objects in canvas unselectable?
提问by Yevgen
I want to make all object in canvas unselectable. I've found selectablemethod but i didn't find the way to implement it to all objects.
我想让画布中的所有对象都无法选择。我找到了可选方法,但我没有找到将它实现到所有对象的方法。
回答by Jitendra Pawar
You can make all the elements un-selectable using below code
您可以使用以下代码使所有元素不可选择
canvas.deactivateAll();
canvas.renderAll();
canvas.forEachObject(function(object){
object.selectable = false;
});
回答by Emerica
I was looking for an unmovableand uneditableFabric Text and I finally found a solution combining several SO, hope I can save someone some time.
我一直在寻找一个不可移动和不可编辑的Fabric Text,我终于找到了一个结合了几个 SO 的解决方案,希望我可以节省一些时间。
Using "selectable": falsewasn't enough in my case : the text was still editable and the cursor was still the "movable cursor" (even if the object wasn't selectable).
在我的情况下,使用"selectable": false是不够的:文本仍然可以编辑并且光标仍然是“可移动光标”(即使对象不可选择)。
I had to add "evented": false. Here is an example:
我不得不添加"evented": false。下面是一个例子:
this.canvas.add(new fabric.Text("Hello world !", {
"selectable": false,
"evented": false
}));
You can play with different control options here : http://fabricjs.com/controls-customization
您可以在此处使用不同的控制选项:http: //fabricjs.com/controls-customization
回答by rafi
There is a way like this in option -
在选项中有这样的方法 -
selectable: false
or
object.set({selectable:false})
or
object.selectable = false;