如何使用FabricJS鎖定橢圓的垂直傾斜?


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

語法

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

引數

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

選項鍵

  • lockSkewingY − 此屬性接受一個布林值。如果我們將其賦值為“true”,則物件的垂直傾斜將被鎖定。

示例1

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

讓我們來看一個示例,瞭解在不使用`lockSkewingY`屬性時橢圓物件的預設行為。透過按住**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>How to lock the vertical skewing of Ellipse using FabricJS?</h2>
      <p>Select the object and stretch it horizontally or vertically by pressing the <b>shift</b> key. The object will get skewed. This is the default behavior. Here we have not applied the <b>lockSkewing</b> property. </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

將`lockSkewingY`作為值為“true”的鍵傳遞

在此示例中,我們將看到如何使用`lockSkewingY`屬性來阻止橢圓物件垂直傾斜。儘管我們可以水平傾斜橢圓物件,但我們不允許垂直執行相同的操作。

<!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 vertical skewing of Ellipse using FabricJS?</h2>
      <p>Select the object and stretch it horizontally by pressing the <b>shift</b> key. The object will get skewed. But we have locked its vertical skewing by applying the <b>lockSkewingY</b> property. </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,
            lockSkewingY: true,
         });

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

更新於:2022年5月24日

117 次檢視

開啟您的職業生涯

完成課程獲得認證

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