微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 FabricJS 在移动对象上创建带有等待光标的三角形?

如何使用 FabricJS 在移动对象上创建带有等待光标的三角形?

在本教程中,我们将使用 FabricJS 创建一个带有等待光标移动对象的三角形。 wait 是可用的本机光标样式之一,也可以在 FabricJS 画布中使用。 FabricJS 提供了各种类型的光标,例如认、全滚动、十字准线、列调整大小、行调整大小等,它们在幕后重用了本机光标。

moveCursor 属性设置当对象在画布中移动时光标的样式。

语法

new fabric.Triangle({ moveCursor: String }: Object)

参数

  • 选项(可选) - 此参数是一个对象 为我们的三角形提供额外的定制。使用此参数,可以更改与 moveCursor属性的对象相关的属性,例如颜色、光标、描边宽度和许多其他属性

  • 选项键

    • moveCursor - 该属性接受一个字符串,它允许我们设置在画布上移动此三角形对象时的认光标值。该值决定移动画布对象时使用的光标类型。

    示例 1

    对象移动时的认光标值在画布上移动

    认情况下,当我们在画布中围绕三角形对象移动时,光标类型是移动。让我们看一个代码示例来理解这一点。

<!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 cursor value when object is moved around the canvas</h2>
   <p>You can move around the triangle to see that the default cursor type is "move"</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: 75,
         width: 90,
         height: 80,
         fill: "#eedc82",
         stroke: "#bcb88a",
         strokeWidth: 5,
      });

      // Add it to the canvas
      canvas.add(triangle);
   </script>
</body>
</html>

示例 2

将 moveCursor 属性作为键传递给值

在此示例中,我们将 moveCursor 键传递给三角形类值为“等待”。这将确保当我们在画布中的对象周围移动时,光标值处于等待状态。下面的代码示例对此进行了说明。

<!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 the moveCursor property as key with a value</h2>
   <p>You can move around the triangle to see that the cursor type is "wait"</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: 75,
         width: 90,
         height: 80,
         fill: "#eedc82",
         stroke: "#bcb88a",
         strokeWidth: 5,
         moveCursor: "wait",
      });

      // Add it to the canvas
      canvas.add(triangle);
   </script>
</body>
</html>

以上就是如何使用 FabricJS 在移动对象上创建带有等待光标的三角形?的详细内容,更多请关注编程之家其它相关文章

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐