微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 FabricJS 创建一个带有背景颜色的圆形?

如何使用 FabricJS 创建一个带有背景颜色的圆形?

在本教程中,我们将使用 FabricJs 创建一个具有背景色的圆形。圆形是 FabricJS 提供的各种形状之一。为了创建一个圆圈,我们必须创建一个 Fabric.Circle 类的实例并将其添加到画布中。 backgroundColor 属性允许我们为对象的背景指定颜色。它是方形容器(圆圈所在的位置)的颜色。

语法

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

参数

  • 选项(可选) - 此参数是一个对象 为我们的对象提供额外的定制。使用此参数,可以更改与 Circle 相关的属性,例如颜色、光标、描边宽度和许多其他属性,其中 backgroundColor 是其属性

  • ul>

    选项键

    • backgroundColor - 此属性接受String 决定对象背景的颜色。该值可以是十六进制值、rgba 值或只是我们希望背景颜色的颜色名称

    示例 1

    backgroundColor 属性作为具有十六进制值的键传递

    让我们看一个使用十六进制颜色值向圆形对象分配背景颜色的示例。在此示例中,我们使用了十六进制颜色代码 #d0db61,它是深卡其色。

<!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 a background colour using FabricJS</h2>
      <p>Notice the dark-khaki background around the circle. It appears as we have applied the <b>backgroundColor</b> property and assigned it a Hex color code. </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: "#74c365",
            stroke: "#00b7eb",
            strokeWidth: 2,
            backgroundColor: "#d0db61",
         });

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

示例 2

backgroundColor 属性作为带有 rgba 值的键传递

我们可以使用 RGBA (红色、蓝色、绿色和 alpha)值而不是十六进制颜色代码。 alpha 参数指定颜色的不透明度。在此示例中,我们使用了 rgba 值 (255,0,0,0.7),它是不透明度为 0.7 的红色。

<!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 a background colour using FabricJS</h2>
      <p>Notice the orange-red background around the circle. Here we have used the <b>backgroundColor</b> property and assigned it an 'rgba' 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: "green",
            stroke: "blue",
            strokeWidth: 2,
            backgroundColor: "rgba(255,0,0,0.7)",
         });

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

以上就是如何使用 FabricJS 创建一个带有背景颜色的圆形?的详细内容,更多请关注编程之家其它相关文章

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐