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


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

語法

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

引數

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

選項鍵

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

示例 1

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

讓我們來看一個使用`cornerColor`屬性更改顏色的示例。在本例中,我們將值為“黑色”賦給鍵,使控制角顯示為黑色。

<!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 colour of controlling corners of circle using FabricJS</h2>
      <p>Select the object and notice the color of its controlling corners. Here we have used the <b>cornerColor</b> property to set the corners black. </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: "black"
         });
         
         // Adding it to the canvas
         canvas.add(cir);
         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>Setting the colour of controlling corners of circle using FabricJS</h2>
      <p>Select the object and notice the color of its controlling corners. Here we have used the <b>cornerColor</b> and assigned it an "rgba" value to set the corners purple. </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>

更新於:2022年5月25日

瀏覽量:117

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告