如何使用 FabricJS 隱藏圓形的控制角?
在本教程中,我們將學習如何使用 FabricJS 隱藏圓形的控制角。圓形是 FabricJS 提供的各種形狀之一。為了建立一個圓形,我們必須建立一個 fabric.Circle 類的例項並將其新增到畫布上。物件的控制角允許我們增加或減少其比例、拉伸或更改其位置。我們可以透過多種方式自定義我們的控制角,例如為其新增特定的顏色、更改其大小等。但是,我們也可以使用 hasControls 屬性隱藏它們。
語法
new fabric.Circle({ hasControls: Boolean }: Object)
引數
options(可選) - 此引數是一個 Object,它為我們的圓形提供了額外的自定義。使用此引數,可以更改與物件的 hasControls 屬性相關的屬性,例如顏色、游標、筆觸寬度以及許多其他屬性。
選項鍵
hasControls - 此屬性接受一個 布林值,允許我們顯示或隱藏活動選擇物件的控制角。其預設值為 True。
示例 1
控制角的預設外觀
讓我們看看一段程式碼,它顯示了控制角的預設外觀。由於 hasControls 屬性的預設值為 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>Hiding the controlling corners of a circle using FabricJS</h2> <p>Select the object and observe its controlling corners. This is the default appearnce. Even though we have not applied the <b>hasControls</b> property, it is by default set to True. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); var circle = new fabric.Circle({ left: 215, top: 100, fill: "white", radius: 50, stroke: "#c154c1", strokeWidth: 5 }); // Adding it to the canvas canvas.add(circle); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
示例 2
將 hasControls 作為鍵併為其分配“false”值
在本例中,我們將看到如何使用 hasControls 屬性隱藏控制角。我們需要為 hasControls 鍵分配一個 '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>Hiding the controlling corners of a circle using FabricJS</h2> <p>Select the object and you will notice that the controlling corners are no longer there. Here we have set <b>hasControls</b> to False.</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); var circle = new fabric.Circle({ left: 215, top: 100, fill: "white", radius: 50, stroke: "#c154c1", strokeWidth: 5, hasControls: false }); // Adding it to the canvas canvas.add(circle); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
廣告