Fabric.js Textbox textBackgroundColor属性
在这篇文章中,我们将看到如何使用FabricJS的textBackgroundColor属性来改变画布文本框的背景颜色。画布表示可移动、可旋转、可调整大小并可拉伸的文本框。但在这篇文章中,我们将改变背景颜色。
方法: 为了实现这个目标,我们将使用一个叫做FabricJS的JavaScript库。在使用CDN导入库之后,我们将在body标签中创建一个包含我们文本框的画布块。在此之后,我们将初始化由FabricJS提供的Canvas和Textbox实例,并使用textBackgroundColor属性来改变背景颜色,然后根据下面的示例在文本框上渲染Canvas。
语法:
fabric.Textbox('text', {
textBackgroundColor: 'string'
});
参数: 此函数接受一个参数,如上述所述,并在下面进行说明:
- textBackgroundColor: 它指定要设置的背景颜色。
示例: 此示例使用FabricJS Textbox textBackgroundColor属性将画布文本框的背景颜色设置为以下内容:
<!DOCTYPE html>
<html>
<head>
<!-- Adding the FabricJS library -->
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js">
</script>
</head>
<body>
<h1 style="color: green;">
GeeksforGeeks
</h1>
<h3>
Fabric.js | Textbox textBackgroundColor Property
</h3>
<canvas id="canvas" width="600" height="300"
style="border:1px solid #000000">
</canvas>
<script>
// Initiate a Canvas instance
var canvas = new fabric.Canvas("canvas");
// Create a new Textbox instance
var text = new fabric.Textbox(
'A Computer Science Portal', {
width: 500,
textBackgroundColor: "green"
});
// Render the Textbox in canvas
canvas.add(text);
canvas.centerObject(text);
</script>
</body>
</html>
输出: