如何使用 FabricJS 设置椭圆的填充?
在本教程中,我们将学习如何使用 FabricJS 设置椭圆的填充。椭圆是 FabricJS 提供的各种形状之一。为了创建椭圆,我们必须创建一个 `fabric.Ellipse` 类的实例并将其添加到画布。就像我们可以指定画布中椭圆对象的位 置、颜色、不透明度和尺寸一样,我们也可以设置椭圆对象的填充。这可以通过使用 `padding` 属性来完成。
语法
new fabric.Ellipse({ padding : Number }: Object)参数
options (可选) - 此参数是一个对象,它为我们的椭圆提供额外的自定义设置。使用此参数可以更改与对象相关的颜色、光标、描边宽度和许多其他属性,其中 `padding` 是一个属性。
选项键
padding - 此属性接受一个 数字 值。分配的值决定了椭圆对象与其控制边界之间的距离。
示例 1
不使用 padding 时的默认外观
让我们看一个示例,该示例显示当不使用 `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>How to set the padding of Ellipse using FabricJS?</h2>
<p>Click the object and observe that there is no padding between the ellipse and its controlling borders.</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: 115,
top: 50,
rx: 80,
ry: 50,
fill: "#ff1493",
});
// Adding it to the canvas
canvas.add(ellipse);
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>How to set the padding of Ellipse using FabricJS?</h2>
<p>Select the object and you will notice that there is a padding between the ellipse and its surrounding controlling borders. Here we have used a <b>padding</b> of 7.</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: 115,
top: 50,
rx: 80,
ry: 50,
fill: "#ff1493",
padding: 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