在本文中,我们将说明如何在 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 通过在画布中拖动来禁用对象选择?的详细内容。