如何使用 FabricJS 隱藏圓形的控制邊界?


在本教程中,我們將學習如何使用 FabricJS 隱藏圓形的控制邊界。圓形是 FabricJS 提供的各種形狀之一。為了建立一個圓形,我們將建立一個`fabric.Circle`類的例項並將其新增到畫布中。我們可以透過多種方式自定義控制邊界,例如為其新增特定顏色、虛線圖案等。但是,我們也可以使用`hasBorders`屬性完全消除邊界。

語法

new fabric.Circle({ hasBorders: Boolean }: Object)

引數

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

選項鍵

  • hasBorders − 此屬性接受一個布林值,當設定為false時,將不會渲染控制邊界。預設值為true

示例 1

圓形物件的控制邊界的預設外觀

讓我們來看一段程式碼,該程式碼顯示了圓形控制邊界的預設外觀。由於`hasBorders`屬性的預設值為**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>Hiding the controlling borders of a circle using FabricJS</h2>
      <p>Select the object and notice its controlling borders. This is the default appearance. Although we have not used the <b>hasBorders</b> property, it is by default set to True.</p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 215,
            top: 100,
            fill: "white",
            radius: 50,
            stroke: "#c154c1",
            strokeWidth: 5
         });

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

示例 2

將hasBorders作為鍵併為其賦值“false”

如果將`hasBorders`屬性賦值為**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>Hiding the controlling borders of a circle using FabricJS</h2>
      <p>Select the object and now you will no longer be able to see its controlling borders, as we have set <b>hasBorders</b> as False.</p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 215,
            top: 100,
            fill: "white",
            radius: 50,
            stroke: "#c154c1",
            strokeWidth: 5,
            hasBorders: false
         });

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

更新於:2022年5月25日

120 次瀏覽

啟動你的職業生涯

完成課程獲得認證

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