如何使用 FabricJS 設定橢圓的縮放因子(邊框)?
在本教程中,我們將學習如何使用 FabricJS 設定橢圓的縮放因子(邊框)。橢圓是 FabricJS 提供的各種形狀之一。為了建立橢圓,我們必須建立一個 *fabric.Ellipse* 類的例項並將其新增到畫布中。我們可以使用 *borderScaleFactor* 屬性,它指定物件控制邊框的縮放因子。
語法
new fabric.Ellipse({ borderScaleFactor: Number }: Object)
引數
options (可選) − 此引數是一個物件,它為我們的橢圓提供了額外的自定義選項。使用此引數,可以更改與物件相關的許多屬性,其中 *borderScaleFactor* 就是一個屬性,例如顏色、游標、描邊寬度等。
選項鍵
borderScaleFactor − 此屬性接受一個數字,指定邊框厚度。預設值為 1。
示例 1
*borderScaleFactor* 屬性的預設行為
讓我們來看一個例子,看看 *borderScaleFactor* 屬性的預設行為。儘管我們在本例中指定了它,但即使未指定,*borderScaleFactor* 預設使用的值也是 1。
<!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>How to set the scale factor (border) of Ellipse using FabricJS?</h2> <p>Select the object and observe its controlling borders. Here we have set the <b>borderScaleFacto</b> at 1, which is the default value.</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); // Initiate an ellipse instance var ellipse = new fabric.Ellipse({ left: 215, top: 100, fill: "white", rx: 90, ry: 50, stroke: "#c154c1", strokeWidth: 5, borderColor: "#966fd6", borderScaleFactor: 1, }); // Adding it to the canvas canvas.add(ellipse); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
示例 2
將 *borderScaleFactor* 作為鍵傳遞
讓我們看一段程式碼,在橢圓物件被選中時增加邊框厚度。在本例中,我們將 *borderScaleFactor* 的值設定為 5,指定邊框的厚度。
<!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>How to set the scale factor (border) of Ellipse using FabricJS?</h2> <p>Select the object and observe its controlling borders. Here we have set the <b>borderScaleFactor</b> at 5. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); // Initiate an ellipse instance var ellipse = new fabric.Ellipse({ left: 215, top: 100, fill: "white", rx: 90, ry: 50, stroke: "#c154c1", strokeWidth: 5, borderColor: "#966fd6", borderScaleFactor: 5, }); // Adding it to the canvas canvas.add(ellipse); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
廣告