如何使用 FabricJS 设置圆形的填充?
在本教程中,我们将学习如何使用 FabricJS 设置圆形的填充。圆形是 FabricJS 提供的各种形状之一。为了创建一个圆形,我们必须创建一个fabric.Circle类的实例并将其添加到画布中。就像我们可以指定画布中圆形对象的位 置、颜色、不透明度和尺寸一样,我们也可以设置圆形对象的填充。这可以通过使用padding属性来实现。
语法
new fabric.Circle({ padding : Number }: Object)参数
options (可选) − 此参数是一个对象,它为我们的圆形提供了额外的自定义选项。使用此参数,可以更改与对象相关的许多属性,例如颜色、光标、笔触宽度等等,其中padding也是一个属性。
选项键
padding − 此属性接受一个数字值。分配的值决定了圆形对象与其控制边界之间的距离。
示例 1
不使用填充时的默认外观
让我们来看一段代码,这段代码显示了不使用padding属性时圆形对象的外观。我们可以看到,对象与其周围的控制边界之间没有间隙。这意味着圆形与其控制边界之间没有填充。
<!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 padding of circle using FabricJS</h2>
<p>Select the object and observe that there is no space between the object and its surrounding controlling borders. This is the default behavior. We haven't used any <b>padding</b> here. </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: "#85bb65"
});
canvas.add(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
将padding属性作为键传递
在此示例中,我们将padding属性作为键传递,其值为7。这表示圆形对象与其所有控制边界之间将有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>Setting the padding of circle using FabricJS</h2>
<p>Select the object and notice the padding between the object and its controlling border. Here we have set the <b>padding</b> at 10px.</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,
padding: 10,
radius: 50,
fill: "#85bb65"
});
canvas.add(circle);
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