如何使用 FabricJS 設定橢圓控制角的樣式?


在本教程中,我們將學習如何使用 FabricJS 設定橢圓控制角的樣式。橢圓是 FabricJS 提供的各種形狀之一。為了建立橢圓,我們將建立一個 fabric.Ellipse 類的例項並將其新增到畫布上。物件的控制角允許我們縮放、拉伸或更改其位置。我們可以透過多種方式自定義我們的控制角,例如為其新增特定的顏色、更改其大小等。我們可以使用 cornerStyle 屬性更改樣式。

語法

new fabric.Ellipse({ cornerStyle: String }: Object)

引數

  • options (可選) − 此引數是一個 物件,它為我們的橢圓提供了額外的自定義選項。使用此引數,可以更改與物件相關的顏色、游標、描邊寬度和許多其他屬性,其中 cornerStyle 是一個屬性。

選項鍵

  • cornerStyle − 此屬性接受一個 字串,它允許我們指定所需的控制角樣式。

示例 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>Setting the style of controlling corners of an Ellipse using FabricJS</h2>
      <p>Select the object and observe its controlling corners. This is the default appearance as we have not used the <b>cornerStyle</b> property. </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: "#daa520",
            cornerColor: "rgb(255,20,147)",
         });

         // Adding it to the canvas
         canvas.add(ellipse);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

示例 2

cornerStyle 作為鍵,值為 "circle"

我們可以透過將值指定為 "circle" 或 "rect" 來指定活動選擇物件的控制角的樣式或外觀。將值指定為 "circle" 將使控制角顯示為圓形,如下面的示例所示:

<!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>Setting the style of controlling corners of Ellipse using FabricJS</h2>
      <p>Select the object and observe the shape of its controlling corners. We have set the <b>cornerStyle</b> as circle.</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: "#daa520",
            cornerColor: "rgb(255,20,147)",
            cornerStyle: "circle",
         });

         // Adding it to the canvas
         canvas.add(ellipse);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

更新於: 2022年5月25日

148 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.