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


在本教程中,我們將學習如何使用 FabricJS 鎖定圓形的水平傾斜。就像我們可以在畫布上指定圓形物件的的位置、顏色、不透明度和尺寸一樣,我們還可以指定是否希望停止物件的水平傾斜。這可以透過使用 lockSkewingX 屬性來實現。

語法

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

引數

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

選項鍵

  • lockSkewingX - 此屬性接受一個布林值。如果我們為其分配“true”值,則物件的水平傾斜將被鎖定。

示例 1

畫布中圓形物件的預設行為

讓我們看一段程式碼來了解當不使用 lockSkewingX 屬性時圓形物件的預設行為。透過按住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 circle using FabricJS</h2>
      <p>Select the object, hold the <b>shift</b> key, and stretch the object horizontally or vertically. You will notice that the object gets skewed freely in either direction. This is the default behavior. Here we have not applied the <b>lockSkewingX</b> property, but by default, it is set to False. </p>
      <canvas id="canvas"></canvas>

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

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

示例 2

將 lockSkewingX 作為鍵傳遞,值為“true”

在此示例中,我們將看到如何使用 lockSkewingX 屬性停止圓形物件水平傾斜的能力。如我們所見,儘管我們可以透過按住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 a circle using FabricJS</h2>
      <p>Select the object, hold the <b>shift</b> key, and try to stretch the object horizontally. You will notice that horizontal skewing is not allowed, as we have set <b>lockSkewingX</b> to True. You can however skew the object in the vertical direction.</p>
      <canvas id="canvas"></canvas>

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

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

更新於: 2022年5月25日

127 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.