如何使用 FabricJS 設定圓形在 X 軸上的傾斜角度?


在本教程中,我們將學習如何使用 FabricJS 設定圓形在 X 軸上的傾斜角度。圓形是 FabricJS 提供的各種形狀之一。為了建立圓形,我們將建立 fabric.Circle 類的例項並將其新增到畫布上。我們的圓形物件可以透過多種方式進行自定義,例如更改其尺寸、新增背景顏色或更改 X 軸上的傾斜角度。我們可以透過使用 skewX 屬性來實現這一點。

語法

new fabric.Circle({ skewX : Number }: Object)

引數

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

選項鍵

  • skewX - 此屬性接受一個數字,用於確定物件在 X 軸上的傾斜角度。

示例 1

當未應用 skewX 屬性時

讓我們來看一個例子,瞭解當未應用 skewX 屬性時我們的圓形物件是如何顯示的。在這種情況下,我們的圓形物件不會受到任何角度的扭曲。

<!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>Setting the angle of skew on X-axis of circle using FabricJS</h2>
      <p>Here we have not applied the <b>skewX</b> property, hence there is no distortion in the object. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 50,
            top: 90,
            radius: 50,
            fill: "#ccccff",
            stroke: "#7b68ee",
            strokeWidth: 5
         });
         canvas.add(circle);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

示例 2

將 skewX 作為鍵併為其分配自定義值。

在這個例子中,我們將看到如何為 skewX 屬性分配一個數值。傳遞的值將決定沿 X 軸的扭曲程度。由於我們將 skewX 屬性的值設定為 30,因此效果就像圓形物件在水平方向上沿角被擠壓,形成一個 30 度角。

<!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>Setting the angle of skew on X-axis of circle using FabricJS</h2>
      <p>Observe that the object is skewed on the X-axis in the clockwise direction at an angle of 30 degrees, as we have set <b>skewX</b> at 30. </p>
      <canvas id="canvas"></canvas>
     
      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 50,
            top: 90,
            radius: 50,
            fill: "#ccccff",
            stroke: "#7b68ee",
            strokeWidth: 5,
            skewX: 30
         });
         canvas.add(circle);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

更新於: 2022年5月25日

174 次檢視

開啟您的 職業生涯

完成課程並獲得認證

開始學習
廣告

© . All rights reserved.