如何使用 FabricJS 设置圆形的填充颜色?
在本教程中,我们将学习如何通过使用 FabricJS 更改圆形对象的填充颜色来改变其外观。圆形是 FabricJS 提供的各种形状之一。为了创建一个圆形,我们将不得不创建一个 fabric.Circle 类的实例并将其添加到画布上。我们可以使用 fill 属性更改填充颜色,该属性允许我们指定对象填充的颜色。
语法
new fabric.Circle({ fill: String }: Object)参数
options(可选) - 此参数是一个 Object,它为我们的圆形提供额外的自定义。使用此参数,可以更改与对象相关的属性,例如颜色、光标、描边宽度以及许多其他属性,其中 fill 是一个属性。
选项键
fill - 此属性接受一个 String 值,允许我们更改对象的填充颜色。其默认值为 rgb(0,0,0),即黑色。
示例 1
圆形对象的默认填充颜色
让我们来看一段代码,它向我们展示了 FabricJS 中圆形对象的默认 fill 颜色。如果我们在创建圆形对象时完全跳过 fill 属性,它将呈现为黑色。
<!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 fill color of a Circle using FabricJS</h2>
<p>Here we have not used the <b>fill</b> property, so by default, the circle is rendered black. </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,
stroke: "#c154c1",
strokeWidth: 5
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
将 fill 属性作为键传递
我们还可以为 fill 属性分配任何颜色名称。在本例中,我们将其分配了一个值为“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 fill colour of a Circle using FabricJS</h2>
<p>Here we have used the <b>fill</b> property to render the body of the circle sky-blue. </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,
fill: "skyBlue",
radius: 50,
stroke: "#c154c1",
strokeWidth: 5
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP