Fabric.js Triangle的hasBorders属性
在这篇文章中,我们将看到如何使用FabricJS设置画布三角形的边框。画布三角形意味着三角形可以移动,并且可以根据需要进行拉伸。此外,当涉及到初始描边颜色、高度、宽度、填充颜色或描边宽度时,可以自定义三角形。
要实现这一目标,我们将使用一个名为 FabricJS 的JavaScript库。在导入该库之后,我们将在body标签中创建一个包含三角形的canvas
块。然后,我们将使用FabricJS提供的Canvas和Triangle的实例,并使用 hasBorders 属性设置画布三角形的边框,并按照以下示例在画布上渲染三角形。
语法:
fabric.Triangle({
width: number,
height: number,
hasBorders: boolean
});
参数: 此函数接受上述和下述描述的三个参数:
- width: 它指定三角形的宽度。
- height: 它指定三角形的高度。
- hasBorders: 它指定是否使边框可见。
示例: 此示例使用FabricJS将画布三角形的边框禁用,如下所示。
<!DOCTYPE html>
<html>
<head>
<title>
Fabric.js Triangle hasBorders Property
</title>
<!-- Adding the FabricJS library -->
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js">
</script>
</head>
<body>
<center>
<h1 style="color: green;">
GeeksforGeeks
</h1>
<b>
Fabric.js Triangle hasBorders Property
</b>
</center>
<canvas id="canvas" width="600" height="300"
style="border:1px solid #000000">
</canvas>
<script>
// Initiate a Canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate a triangle instance
var triangle = new fabric.Triangle({
width: 300,
height: 150,
fill: '',
stroke: 'green',
strokeWidth: 3,
hasBorders: false
});
// Render the Triangle in canvas
canvas.add(triangle);
canvas.centerObject(triangle);
</script>
</body>
</html>
输出: