如何使用 FabricJS 設定圓形控制角的大小?


在本教程中,我們將學習如何使用 FabricJS 設定圓形控制角的大小。物件的控制角允許我們縮放、拉伸或更改其位置。我們可以透過多種方式自定義控制角,例如為其新增特定的顏色、更改其大小等。我們可以使用cornerSize 屬性更改大小。

語法

new fabric.Circle({ cornerSize: Number }: Object)

引數

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

選項鍵

  • cornerSize − 此屬性接受一個數字,允許我們操作所選物件的控制角的大小。其預設值為 13。

示例 1

控制角的預設大小

讓我們來看一段程式碼,該程式碼描述了圓形物件在被主動選中時控制角的預設大小。

<!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 size of the controlling corners of a circle using FabricJS</h2>
      <p>Select the object and notice the size of its controlling corners. This is the default appearance. Here we haven't used the <b>cornerSize</b> property.</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>

示例 2

cornerSize 作為鍵傳遞自定義值

在此示例中,我們將cornerSize 屬性作為鍵傳遞,其值為 7。我們可以看到當圓形物件被選中時,這如何更改控制角的大小。

<!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 size of the controlling corners of a circle using FabricJS</h2>
      <p>Select the object and notice the size of its controlling corners. Here we have set the <b>cornerSize</b> at 7. </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)",
            cornerSize: 7
         });

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

更新於: 2022年5月27日

451 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.