如何使用 FabricJS 设置椭圆的描边宽度?


在本教程中,我们将学习如何使用 FabricJS 设置椭圆的描边宽度。椭圆是 FabricJS 提供的各种形状之一。为了创建椭圆,我们将创建一个 fabric.Ellipse 类的实例并将其添加到画布上。strokeWidth 属性允许我们指定对象描边的宽度。

语法

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

参数

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

选项键

  • strokeWidth - 此属性接受一个 Number 值,允许我们指定对象的描边宽度。其默认值为 1。

示例 1

对象的描边的默认外观

以下示例描述了椭圆对象的描边的默认外观。由于我们没有使用 strokeWidth 属性,因此正在渲染默认宽度。

<!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 width of stroke of an Ellipse using FabricJS</h2>
      <p>Here we haven't set the <b>strokeWidth</b> explicitly. By default, it is set to 1. </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,
            rx: 80,
            ry: 50,
            fill: "#ffdead",
            stroke: "#cd853f",
         });

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

示例 2

strokeWidth 属性作为键传递

在此示例中,我们将 strokeWidth 属性的值设置为 5。这将确保我们的椭圆对象以宽度为 5 像素的描边进行渲染。

<!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 width of stroke of an Ellipse using FabricJS</h2>
      <p>Observe the stroke width of the ellipse. Here we have set the <b>strokeWidth</b> at 5 pixels.</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,
            rx: 80,
            ry: 50,
            fill: "#ffdead",
            stroke: "#cd853f",
            strokeWidth: 5,
         });

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

更新于: 2022-05-25

116 次浏览

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.