
在本文中,我们将说明如何在 FabricJS 中通过拖动来禁用对象选择。在 FabricJS 画布中,我们基本上可以单击任意位置并选择一个区域,该区域中的任何对象都会被选中。在本文中,我们将了解如何禁止这种行为
语法
new fabric.Canvas(element: HTMLElement|String, {selection: boolean}: Object)参数
元素 - 此参数是
选项(可选) - 此参数是一个对象,提供额外的对我们的画布进行定制。使用此参数,可以更改与画布相关的颜色、光标、边框宽度和许多其他属性等属性。选择参数指示是否应启用选择。该键的默认值为 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,我们无法再选择对象周围的部分来拖动它。不过,我们仍然可以手动单击并选择对象。










