如何在 FabricJS 中编辑模式下设置文本对象的边框颜色?

fabricjshtml5 canvasjavascript

在本教程中,我们将学习如何使用 FabricJS 在编辑模式下设置文本对象的边框颜色。我们可以通过向文本框对象添加填充颜色、消除其边框甚至更改其尺寸来自定义文本框对象。同样,可以指示文本是否可编辑。我们还可以使用名为 editingBorderColor 的属性在编辑模式下更改文本对象的边框颜色。

语法

new fabric.Textbox(text: String, { editingBorderColor: String }: Object)

参数

  • text − 此参数接受一个 String,这是我们想要在文本框内显示的文本字符串。

  • options (可选) − 此参数是一个 Object,它为我们的文本框提供额外的自定义。使用此参数可以更改与对象相关的颜色、光标、边框宽度和许多其他属性,其中 editingBorderColor 是该对象的一个​​属性。

选项键

  • editingBorderColor:此属性接受一个 String,允许我们在编辑模式下控制文本对象的边框颜色。editingBorderColor 属性的默认值为 rgba(102,153,255,0.25)

示例 1

文本框对象的默认外观

让我们看一个代码示例,看看我们的文本框对象在 editingBorderColor 属性的默认值下是什么样子。在这个例子中,我们不会将任何 editingBorderColor 键传递给类,如下所示 −

<!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>Default appearance of a textbox object</h2> <p>You can double click on the textbox to enable editing mode</p> <canvas id="canvas"></canvas> <script> // 启动一个canvas实例 var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // 初始化一个 textbox 文本框对象 var textbox = new fabric.Textbox("Being positive is a sign of intelligence.", { left: 110, top: 45, fill: "black", stroke: "green", width: 400, backgroundColor: "#ffffe7", }); // 将其添加到画布 canvas.add(textbox); </script> </body> </html>

示例 2

editingBorderColor 属性作为键传递

在此示例中,我们将看到如何为 editingBorderColor 属性分配值以更改编辑模式下文本对象边框的颜色。这里我们使用颜色"红色"来演示这一点。

<!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>Passing the editingBorderColor property as key</h2> <p>You can double click on the text object to see that in editing mode the colour of the border is red</p> <canvas id="canvas"></canvas> <script> // 启动一个canvas实例 var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // 初始化一个 textbox 文本框对象 var textbox = new fabric.Textbox("Being positive is a sign of intelligence.", { left: 110, top: 45, fill: "black", stroke: "green", width: 400, backgroundColor: "#ffffe7", editingBorderColor: "red", }); // 将其添加到画布 canvas.add(textbox); </script> </body> </html>

相关文章