您好,欢迎访问一九零五行业门户网

如何使用FabricJS设置椭圆的填充颜色?

在本教程中,我们将学习如何使用 fabricjs 更改 ellipse 对象的填充颜色来更改其外观。椭圆形是 fabricjs 提供的各种形状之一。为了创建一个椭圆,我们必须创建一个 fabric.ellipse 类的实例并将其添加到画布中。我们可以使用 fill 属性来更改填充颜色,该属性允许我们指定对象的填充颜色。
语法new fabric.ellipse({ fill: string }: object)
参数选项(可选)- 此参数是一个对象 为我们的椭圆提供额外的定制。使用此参数,可以更改与 fill 为属性的对象相关的颜色、光标、描边宽度和许多其他属性。
选项键填充 - 此属性接受字符串 值允许我们更改对象的填充颜色。其默认值为 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>
以上就是如何使用fabricjs设置椭圆的填充颜色?的详细内容。
其它类似信息

推荐信息