
在本教程中,我们将学习如何使用 FabricJS 设置 Circle 的不透明度。圆形是 FabricJS 提供的各种形状之一。为了创建一个圆圈,我们将创建一个 Fabric.Circle 类的实例并将其添加到画布中。我们可以通过添加填充颜色来自定义圆形对象,消除其边框,甚至更改其尺寸。同样,我们也可以使用 opacity 属性来更改其不透明度。
语法
new fabric.Circle({ opacity: Number }: Object)参数
选项(可选) - 此参数是一个对象 为我们的圈子提供额外的定制。使用此参数,可以更改与不透明度为属性的对象相关的颜色、光标、边框宽度和许多其他属性
选项键
不透明度 - 此属性接受数字 允许我们控制对象的不透明度。 opacity 属性的默认值为 1。
示例 1
圆形对象的默认外观
让我们看一段代码,看看我们的圆形对象在使用 opacity 属性的默认值时是什么样子。
<!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 opacity of Circle using FabricJS</h2>
<p>Here we haven't used the <b>opacity</b> property, but by default, it is set to 1. This is the default appearance. </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: "#ff1493"
});
// Adding it to the canvas
canvas.add(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
将不透明度属性作为键传递
在此示例中,我们将了解为不透明度属性分配值如何更改不透明度我们画布中的圆形对象。这里我们使用 0.3 作为不透明度,这使得我们的圆形对象看起来半透明而不是完全不透明。
<!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 opacity of Circle using FabricJS</h2>
<p>Here we have set the <b>opacity</b> at 0.3, which is why the circle appears dull. </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: "#ff1493",
opacity: 0.3
});
// Adding it to the canvas
canvas.add(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>










