Fabric.js Polyline selectionBackgroundColor 属性
在本文中,我们将看到如何使用 Fabric.js 改变 canvas 的 Polyline 的选定背景色。Fabric.js 中的 Polyline 可以移动并根据需求进行拉伸. 此外,在初始描边颜色、高度、宽度、填充颜色或描边宽度方面,Polyline 可以进行自定义。
为了实现这个目标,我们将使用一个叫做 Fabric.js 的 JavaScript 库。在引入库之后,我们将在 body 标签中创建一个包含 Polyline 的 canvas 块。然后,我们将初始化由 Fabric.js 提供的 Canvas 和 Polyline 实例,使用 selectionBackgroundColor 属性来改变选取的背景色,并在 Canvas 上渲染 Polyline,如下例所示。
语法:
var polyline = new fabric.Polyline(Points, {
selectionBackgroundColor: string
});
参数: 此属性接受一个单一参数,如上所述,具体如下所述:
- selectionBackgroundColor: 它指定选中背景的颜色。
下面的示例演示了Fabric.js中的selectionBackgroundColor属性。请注意,您必须单击对象以查看其选中背景颜色。
示例:
<html>
<head>
<!-- Loading the FabricJS library -->
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js">
</script>
</head>
<body>
<div style="text-align: center;width: 600px;">
<h1 style="color: green;">
GeeksforGeeks
</h1>
<b>
Fabric.js | Polyline selectionBackgroundColor Property
</b>
</div>
<canvas id="canvas" width="600" height="200"
style="border:1px solid #000000;">
</canvas>
<script>
// Initiate a Canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate a polyline instance
var polyline = new fabric.Polyline([
{
x: 200,
y: 10
}, {
x: 250,
y: 50
}, {
x: 250,
y: 180
}, {
x: 150,
y: 180
}, {
x: 150,
y: 50
}, {
x: 200,
y: 10
}], {
stroke: 'green',
strokeWidth: 3,
fill: 'yellow',
selectionBackgroundColor: '#e1e1e1'
});
// Render the polyline in canvas
canvas.add(polyline);
</script>
</body>
</html>
输出结果: