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


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

語法

new fabric.Circle({ 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>Locking the vertical skewing of a circle using FabricJS</h2>
      <p>Select the object, hold the <b>shift</b> key, and stretch it horizontally or vertically. The object will get skewed freely. This is the default behavior. Here we have not applied the <b>lockSkewingY</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

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

在本例中,我們將看到如何使用`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>Locking the vertical skewing of a circle using FabricJS</h2>
      <p>Select the object, hold the <b>shift</b> key, and try to stretch the circle horizontally or vertically. You will notice that the object gets skewed horizontally, but its vertical skewing is locked. Here we have set <b>lockSkewingY</b> to True. </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,
            lockSkewingY: true
         });
   
         // Adding it to the canvas
         canvas.add(circle);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

更新於:2022年5月25日

瀏覽量:122

開啟你的職業生涯

完成課程獲得認證

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