首頁  >  文章  >  web前端  >  如何使用 FabricJS 禁用橢圓的居中旋轉?

如何使用 FabricJS 禁用橢圓的居中旋轉?

WBOY
WBOY轉載
2023-08-28 09:13:111084瀏覽

如何使用 FabricJS 禁用椭圆的居中旋转?

在本教程中,我們將學習如何使用 FabricJS 停用橢圓的居中旋轉。橢圓形是 FabricJS 提供的各種形狀之一。為了創建一個橢圓,我們必須建立一個 Fabric.Ellipse 類別的實例並將其新增到畫布中。預設情況下,FabricJS 中的所有物件都使用其中心作為旋轉點。但是,我們可以使用 centeredRotation 屬性來更改此行為。

語法

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

參數

  • #選項(可選)- 此參數是一個物件 為我們的橢圓提供額外的客製化。使用此參數,可以變更與 centeredRotation 屬性相關的物件的顏色、遊標、描邊寬度和許多其他屬性。

選項鍵

  • #centeredRotation - 此屬性接受布爾值 em> 值可讓我們控制物件在透過控制項旋轉時是否使用其中心點作為變換原點。它的預設值為True

範例1

FabricJS中橢圓旋轉的預設行為 strong>

讓我們來看一個描述橢圓物件預設行為的範例。由於 centeredRotation 屬性預設為“true”,因此橢圓物件使用其中心作為旋轉點。

<!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 disable the centered rotation of Ellipse using FabricJS?</h2>
      <p>Select the object and rotate it. The ellipse will by default rotate around its center. This is the default behavior.</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: 215,
            top: 100,
            fill: "white",
            rx: 90,
            ry: 50,
            stroke: "#c154c1",
            strokeWidth: 5,
            borderColor: "#daa520",
         });

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

範例2

傳遞值為「false」的centeredRotation 鍵

現在我們已經看到了預設行為,讓我們看一下程式碼來了解當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>How to disable the centered rotation of Ellipse using FabricJS?</h2>
      <p>Select the object and try to rotate it. The ellipse will not rotate around its center because we have set <b>centeredRotation</b> to False. </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: 215,
            top: 100,
            fill: "white",
            rx: 90,
            ry: 50,
            stroke: "#c154c1",
            strokeWidth: 5,
            borderColor: "#daa520",
            centeredRotation: false,
         });

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

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

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除