在本文中,我們將學習如何在 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中文網其他相關文章!