使用 FabricJS 设置圆形选中区域的背景颜色
在本教程中,我们将学习如何使用 FabricJS 设置圆形选中区域的背景颜色。圆形是 FabricJS 提供的各种形状之一。为了创建一个圆形,我们必须创建一个fabric.Circle类的实例并将其添加到画布中。当圆形被选中时,我们可以更改对象的尺寸、旋转它或操作它。我们可以使用selectionBackgroundColor属性更改圆形选中区域的背景颜色。
语法
new fabric.Circle({ selectionBackgroundColor : String }: Object)参数
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>Setting the Background Colour on selection of circle</h2>
<p>Select the object and notice the selection area. Here we have not used the <b>selectionBackgroundColor</b> property.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var circle = new fabric.Circle({
left: 115,
top: 50,
radius: 50,
fill: "#85bb65"
});
canvas.add(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
将 selectionBackgroundColor 属性作为键传递
在这个例子中,我们为selectionBackgroundColor属性赋值。“skyBlue”颜色,因此选择区域显示为天蓝色。
<!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>Setting the Background Colour on selection of circle</h2>
<p>Select the object to see the background color of the selection area. Here we have used the <b>selectionBackgroundColor</b> property and assigned it 'skyBlue' color. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var circle = new fabric.Circle({
left: 115,
top: 50,
radius: 50,
fill: "#85bb65",
selectionBackgroundColor: "skyBlue"
});
canvas.add(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP