如何使用 FabricJS 设置圆的比例因子(边框)?

在本教程中,我们将使用 FabricJS 设置 Circle 的比例因子(边框)。圆形是 FabricJS 提供的各种形状之一。为了创建一个圆圈,我们必须创建一个 Fabric.Circle 类的实例并将其添加到画布中。我们可以使用 borderScaleFactor 属性来指定控制边框的对象的比例因子。
语法
new fabric.Circle({ borderScaleFactor: Number }: Object)参数
选项(可选) - 此参数是一个对象< /em>这为我们的圈子提供了额外的定制。使用此参数,可以更改与 borderScaleFactor 为属性的对象相关的颜色、光标、描边宽度和许多其他属性等属性。
ul>borderScaleFactor − 此属性接受指定边框粗细的数字。默认值为 1。
选项键
示例 1
borderScaleFactor属性的默认行为< /p>
让我们看一个描述 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>Setting the scale factor (border) of circle using FabricJS</h2>
<p>Select the object and notice its border. Here we have set <b>borderScaleFactor</b> at 1, which is the default value. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var cir = new fabric.Circle({
left: 215,
top: 100,
fill: "white",
radius: 50,
stroke: "#c154c1",
strokeWidth: 5,
borderColor: "#966fd6",
borderScaleFactor: 1
});
// Adding it to the canvas
canvas.add(cir);
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>Setting the scale factor (border) of a circle using FabricJS</h2>
<p>Select the object and notice the thickness of its border. 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");
var cir = new fabric.Circle({
left: 215,
top: 100,
fill: "white",
radius: 50,
stroke: "#c154c1",
strokeWidth: 5,
borderColor: "#966fd6",
borderScaleFactor: 5
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
javascript