如何使用 FabricJS 設定橢圓選擇區域的背景顏色?


在本教程中,我們將學習如何使用 FabricJS 設定橢圓選擇區域的背景顏色。橢圓是 FabricJS 提供的各種形狀之一。要建立橢圓,我們必須建立fabric.Ellipse類的例項並將其新增到畫布中。當物件被選中時,我們可以更改其尺寸、旋轉或操作它。我們可以使用selectionBackgroundColor屬性更改橢圓選擇區域的背景顏色。

語法

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

引數

  • options (可選) − 此引數是一個物件,它為我們的橢圓提供額外的自定義功能。使用此引數,可以更改與物件相關的許多屬性,其中selectionBackgroundColor就是一個屬性,例如顏色、游標、筆劃寬度等。

選項鍵

  • selectionBackgroundColor − 此屬性接受一個字串,用於確定選擇區域的背景顏色。

示例 1

未使用selectionBackgroundColor屬性時的預設顏色

讓我們來看一個示例,瞭解在未使用selectionBackgroundColor屬性時選擇區域的外觀。從這個示例中我們可以看到,物件後面的選擇區域沒有顏色。

<!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 background color of selection of Ellipse using FabricJS?</h2>
      <p>Select the object and you will observe that the selection background has no color. Here we have not applied the <b>selectionBackgroundColor</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: 115,
            top: 50,
            rx: 80,
            ry: 50,
            fill: "#ff1493",
         });

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

示例 2

selectionBackgroundColor屬性作為鍵傳遞

在這個例子中,我們為selectionBackgroundColor屬性賦值。在這裡,我們傳遞了“darkBlue”顏色,因此選擇區域顯示為深藍色。

<!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 background color of selection of Ellipse using FabricJS?</h2>
      <p>Select the object and you will observe that the background of the selection appears dark blue. This is because we have set the <b>selectionBackgroundColor</b> as dark blue. </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: 115,
            top: 50,
            rx: 80,
            ry: 50,
            fill: "#ff1493",
            selectionBackgroundColor: "darkBlue",
         });

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

更新於:2022年5月25日

209 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告