如何使用FabricJS隐藏矩形的控制边框?

在本教程中,我们将学习如何使用FabricJS隐藏矩形的控制边框。矩形是FabricJS提供的各种形状之一。为了创建一个矩形,我们需要创建一个fabric.Rect类的实例并将其添加到画布中。
我们可以以多种方式自定义控制边框,例如为其添加特定的颜色、虚线模式等。然而,我们也可以通过使用hasBorders属性完全消除边框。
语法
new fabric.Rect({ hasBorders: Boolean }: Object)参数
Options (optional) − 此参数是一个对象,用于提供对矩形的其他自定义设置。使用此参数,可以更改与具有边框属性的对象相关的颜色、光标、描边宽度和许多其他属性。
选项键
hasBorders − 此属性接受一个布尔值,当设置为false时,控制边框将不会被渲染。它有助于渲染控制边框。默认值为true。
示例1
矩形对象控制边框的默认外观
让我们看一个代码示例,展示矩形的控制边框的默认外观。由于hasBorders属性的默认值为True,所以在选择矩形对象时会渲染边框。
<!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>Default appearance of controlling borders of a rectangle object</h2>
<p>Select the rectangle to see the default appearance of controlling borders</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiate a rectangle object
var rect = new fabric.Rect({
left: 125,
top: 90,
width: 170,
height: 70,
strokeWidth: 3,
stroke: "#4169e1",
fill: "grey",
padding: 15,
});
// Add it to the canvas
canvas.add(rect);
</script>
</body>
</html>示例2
将hasBorders作为键并将其赋值为“false”
如果hasBorders属性被赋予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>Passing hasBorders as key and assigning a False value to it</h2>
<p>Select the rectangle to see that the controlling borders have now been hidden</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiate a rectangle object
var rect = new fabric.Rect({
left: 125,
top: 90,
width: 170,
height: 70,
strokeWidth: 3,
stroke: "#4169e1",
fill: "grey",
padding: 15,
hasBorders: false,
});
// Add it to the canvas
canvas.add(rect);
</script>
</body>
</html>
javascript