如何使用 FabricJS 创建带有背景色的椭圆形?
在本教程中,我们将学习如何使用 FabricJs 创建带有背景色的椭圆形。椭圆形是 FabricJS 提供的各种形状之一。为了创建椭圆形,我们必须创建一个 fabric.Ellipse 类的实例并将其添加到画布上。backgroundColor 属性允许我们为对象的背景分配颜色。它是椭圆形所在的容器的颜色,对于椭圆形来说,形状是矩形。
语法
new fabric.Ellipse({ 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>How to create an Ellipse with a background color using FabricJS?</h2>
<p>Observe the dark khaki color which we have applied as the background color.</p>
<canvas id="canvas" width="700" height="300" style="margin-left: 2px"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an Ellipse instance
var ellipse = new fabric.Ellipse({
left: 180,
top: 100,
rx: 90,
ry: 50,
fill: "#74c365",
stroke: "#00b7eb",
strokeWidth: 2,
backgroundColor: "#d0db61"
});
// Adding it to the canvas
canvas.add(ellipse);
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>How to create an Ellipse with a background color using FabricJS?</h2>
<p>Observe the background color which we have applied using an "rgba" value.</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: 180,
top: 100,
rx: 90,
ry: 50,
fill: "green",
stroke: "blue",
strokeWidth: 2,
backgroundColor: "rgba(255,0,0,0.7)",
});
// 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