首頁  >  文章  >  web前端  >  如何使用FabricJS設定橢圓選區的背景顏色?

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

PHPz
PHPz轉載
2023-09-10 13:13:021428瀏覽

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

在本教學中,我們將學習如何使用 FabricJS 設定橢圓選取範圍的背景顏色。橢圓形是 FabricJS 提供的各種形狀之一。為了創建一個橢圓,我們必須建立一個 Fabric.Ellipse 類別的實例並將其新增到畫布中。當主動選擇物件時,我們可以更改物件的尺寸、旋轉它或操縱它。我們可以使用 selectionBackgroundColor 屬性來變更橢圓選取範圍的背景顏色。

語法

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

參數

  • #選項(可選)- 此參數是一個物件 為我們的橢圓提供額外的客製化。使用此參數,可以變更與 selectionBackgroundColor 為屬性的物件相關的顏色、遊標、描邊寬度和許多其他屬性。

選項鍵

  • #selectionBackgroundColor## - 此屬性接受字符字串 em> 決定選取範圍的背景顏色。

範例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>

以上是如何使用FabricJS設定橢圓選區的背景顏色?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除