如何使用FabricJS锁定椭圆的垂直倾斜?


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

语法

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

参数

  • options (可选) − 此参数是一个对象,它为我们的椭圆提供额外的自定义功能。使用此参数可以更改与`lockSkewingY`属性相关的对象的许多属性,例如颜色、光标和描边宽度。

选项键

  • lockSkewingY − 此属性接受一个布尔值。如果我们将其赋值为“true”,则对象的垂直倾斜将被锁定。

示例1

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

让我们来看一个示例,了解在不使用`lockSkewingY`属性时椭圆对象的默认行为。通过按住**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>How to lock the vertical skewing of Ellipse using FabricJS?</h2>
      <p>Select the object and stretch it horizontally or vertically by pressing the <b>shift</b> key. The object will get skewed. This is the default behavior. Here we have not applied the <b>lockSkewing</b> property. </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

将`lockSkewingY`作为值为“true”的键传递

在此示例中,我们将看到如何使用`lockSkewingY`属性来阻止椭圆对象垂直倾斜。尽管我们可以水平倾斜椭圆对象,但我们不允许垂直执行相同的操作。

<!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 vertical skewing of Ellipse using FabricJS?</h2>
      <p>Select the object and stretch it horizontally by pressing the <b>shift</b> key. The object will get skewed. But we have locked its vertical skewing by applying the <b>lockSkewingY</b> property. </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,
            lockSkewingY: true,
         });

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

更新于:2022年5月24日

117 次查看

开启您的职业生涯

完成课程获得认证

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