如何使用 FabricJS 设置圆形的缩放因子(边框)?
在本教程中,我们将学习如何使用 FabricJS 设置圆形的缩放因子(边框)。圆形是 FabricJS 提供的各种形状之一。为了创建圆形,我们必须创建 fabric.Circle 类的实例并将其添加到画布上。我们可以使用 borderScaleFactor 属性,它指定控制边框的对象的缩放因子。
语法
new fabric.Circle({ 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>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>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP