如何使用 FabricJS 设置椭圆的高度?


在本教程中,我们将学习如何使用 FabricJS 设置椭圆的高度。椭圆是 FabricJS 提供的各种形状之一。为了创建一个椭圆,我们将创建一个fabric.Ellipse类的实例并将其添加到画布中。我们可以通过更改其位置、不透明度、描边以及尺寸来操作椭圆对象。FabricJS 允许我们使用widthheight属性来控制对象的尺寸。

语法

new fabric.Ellipse({ height: Number }: Object)

参数

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

选项键

  • height − 此属性接受一个数字,允许我们指定对象的高度。分配的值决定了高度。

示例 1

height属性的默认行为

让我们来看一个例子,了解height属性的默认行为。

<!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 set the height of an Ellipse using FabricJS?</h2>
      <p>Here we have set the dimensions of the ellipse by passing the horizonal and vertical radius, <b>rx</b> and <b>ry</b>. We have not used the <b>height</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: 100,
            top: 100,
            fill: "white",
            rx: 100,
            ry: 60,
            stroke: "#c154c1",
            strokeWidth: 5,
         });

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

示例 2

height属性作为键传递

由于椭圆的尺寸由其水平和垂直半径决定,因此其大小不会有任何变化。

<!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 set the height of an Ellipse using FabricJS?</h2>
      <p>There is no change in the dimensions of the ellipse even though we have used the <b>height</b> property. This is because the dimensions are controlled by the horizontal and vertical radius, <b>rx</b> and <b>ry</b>. </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({
            top: 100,
            left: 100,
            fill: "white",
            rx: 100,
            ry: 60,
            height: 90,
            stroke: "#c154c1",
            strokeWidth: 5,
         });

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

更新于:2022年5月25日

浏览量:167

启动你的职业生涯

完成课程获得认证

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