如何使用 FabricJS 创建带有虚线边框的圆形?
在本教程中,我们将使用 FabricJS 创建一个带有**虚线边框**的圆形。圆形是 FabricJS 提供的各种形状之一。为了创建圆形,我们将创建一个fabric.Circle类的实例并将其添加到画布中。我们可以使用borderDashArray属性更改边框虚线的样式。但是,为了使此属性生效,我们的圆形对象必须具有边框。如果将hasBorders属性设置为false值,则此属性将无效。
语法
new fabric.Circle({ borderDashArray: Array }: Object)参数
options (可选) − 此参数是一个对象,它为我们的圆形提供了额外的自定义选项。使用此参数,可以更改与borderDashArray属性相关的对象的许多属性,例如颜色、光标、描边宽度等。
选项键
borderDashArray − 此属性接受一个数组,该数组通过数组指定间隔来指定虚线样式。例如,如果我们传入一个值为[2,3]的数组,则表示2px的虚线和3px的间隙,并无限重复此样式。
示例 1
使用自定义值作为键传递borderDashArray
让我们来看一个使用 FabricJS 中的borderDashArray属性创建虚线边框的示例。在这个例子中,我们使用了[7,10]数组,它告诉我们图案将通过绘制一条7px长的线,然后是10px的间隙来创建。
<!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>Creating a circle with dash pattern border using FabricJS</h2>
<p>Select the object and observe outline of the selection area. It will have a dashed pattern as we have applied the <b>borderDashArray</b> property. </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,
radius: 50,
fill: "red",
borderColor: "rgb(128,0,128)",
borderDashArray: [7, 10]
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
将hasBorders键的值设置为“false”
正如我们在这个例子中看到的,即使我们已经为borderColor和borderDashArray属性分配了正确的数值,它们也不会生效,因为hasBorders属性被设置为false。当它设置为false时,对象的边框不会渲染。
!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>Creating a circle with dash pattern border using FabricJS</h2>
<p>Select the object and notice the outline of the selection area. Here we have set the <b>hasBorders</b> property to False, hence no border. </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,
radius: 50,
fill: "red",
borderColor: "rgb(128,0,128)",
borderDashArray: [7, 10],
hasBorders: false
});
// Adding it to the canvas
canvas.add(cir);
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