如何使用 FabricJS 设置椭圆选择区域的背景颜色?


在本教程中,我们将学习如何使用 FabricJS 设置椭圆选择区域的背景颜色。椭圆是 FabricJS 提供的各种形状之一。要创建椭圆,我们必须创建fabric.Ellipse类的实例并将其添加到画布中。当对象被选中时,我们可以更改其尺寸、旋转或操作它。我们可以使用selectionBackgroundColor属性更改椭圆选择区域的背景颜色。

语法

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

参数

  • options (可选) − 此参数是一个对象,它为我们的椭圆提供额外的自定义功能。使用此参数,可以更改与对象相关的许多属性,其中selectionBackgroundColor就是一个属性,例如颜色、光标、笔划宽度等。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

选项键

  • selectionBackgroundColor − 此属性接受一个字符串,用于确定选择区域的背景颜色。

示例 1

未使用selectionBackgroundColor属性时的默认颜色

让我们来看一个示例,了解在未使用selectionBackgroundColor属性时选择区域的外观。从这个示例中我们可以看到,对象后面的选择区域没有颜色。

Open Compiler
<!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 background color of selection of Ellipse using FabricJS?</h2> <p>Select the object and you will observe that the selection background has no color. Here we have not applied the <b>selectionBackgroundColor</b> property. </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: "#ff1493", }); // Adding it to the canvas canvas.add(ellipse); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>

示例 2

selectionBackgroundColor属性作为键传递

在这个例子中,我们为selectionBackgroundColor属性赋值。在这里,我们传递了“darkBlue”颜色,因此选择区域显示为深蓝色。

Open Compiler
<!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 background color of selection of Ellipse using FabricJS?</h2> <p>Select the object and you will observe that the background of the selection appears dark blue. This is because we have set the <b>selectionBackgroundColor</b> as dark blue. </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: "#ff1493", selectionBackgroundColor: "darkBlue", }); // Adding it to the canvas canvas.add(ellipse); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>

更新于:2022年5月25日

209 次浏览

开启您的职业生涯

完成课程获得认证

开始学习
广告