如何使用 FabricJS 设置椭圆的缩放因子(边框)?
在本教程中,我们将学习如何使用 FabricJS 设置椭圆的缩放因子(边框)。椭圆是 FabricJS 提供的各种形状之一。为了创建椭圆,我们必须创建一个 *fabric.Ellipse* 类的实例并将其添加到画布中。我们可以使用 *borderScaleFactor* 属性,它指定对象控制边框的缩放因子。
语法
new fabric.Ellipse({ borderScaleFactor: Number }: Object)参数
options (可选) − 此参数是一个对象,它为我们的椭圆提供了额外的自定义选项。使用此参数,可以更改与对象相关的许多属性,其中 *borderScaleFactor* 就是一个属性,例如颜色、光标、描边宽度等。
选项键
borderScaleFactor − 此属性接受一个数字,指定边框厚度。默认值为 1。
示例 1
*borderScaleFactor* 属性的默认行为
让我们来看一个例子,看看 *borderScaleFactor* 属性的默认行为。尽管我们在本例中指定了它,但即使未指定,*borderScaleFactor* 默认使用的值也是 1。
<!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 scale factor (border) of Ellipse using FabricJS?</h2>
<p>Select the object and observe its controlling borders. Here we have set the <b>borderScaleFacto</b> at 1, which is the default 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: 215,
top: 100,
fill: "white",
rx: 90,
ry: 50,
stroke: "#c154c1",
strokeWidth: 5,
borderColor: "#966fd6",
borderScaleFactor: 1,
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
将 *borderScaleFactor* 作为键传递
让我们看一段代码,在椭圆对象被选中时增加边框厚度。在本例中,我们将 *borderScaleFactor* 的值设置为 5,指定边框的厚度。
<!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 scale factor (border) of Ellipse using FabricJS?</h2>
<p>Select the object and observe its controlling borders. Here we have set the <b>borderScaleFactor</b> at 5. </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: "white",
rx: 90,
ry: 50,
stroke: "#c154c1",
strokeWidth: 5,
borderColor: "#966fd6",
borderScaleFactor: 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