如何使用 FabricJS 设置文本框选中区域的背景颜色?
在本教程中,我们将学习如何使用 FabricJS 设置文本框选中区域的背景颜色。我们可以自定义、拉伸或移动文本框中的文字。为了创建一个文本框,我们必须创建一个 fabric.Textbox 类的实例并将其添加到画布中。当对象被选中时,我们可以更改其尺寸、旋转或操作它。我们可以使用 selectionBackgroundColor 属性更改文本框选中区域的背景颜色。
语法
new fabric.Textbox(text: String, { selectionBackgroundColor : String}: Object)
参数
text − 此参数接受一个字符串,即我们希望在文本框中显示的文本字符串。
options (可选) − 此参数是一个对象,它为我们的文本框提供额外的自定义选项。使用此参数,可以更改许多与对象相关的属性,其中 selectionBackgroundColor 就是一个属性,例如颜色、光标、笔划宽度等等。
选项键
selectionBackgroundColor − 此属性接受一个字符串值。分配的值将决定选中区域的背景颜色。
示例 1
不使用selectionBackgroundColor 属性时的默认颜色
让我们来看一个代码示例,了解当不使用selectionBackgroundColor 属性时选择区域的外观。从这个例子中我们可以看到,选择区域或对象后面的区域没有颜色。
<!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 colour when selectionBackgroundColor property is not used</h2> <p>You can select the textbox to see that the selection area has no colour</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiate a textbox object var textbox = new fabric.Textbox("Everyone smiles in the same language.", { width: 400, left: 110, top: 70, fill: "violet", strokeWidth: 2, stroke: "blue", textAlign: "center", }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
示例 2
将selectionBackgroundColor 属性作为键传递
在这个例子中,我们为selectionBackgroundColor 属性赋值。在这里,我们传递了十六进制值“#e0ffff”,这是一种浅青色,因此选择区域显示为这种颜色。
<!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 selectionBackgroundColor property as key</h2> <p>You can select the textbox to see that the selection area now has a light cyan colour</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiate a textbox object var textbox = new fabric.Textbox("Everyone smiles in the same language.", { width: 400, left: 110, top: 70, fill: "violet", strokeWidth: 2, stroke: "blue", textAlign: "center", selectionBackgroundColor: "#e0ffff", }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP