首頁  >  文章  >  web前端  >  如何使用 FabricJS 鎖定 Ellipse 的水平傾斜?

如何使用 FabricJS 鎖定 Ellipse 的水平傾斜?

PHPz
PHPz轉載
2023-09-14 12:49:02756瀏覽

如何使用 FabricJS 锁定 Ellipse 的水平倾斜?

在本教程中,我們將學習如何使用 FabricJS 鎖定橢圓的水平傾斜。正如我們可以指定畫布中橢圓物件的位置、顏色、不透明度和尺寸一樣,我們也可以指定是否要停止水平傾斜物件。這可以透過使用lockSkewingX屬性來完成。

語法

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

參數

  • #選項(可選)- 此參數是一個提供額外自訂的物件到我們的橢圓。使用此參數,可以變更與 lockSkewingX 為屬性的物件相關的顏色、遊標、描邊寬度和許多其他屬性。

選項鍵

  • #lockSkewingX - 此屬性接受布爾值强>值。如果我們為其指定“true”值,則物件的水平傾斜將被鎖定。

範例1

預設行為畫布中的Ellipse 物件

讓我們透過一個範例來了解未使用lockSkewingX 屬性時Ellipse 物件的預設行為。按住 Shift 鍵,然後沿水平或垂直方向拖曳,可以使物件在水平和垂直方向上傾斜。

<!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>Locking the horizontal skewing of Ellipse using FabricJS</h2>
      <p>Select the object. Hold the "shift" and try to stretch the object (not diagonally). You can skew the object both horizontally and vertically. This is the default behavior.</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: 115,
            top: 50,
            fill: "white",
            rx: 80,
            ry: 50,
            stroke: "black",
            strokeWidth: 5,
         });

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

範例2

lockSkewingX 作為具有「true」值的鍵傳遞

在此範例中,我們將了解如何可以使用lockSkewingX屬性來停止Ellipse物件水平傾斜的能力。雖然我們可以垂直傾斜橢圓對象,但我們不允許水平執行相同的操作。

<!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 lock the horizontal skewing of Ellipse using FabricJS?</h2>
      <p>Select the object; hold the "shift" key and try to stretch the object horizontally. You cannot skew the object horizontally because we have set <b>lockSkewingX</b> to True. </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: 115,
            top: 50,
            fill: "white",
            rx: 80,
            ry: 50,
            stroke: "black",
            strokeWidth: 5,
            lockSkewingX: true,
         });
         // Adding it to the canvas
         canvas.add(ellipse);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

以上是如何使用 FabricJS 鎖定 Ellipse 的水平傾斜?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除