如何使用 FabricJS 创建具有背景颜色的圆形?
在本教程中,我们将使用 FabricJS 创建一个具有**背景颜色**的圆形。圆形是 FabricJS 提供的各种形状之一。要创建圆形,我们必须创建一个fabric.Circle类的实例并将其添加到画布中。backgroundColor属性允许我们将颜色赋予对象的背景。它是容器(圆形所在的区域)的颜色,形状为正方形。
语法
new fabric.Circle({ backgroundColor: String }: Object)参数
options (可选) − 此参数是一个对象,它为我们的对象提供额外的自定义选项。使用此参数,可以更改与圆形相关的属性,例如颜色、光标、笔划宽度以及许多其他属性,其中backgroundColor就是一个属性。
选项键
backgroundColor − 此属性接受一个字符串,用于确定对象背景的颜色。该值可以是十六进制值、rgba 值或我们想要的背景颜色的名称。
示例 1
使用十六进制值作为backgroundColor属性的键
让我们来看一个使用十六进制颜色值将背景颜色赋予圆形对象的示例。在此示例中,我们使用了十六进制颜色代码#d0db61,这是一种深卡其色。
<!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>Creating a circle with a background colour using FabricJS</h2>
<p>Notice the dark-khaki background around the circle. It appears as we have applied the <b>backgroundColor</b> property and assigned it a Hex color code. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var cir = new fabric.Circle({
left: 215,
top: 100,
radius: 50,
fill: "#74c365",
stroke: "#00b7eb",
strokeWidth: 2,
backgroundColor: "#d0db61",
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
使用 rgba 值作为backgroundColor属性的键
我们可以使用RGBA(红色、蓝色、绿色和 alpha)值代替十六进制颜色代码。alpha 参数指定颜色的不透明度。在此示例中,我们使用了rgba 值 (255,0,0,0.7),这是不透明度为 0.7 的红色。
<!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>Creating a circle with a background colour using FabricJS</h2>
<p>Notice the orange-red background around the circle. Here we have used the <b>backgroundColor</b> property and assigned it an 'rgba' value. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var cir = new fabric.Circle({
left: 215,
top: 100,
radius: 50,
fill: "green",
stroke: "blue",
strokeWidth: 2,
backgroundColor: "rgba(255,0,0,0.7)",
});
// Adding it to the canvas
canvas.add(cir);
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