如何使用 FabricJS 在縮放橢圓時鎖定翻轉?


在本教程中,我們將學習如何使用 FabricJS 在縮放橢圓時鎖定翻轉。就像我們可以指定畫布中橢圓物件的位 置、顏色、不透明度和尺寸一樣,我們還可以指定是否要在縮放時停止翻轉物件。這可以透過使用 `lockScalingFlip` 屬性來實現。

語法

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

引數

  • options (可選) − 此引數是一個物件,它為我們的橢圓提供額外的自定義設定。使用此引數可以更改與物件的許多屬性相關的顏色、游標、描邊寬度等,其中 `lockScalingFlip` 是一個屬性。

選項鍵

  • lockScalingFlip − 此屬性接受一個 布林值。如果我們將其賦值為“true”,則不允許物件在縮放期間翻轉。

示例 1

畫布中橢圓物件的預設行為

讓我們來看一個例子,瞭解在不使用 `lockScalingFlip` 屬性時橢圓物件的預設行為。

<!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 lock the flipping during scaling of Ellipse using FabricJS?</h2>
      <p>Select the object and scale it up. The object will flip. 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: 115,
            top: 50,
            fill: "white",
            rx: 80,
            ry: 50,
            stroke: "black",
            strokeWidth: 5,
         });

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

示例 2

將 `lockScalingFlip` 作為鍵傳遞,值為“true”

在這個例子中,我們將看到如何透過使用 `lockScalingFlip` 屬性來停止橢圓物件在縮放時的翻轉能力。正如我們所看到的,即使我們試圖翻轉橢圓物件,它也不再被允許了。

<!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 lock the flipping during scaling of Ellipse using FabricJS?</h2>
      <p>Select the object and stretch it from its corners. The object will not flip because we have set the property <b>lockScalingFlip</b> to True. </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,
            fill: "white",
            rx: 80,
            ry: 50,
            stroke: "black",
            strokeWidth: 5,
            lockScalingFlip: true,
         });

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

更新於:2022年5月24日

104 次檢視

啟動您的 職業生涯

完成課程獲得認證

開始學習
廣告