在本文中,我们将学习如何在 FabricJS 中禁用画布的交互性。每个对象之上的交互层是 FabricJS 的独特功能之一。一旦我们初始化了画布,就可以选择对象、拖动它们或操作组选择。但是,所有这一切都可以通过将交互属性指定为 False 来撤消。
new fabric.Canvas(element: HTMLElement|String, { interactive : Boolean }: Object)
元素 - 此参数是
选项(可选) - 此参数是一个对象,它提供对我们的画布进行额外的定制。使用此参数,可以更改与画布相关的颜色、光标、边框宽度等属性,以及许多其他属性,其中 Interactive 是我们可以决定是否需要交互式画布的属性。该属性的默认值为 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 interactivity of canvas</h2> <p>Here you can drag the object and manipulate them freely as we have set the <b>interactive</b> property to True. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas", { interactive: true, }); // Creating an instance of the fabric.Rect class var obj1 = new fabric.Rect({ left: 170, top: 90, width: 60, height: 80, fill: "#966fd6", angle: 90, }); var obj2 = new fabric.Rect({ left: 200, top: 120, width: 60, height: 80, fill: "#ffa343", angle: 56, }); // Adding it to the canvas canvas.add(obj1); canvas.add(obj2); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
将交互键传递给类
让我们看一个代码示例,了解如何禁用画布的交互性。我们可以为交互属性分配一个 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 interactivity of canvas</h2> <p>Here you cannot select an area around the objects and manipulate them freely, as we have set the <b>interactive</b> property as False. </b> </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas", { interactive: false, }); // Creating an instance of the fabric.Rect class var obj1 = new fabric.Rect({ left: 170, top: 90, width: 60, height: 80, fill: "#966fd6", angle: 90, }); var obj2 = new fabric.Rect({ left: 200, top: 120, width: 60, height: 80, fill: "#ffa343", angle: 56, }); // Adding it to the canvas canvas.add(obj1); canvas.add(obj2); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
以上是如何使用 FabricJS 禁用画布的交互性?的详细内容。更多信息请关注PHP中文网其他相关文章!