使用 FabricJS 設定圓形選中區域的背景顏色


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

語法

new fabric.Circle({ 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>Setting the Background Colour on selection of circle</h2>
      <p>Select the object and notice the selection area. Here we have not used the <b>selectionBackgroundColor</b> property.</p>
      <canvas id="canvas"></canvas>
   
      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 115,
            top: 50,
            radius: 50,
            fill: "#85bb65"
         });
         canvas.add(circle);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

示例 2

將 selectionBackgroundColor 屬性作為鍵傳遞

在這個例子中,我們為selectionBackgroundColor屬性賦值。“skyBlue”顏色,因此選擇區域顯示為天藍色。

<!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 Background Colour on selection of circle</h2>
      <p>Select the object to see the background color of the selection area. Here we have used the <b>selectionBackgroundColor</b> property and assigned it 'skyBlue' color. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 115,
            top: 50,
            radius: 50,
            fill: "#85bb65",
            selectionBackgroundColor: "skyBlue"
         });
         canvas.add(circle);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

更新於:2022年5月25日

259 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.