如何使用 FabricJS 通过在画布中拖动来禁用对象选择?
在本文中,我们将说明如何在 FabricJS 中通过拖动来禁用对象选择。在 FabricJS 画布中,我们基本上可以单击任意位置并选择一个区域,该区域中的任何对象都会被选中。在本文中,我们将了解如何禁止这种行为
语法
new fabric.Canvas(element: HTMLElement|String, {selection: boolean}: Object)
参数
-
元素 – 此参数是 元素本身,可以使用 document.getElementById() 或 元素本身的 id 派生。 FabricJS 画布将在此元素上初始化
-
选项(可选) – 此参数是一个对象,提供额外的对我们的画布进行定制。使用此参数,可以更改与画布相关的颜色、光标、边框宽度和许多其他属性等属性。选择参数指示是否应启用选择。该键的默认值为 True。
示例 1
让我们首先看看通过拖动进行选择的效果如何就像启用它时一样。在此示例中,我们将选择键传递为 True,这也是默认值。让我们看看启用选择后画布的行为如何。
<!DOCTYPE html>
<html>
<head>
<!-- Adding the Fabric JS Library-->
<script src=https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js></script>
</head>
<body>
<h2>Disabling the selection of objects on a canvas</h2>
<p>Here you can select the object as the selection key is True</p>
<canvas id=canvas></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas(canvas, {
selection: true
});
// Creating an instance of the fabric.Circle class
var cir = new fabric.Circle({
radius: 40,
fill: #87a96b,
left: 30,
top: 20,
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
示例 2
选择键指定是否启用或禁用通过拖动选择画布中的对象。如果我们将该键设置为 False,那么我们将无法再通过拖动来选择对象。
<!DOCTYPE html>
<html>
<head>
<!-- Adding the Fabric JS Library-->
<script src=https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js></script>
</head>
<body>
<h2>Disabling the selection of objects on a canvas</h2>
<p> Here you cannot select an area around the object as the selection key is set to False.</p>
<canvas id=canvas></canvas>
<script>
//Initiate a canvas instance
var canvas = new fabric.Canvas(canvas, {
selection: false
});
//creating an instance of the fabric.Circle class
var cir = new fabric.Circle({
radius: 40,
fill: #87a96b,
left: 30,
top: 20,
});
//adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
现在我们已将选择设置为 False,我们无法再选择对象周围的部分来拖动它。不过,我们仍然可以手动单击并选择对象。
以上就是如何使用 FabricJS 通过在画布中拖动来禁用对象选择?的详细内容,更多请关注双恒网络其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。



