如何使用FabricJS设置椭圆的填充颜色?
在本教程中,我们将学习如何通过使用FabricJS更改其填充颜色来更改椭圆对象的样式。椭圆是FabricJS提供的各种形状之一。为了创建一个椭圆,我们将不得不创建一个fabric.Ellipse类的实例并将其添加到画布中。我们可以使用fill属性更改填充颜色,该属性允许我们指定对象填充的颜色。
语法
new fabric.Ellipse({ fill: String }: Object)参数
options (可选) − 此参数是一个对象,它为我们的椭圆提供了额外的自定义功能。使用此参数,可以更改与对象相关的许多属性,其中fill是一个属性,例如颜色、光标和描边宽度。
选项键
fill − 此属性接受一个字符串值,允许我们更改对象的填充颜色。其默认值为rgb(0,0,0),即黑色。
示例1
椭圆对象的默认填充颜色
让我们来看一段代码,它向我们展示了FabricJS中椭圆对象的默认填充颜色。如果我们在创建椭圆对象时完全跳过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>How to set the fill color of Ellipse using FabricJS?</h2>
<p>Observe the ellipse is rendered black as we have not applied the <b>fill</b> property. This is the default fill color. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an ellipse instance
var ellipse = new fabric.Ellipse({
left: 215,
top: 100,
rx: 90,
ry: 50,
stroke: "#c154c1",
strokeWidth: 5,
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例2
将fill属性作为键传递
我们还可以为fill属性赋值任何颜色名称或RGBA值。在这个例子中,我们将其赋值为“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>How to set the fill color of Ellipse using FabricJS?</h2>
<p>Here we have used the <b>fill</b> property and used skyBlue color.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an ellipse instance
var ellipse = new fabric.Ellipse({
left: 215,
top: 100,
fill: "skyBlue",
rx: 90,
ry: 50,
stroke: "#c154c1",
strokeWidth: 5,
});
// Adding it to the canvas
canvas.add(ellipse);
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