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


在本教程中,我們將學習如何使用 FabricJS 設定圓形控制角的樣式。圓形是 FabricJS 提供的各種形狀之一。為了建立一個圓形,我們將必須建立一個fabric.Circle類的例項並將其新增到畫布。

物件的控制角允許我們縮放、拉伸或改變其位置。我們可以透過多種方式自定義我們的控制角,例如向其新增特定顏色、更改其大小等。我們可以使用 cornerStyle 屬性更改樣式。

語法

new fabric.Circle({ 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 circle using FabricJS</h2>
      <p>Select the object and notice the shape and size of its controlling corners. This is the default style of the controlling corners.</p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var cir = new fabric.Circle({
            left: 215,
            top: 100,
            fill: "white",
            radius: 50,
            stroke: "#c154c1",
            strokeWidth: 5,
            borderColor: "#daa520",
            cornerColor: "rgb(255,20,147)"
         });

         // Adding it to the canvas
         canvas.add(cir);
         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 circle using FabricJS</h2>
      <p>Select the object and notice the shape of its controlling corners. Here we have used the <b>cornerStyle</b> property and assigned it the value "circle". </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var cir = new fabric.Circle({
            left: 215,
            top: 100,
            fill: "white",
            radius: 50,
            stroke: "#c154c1",
            strokeWidth: 5,
            borderColor: "#daa520",
            cornerColor: "rgb(255,20,147)",
            cornerStyle: "circle"
         });

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

更新於:2022年5月27日

497 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告