Maison  >  Article  >  interface Web  >  Comment définir la couleur de remplissage d'une ellipse à l'aide de FabricJS ?

Comment définir la couleur de remplissage d'une ellipse à l'aide de FabricJS ?

PHPz
PHPzavant
2023-08-24 14:37:141313parcourir

Comment définir la couleur de remplissage dune ellipse à laide de FabricJS ?

在本教程中,我们将学习如何使用 FabricJS 更改 Ellipse 对象的填充颜色来更改其外观。椭圆形是 FabricJS 提供的各种形状之一。为了创建一个椭圆,我们必须创建一个 Fabric.Ellipse 类的实例并将其添加到画布中。我们可以使用 fill 属性来更改填充颜色,该属性允许我们指定对象的填充颜色。

语法

new fabric.Ellipse({ fill: String }: Object)

参数

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

选项键

  • 填充 - 此属性接受字符串 Strong> 值允许我们更改对象的填充颜色。其默认值为 rgb(0,0,0),即黑色。

示例 1

默认 填充椭圆对象的颜色

让我们看一段代码,它显示了椭圆对象的默认填充颜色FabricJS。如果我们在创建椭圆对象时完全跳过 fill 属性,它将被渲染为黑色。

<!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 fill color of Ellipse using FabricJS?</h2>
      <p>Observe the ellipse is rendered black as we have not applied the <b>fill</b> property. This is the default fill color. </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: 215,
            top: 100,
            rx: 90,
            ry: 50,
            stroke: "#c154c1",
            strokeWidth: 5,
         });

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

示例 2

将 fill 属性作为键传递

我们还可以为 fill 属性分配任何颜色名称或 RGBA 值。在此示例中,我们为其分配了值“skyBlue”,从而相应地更改了填充颜色。

<!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 fill color of Ellipse using FabricJS?</h2>
      <p>Here we have used the <b>fill</b> property and used skyBlue color.</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: 215,
            top: 100,
            fill: "skyBlue",
            rx: 90,
            ry: 50,
            stroke: "#c154c1",
            strokeWidth: 5,
         });

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

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer