如何使用 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 次查看

启动您的 职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.