如何使用 FabricJS 隱藏橢圓的控制角?


在本教程中,我們將學習如何使用 FabricJS 隱藏橢圓的控制角。橢圓是 FabricJS 提供的各種形狀之一。為了建立橢圓,我們必須建立一個fabric.Ellipse類的例項並將其新增到畫布中。物件的控制角允許我們增加或減少其比例、拉伸或更改其位置。我們可以透過多種方式自定義控制角,例如為其新增特定顏色、更改其大小等。但是,我們也可以使用hasControls屬性來隱藏它們。

語法

new fabric.Ellipse({ hasControls: Boolean }: Object)

引數

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

選項鍵

  • hasControls − 此屬性接受一個布林值,允許我們顯示或隱藏活動選擇的物件的控制角。其預設值為True

示例 1

控制角的預設外觀

讓我們來看一個示例,它顯示了控制角的預設外觀。由於hasControls屬性的預設值為“true”,因此不會隱藏控制角。

<!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 hide the controlling corners of an Ellipse using FabricJS?</h2>
      <p>Select the object to see its controlling corners.</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: 100,
            top: 100,
            fill: "white",
            rx: 100,
            ry: 60,
            stroke: "#c154c1",
            strokeWidth: 5,
         });

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

示例 2

hasControls作為鍵併為其賦值“false”

在這個例子中,我們將看到如何使用hasControls屬性隱藏控制角。我們需要將hasControls鍵賦值為“false”。這樣做,控制角將被隱藏。

<!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 hide the controlling corners of an Ellipse using FabricJS?</h2>
      <p>Select the object and here you won't be able to see the controlling corners as we have set the <b>hasControls</b> property to False. </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: 100,
            top: 100,
            fill: "white",
            rx: 100,
            ry: 60,
            stroke: "#c154c1",
            strokeWidth: 5,
            hasControls: false,
         });

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

更新於:2022年5月24日

104 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.