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


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

语法

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

参数

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

选项键

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

示例 1

画布中椭圆 对象 的默认行为

让我们举一个例子来了解当不使用 lockSkewingX 属性时椭圆对象的默认行为。通过按住 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 属性停止椭圆对象水平倾斜的能力。虽然我们可以垂直倾斜椭圆对象,但我们不允许在水平方向上执行相同的操作。

<!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>

更新于: 2022-05-24

132 次浏览

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.