首頁 > web前端 > js教程 > 主體

如何使用 FabricJS 設定圓的旋轉角度?

王林
發布: 2023-09-14 08:57:23
轉載
1518 人瀏覽過

如何使用 FabricJS 设置圆的旋转角度?

在本教學中,我們將使用 FabricJS 設定圓的旋轉角度。圓形是 FabricJS 提供的各種形狀之一。為了創建一個圓圈,我們必須創建一個 Fabric.Circle 類別的實例並將其添加到畫布中。 FabricJS 中的 angle 屬性定義了物件的 2D 旋轉角度。我們也有 centeredRotation 屬性,它允許我們使用圓的中心點作為變換的原點。

語法

new fabric.Circle({ angle: Number, centeredRotation: Boolean }: Object)
登入後複製

參數

  • #選項(可選) - 此參數是一個物件< /em> 為我們的物件提供額外的客製化。使用此參數,可以變更與圓相關的顏色、遊標、描邊寬度等屬性,其中 anglecenteredRotation 是屬性。 p>

選項鍵

  • #」 - 這個屬性接受一個數字,它指定圓的旋轉角度(以度為單位)。

  • centeredRotation - 此屬性接受一個布林值#值,該值決定圓心是否是變換的原點。

範例1

將角度作為具有自訂值的鍵傳遞並停用圓形的居中旋轉

以下範例示範如何在FabricJS 中設定圓的旋轉角度。負角度指定逆時針方向,而正角度指定順時針方向。由於我們已為 centeredRotation 指定了 False 值,因此圓將在使用其角點作為旋轉中心的同時進行旋轉。

<!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>Set the angle of rotation of a Circle using FabricJS</h2>
      <p>Select the object and observe its controlling corners. You will notice that the angle of rotation is set at 60 degrees in the anti-clockwise direction, as we have set the <b>angle</b> at <b>-60</b>. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var cir = new fabric.Circle({
            left: 215,
            top: 100,
            radius: 50,
            fill: "green",
            stroke: "blue",
            strokeWidth: 2,
            angle: -60,
            centeredRotation: false,
         });

         // Adding it to the canvas
         canvas.add(cir);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>
登入後複製

範例2

啟用圓的居中旋轉

#從這個範例中我們可以看到,透過設定centeredRotation#屬性為True,我們的圓現在使用其中心作為旋轉中心。在版本 1.3.4 之前,centeredScalingcenteredRotation 包含在一個名為 centerTransform 的屬性中。

<!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>Set the angle of rotation of a Circle using FabricJS</h2>
      <p>Select the object and observe its controlling corners. Here we have set the angle of rotation at 60 degrees in the anti-clockwise direction, and when you rotate the circle, it will rotate around its center, as we have set <b>centeredRotation</b> to True. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var cir = new fabric.Circle({
            left: 215,
            top: 100,
            radius: 50,
            fill: "green",
            stroke: "blue",
            strokeWidth: 2,
            angle: -60,
            centeredRotation: true,
         });

         // Adding it to the canvas
         canvas.add(cir);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>
登入後複製

以上是如何使用 FabricJS 設定圓的旋轉角度?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!