如何在 FabricJS 中仅当对象完全包含在选择区域内时才启用对象的选中?
在本文中,我们将学习如何使用 FabricJS 仅当对象完全包含在选择区域内时才启用对象的选中。我们可以使用 selectionFullyContained 属性来实现这一点。
语法
new fabric.Canvas(element: HTMLElement|String, { selectionFullyContained: Boolean }: Object)参数
元素 − 此参数是<canvas> 元素本身,可以使用 Document.getElementById() 或<canvas> 元素本身的 id 获取。FabricJS 画布将在此元素上初始化。
选项(可选) − 此参数是一个对象,它为我们的画布提供了额外的自定义功能。使用此参数,可以更改与画布相关的属性,例如颜色、光标、边框宽度以及许多其他属性,其中 selectionFullyContained 是一个属性。它接受一个布尔值,用于确定对象是否仅在完全包含在选择区域内时才被选中。其默认值为 False。
示例 1
将 selectionFullyContained 键的值设置为 False
让我们看一个 FabricJS 中默认行为的代码示例,即即使对象未完全包含在选择区域内,它也会被选中。在此示例中,我们将 selectionFullyContained 的值设置为 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>Enabling selection of an object only when it is fully contained in a selection area</h2>
<p>Select a partial area around the object. The entire object would be selected even if you select a partial area containing the object.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas", {
selectionFullyContained: false
});
// Creating an instance of the fabric.Rect class
var circle = new fabric.Circle({
left: 215,
top: 100,
radius: 50,
fill: "orange",
});
// Adding it to the canvas
canvas.add(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
将 selectionFullyContained 键的值设置为 True 传递给类
让我们看一个代码示例,其中 selectionFullyContained 属性的值已设置为 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>Enabling selection of an object only when it is fully contained in a selection area</h2>
<p>Here you cannot select the object by selecting a partial area around the object. The object must be fully contained inside the selection area.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas", {
selectionFullyContained: true
});
// Creating an instance of the fabric.Rect class
var circle = new fabric.Circle({
left: 215,
top: 100,
radius: 50,
fill: "orange"
});
// Adding it to the canvas
canvas.add(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP