
在本教程中,我们将学习如何使用 FabricJS 禁用三角形的选择性。三角形是 FabricJS 提供的各种形状之一。为了创建一个三角形,我们必须创建一个 Fabric.Triangle 类的实例并将其添加到画布中。
为了修改一个对象,我们必须在 FabricJS 中选择它。但是,我们可以通过使用 selectable 属性来禁用此行为。
语法
new fabric.Triangle{ selectable: Boolean }: Object)参数
选项(可选) - 此参数是一个对象 为我们的三角形提供额外的定制。使用此参数,可以更改与selectable属性相关的对象的属性,例如颜色、光标、描边宽度和许多其他属性。
可选择 - 此属性接受布尔值。当为其分配“假”值时,无法选择该对象进行修改。其默认值为 true。
选项键
示例 1
默认行为或可选属性设置为“true”时
极速网店升级内容:1.网店系统升级到Net2.0框架2.网店系统架构升级,使系统速度提升30%3.修正购物车下一步容易出错的问题4.修正会员删除的Bug5.修正广告时间不能选择的问题6.修正程序的兼容问题2008版升级内容如下:1、修正打SP2后用户登陆时出错的问题;2、修正用户列表错误的问题;3、修正程序的兼容性问题;4、修正用户Cookie加密码乱码的问题5、修正程序中存在的小BUG;6、优化
让我们看一个代码示例,以了解默认情况下 selectable 属性设置为 True 时对象的行为方式。当 selectable 属性设置为 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 behaviour or when selectable property is set to 'true'</h2>
<p>You can try moving the triangle around the canvas or scaling it to prove that it's selectable</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 triangle object
var triangle = new fabric.Triangle({
left: 105,
top: 70,
width: 90,
height: 80,
fill: "#746cc0",
stroke: "#967bb6",
strokeWidth: 5,
});
// Add it to the canvas
canvas.add(triangle);
</script>
</body>
</html>示例 2
将可选属性作为键传递
在此示例中,我们为可选属性分配一个 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 selectable property as key</h2>
<p>You can see that the triangle is no longer selectable</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 triangle object
var triangle = new fabric.Triangle({
left: 105,
top: 70,
width: 90,
height: 80,
fill: "#746cc0",
stroke: "#967bb6",
strokeWidth: 5,
selectable: false,
});
// Add it to the canvas
canvas.add(triangle);
</script>
</body>
</html>









