如何使用 FabricJS 创建带边框颜色的圆形?


在本教程中,我们将使用 FabricJS 创建一个带有**边框颜色**的圆形。圆形是 FabricJS 提供的各种形状之一。为了创建圆形,我们将创建一个fabric.Circle类的实例并将其添加到画布上。由于 FabricJS 非常灵活,因此我们可以根据需要自定义圆形对象。FabricJS 提供的属性之一是borderColor,它允许我们在对象处于活动状态时操作边框的颜色。

语法

new fabric.Circle({ borderColor: String }: Object)

参数

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

选项键

  • borderColor − 此属性接受一个字符串,用于确定对象被选中时边框的颜色。其默认值为rgb(178,204,255)

示例 1

使用字符串值传递 borderColor 键

让我们看看如何为borderColor属性赋值的代码示例。我们将值“blue”分配给borderColor键,这有助于在选中圆形对象时创建蓝色边框。

<!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>Creating a circle with border colour using FabricJS</h2>
      <p>Select the object and observe the blue outline of the selection area. Here we have applied the <b>borderColor</b> property and assigned it 'blue' color. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var cir = new fabric.Circle({
            left: 215,
            top: 100,
            radius: 50,
            fill: "red",
            borderColor: "blue",
         });

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

示例 2

向 borderColor 键传递 rgba 值

除了传递简单的颜色名称作为字符串之外,我们还可以使用RGBA值,其组件指定红色、蓝色、绿色和 Alpha 的数量,其中 alpha 表示不透明度。在本例中,我们使用了rgb(128,0,128),它是紫色颜色的 rgb 值。

<!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>Creating a circle with border colour using FabricJS</h2>
      <p>Select the object and notice the purple outline of the selection area. Here we have applied the <b>borderColor</b> property and assigned it an 'rgb' value. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var cir = new fabric.Circle({
            left: 215,
            top: 100,
            radius: 50,
            fill: "red",
            borderColor: "rgb(128,0,128)",
         });

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

更新于: 2022年5月25日

234 次浏览

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.