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


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

語法

new fabric.Ellipse({ 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 an Ellipse using FabricJS</h2>
      <p>Select the ellipse and notice its controlling corners. This is the default size of the controlling corners. Here we have not used the <b>cornerSize</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: 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>

示例 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>How to set the size of the controlling corners of Ellipse using FabricJS</h2>
      <p>Select the ellipse and notice the size of its controlling corners. We have set <b>cornerSize</b> at 7. </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)",
            cornerSize: 7,
         });

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

更新於: 2022年5月25日

108 次檢視

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告