如何使用 FabricJS 設定橢圓控制角的顏色?


在本教程中,我們將學習如何使用 FabricJS 設定橢圓控制角的顏色。cornerColor 屬性允許我們在物件處於活動狀態時操作控制角的顏色。

語法

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

引數

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

選項鍵

  • cornerColor − 此屬性接受一個字串,允許我們在物件被選中時為控制角分配顏色。

示例 1

cornerColor 作為鍵,顏色名稱作為值傳遞

以下示例演示瞭如何使用 cornerColor 屬性更改顏色。在本例中,我們為鍵分配了值“black”,從而使控制角顯示為黑色。

<!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 color of controlling corners of Ellipse using FabricJS?</h2>
      <p>Select the object and observe the color of the controlling corners. Here we have used the <b>cornerColor</b> property to set the controlling corners black. </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: "black",
         });

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

示例 2

cornerColor 屬性分配 RGBA 值

除了將簡單的顏色名稱作為字串值傳遞給鍵之外,我們還可以分配 RGBA 值。RGBA 代表紅色、綠色、藍色和 alpha,其中 alpha 是不透明度。讓我們來看一個程式碼示例,說明我們如何做到這一點:

<!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 color of controlling corners of Ellipse using FabricJS?</h2>
      <p>Select the object and observe the color of the controlling corners. Here we have used the <b>cornerColor</b> property and passed an RGB value. </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>

更新於: 2022-05-25

284 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告