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


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

语法

new fabric.Circle({ 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>Setting the height of a Circle using FabricJS</h2>
      <p>Note that the dimensions of the circle are controlled by its radius. Select the object and observe the height of its controlling borders. Here we have not used the <b>height</b> property and this is the default appearnace. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 215,
            top: 100,
            fill: "white",
            radius: 50,
            stroke: "#c154c1",
            strokeWidth: 5,
         });

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

示例 2

 传递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>Setting the height of a Circle using FabricJS</h2>
      <p>Select the object and notice the height of its controlling borders. Here we have set the <b>height</b> to 150px. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 215,
            top: 100,
            fill: "white",
            radius: 50,
            stroke: "#c154c1",
            strokeWidth: 5,
            height: 150
         });

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

更新于: 2022-05-25

305 次浏览

启动你的职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.