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


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

語法

new fabric.Ellipse({ 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 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 屬性停止橢圓物件水平傾斜的能力。雖然我們可以垂直傾斜橢圓物件,但我們不允許在水平方向上執行相同的操作。

<!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>

更新於: 2022-05-24

132 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.