首页 > web前端 > js教程 > 正文

如何仅当对象完全包含在 FabricJS 中的选择区域中时才启用对象选择?

WBOY
发布: 2023-08-27 11:25:07
转载
1316 人浏览过

如何仅当对象完全包含在 FabricJS 中的选择区域中时才启用对象选择?

在本文中,我们将学习如何使用 FabricJS 仅当对象完全包含在选择区域中时才启用对象的选择。我们可以使用 SelectionFullyContained 属性来实现此目的。

语法

new fabric.Canvas(element: HTMLElement|String, { selectionFullyContained: Boolean }: Object)
登录后复制

参数

  • 元素 - 此参数是 元素本身,可以使用 Document.getElementById() 或 元素本身的 id 派生。 FabricJS 画布将在此元素上初始化。

  • 选项(可选) - 此参数是一个对象,它提供对我们的画布进行额外的定制。使用此参数,可以更改与画布相关的颜色、光标、边框宽度和许多其他属性,其中selectionFullyContained 是一个属性。它接受一个布尔值,该值确定是否仅当对象完全包含在选择区域中时才应选择该对象。默认值为 False。

示例 1

传递值为 False 的 SelectionFullyContained 键

让我们看一下 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>
登录后复制

以上是如何仅当对象完全包含在 FabricJS 中的选择区域中时才启用对象选择?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!