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


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

語法

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

引數

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

選項鍵

  • skewX - 此屬性接受一個 Number,用於確定物件 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 the X-axis of an Ellipse using FabricJS</h2>
      <p>Notice that by default the object is not skewed. Here we have not applied the <b>skewX</b> property. However you can elect the object and stretch it horizontally or vertically by pressing the <b>shift</b> key. The object will get skewed.</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,
            rx: 80,
            ry: 50,
            fill: "#ff1493",
         });

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

示例 2

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

在本例中,我們將瞭解如何為 skewX 屬性分配數值。傳遞的值將確定沿 X 軸的傾斜。

<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 set the angle of skew on x-axis of Ellipse using FabricJS?</h2>
      <p>Notice that the ellipse is skewed on the X-axis by 50 degrees in the clockwise direction because here we have applied the <b>skewX</b> property and given it a value of 50.</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,
            rx: 80,
            ry: 50,
            fill: "#ff1493",
            skewX: 50,
         });

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

更新於: 2022年5月25日

104 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.