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


在本教程中,我們將學習如何使用 FabricJS 在縮放圓形時鎖定翻轉。就像我們可以在畫布上指定圓形物件的

語法

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

引數

  • options (可選) − 此引數是一個 物件,它為我們的圓形提供了額外的自定義。使用此引數,可以更改與

選項鍵

  • 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 circle using FabricJS</h2>
      <p>Select the circle and try to scale it up or down. You will notice that the circle flips while scaling. This is the default behavior. Here we haven't used the <b>lockScalingFlip</b> property but by default it is set to False.</p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 115,
            top: 50,
            fill: "white",
            radius: 50,
            stroke: "black",
            strokeWidth: 5,
         });

         // Adding it to the canvas
         canvas.add(circle);
         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 circle using FabricJS</h2>
      <p>Select the object and try to scale it up or down. Observe that the circle will no longer flip during scaling, as we have set <b>lockScalingFlip</b> to True. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 115,
            top: 50,
            fill: "white",
            radius: 50,
            stroke: "black",
            strokeWidth: 5,
            lockScalingFlip: true
         });

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

更新於: 2022年5月25日

101 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告