如何使用 FabricJS 锁定 Ellipse 的水平倾斜?

PHPz
PHPz 转载
2023-09-14 12:49:02 250浏览

如何使用 FabricJS 锁定 Ellipse 的水平倾斜?

在本教程中,我们将学习如何使用 FabricJS 锁定椭圆的水平倾斜。正如我们可以指定画布中椭圆对象的位置、颜色、不透明度和尺寸一样,我们也可以指定是否要停止水平倾斜对象。这可以通过使用lockSkewingX属性来完成。

语法

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

参数

  • 选项(可选)- 此参数是一个提供额外自定义的对象到我们的椭圆。使用此参数,可以更改与 lockSkewingX 为属性的对象相关的颜色、光标、描边宽度和许多其他属性。

选项键

  • lockSkewingX - 此属性接受布尔值强>值。如果我们为其指定“true”值,则对象的水平倾斜将被锁定。

示例 1

默认行为画布中的 Ellipse 对象

让我们通过一个示例来了解未使用 lockSkewingX 属性时 Ellipse 对象的默认行为。通过按住 Shift 键,然后沿水平或垂直方向拖动,可以使对象在水平和垂直方向上倾斜。

<!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>Locking the horizontal skewing of Ellipse using FabricJS</h2>
      <p>Select the object. Hold the "shift" and try to stretch the object (not diagonally). You can skew the object both horizontally and vertically. 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

lockSkewingX 作为具有“true”值的键传递

在此示例中,我们将了解如何可以使用lockSkewingX属性来停止Ellipse对象水平倾斜的能力。虽然我们可以垂直倾斜椭圆对象,但我们不允许水平执行相同的操作。

<!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 horizontal skewing of Ellipse using FabricJS?</h2>
      <p>Select the object; hold the "shift" key and try to stretch the object horizontally. You cannot skew the object horizontally because we have set <b>lockSkewingX</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,
            lockSkewingX: true,
         });
         // Adding it to the canvas
         canvas.add(ellipse);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

以上就是如何使用 FabricJS 锁定 Ellipse 的水平倾斜?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除