如何使用 FabricJS 创建一个在悬停于对象上时显示“禁止”光标的圆形?


在本教程中,我们将创建一个圆形,当鼠标悬停在对象上时,它会显示一个**禁止**光标。 “禁止”是可用的原生光标样式之一,它也可以在 FabricJS 画布中使用。FabricJS 提供了各种类型的光标,例如默认、全部滚动、十字准星、列调整大小、行调整大小等,这些光标都在底层重用了原生光标。`hoverCursor` 属性设置鼠标悬停在画布对象上时的光标样式。

语法

new fabric.Circle({ hoverCursor: String }: Object)

参数

  • **options (可选)** − 此参数是一个对象,它为我们的圆形提供了额外的自定义选项。使用此参数,可以更改与对象相关的许多属性,例如颜色、光标、描边宽度以及`hoverCursor`属性。

选项键

  • **hoverCursor** − 此属性接受一个**字符串**,用于确定鼠标悬停在画布对象上时使用的光标名称。使用此属性,我们可以设置鼠标悬停在画布上的圆形对象时默认的光标值。

示例 1

hoverCursor键传递给类

默认情况下,当我们将鼠标悬停在画布中的圆形对象上时,光标类型为“移动”。让我们来看一个代码示例,该示例演示如何在 FabricJS 中创建一个画布,当鼠标悬停在圆形对象上时显示**禁止**光标。

<!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>Creating a circle with not-allowed cursor on hover over objects using FabricJS</h2>
      <p>Hover the mouse over the object to see the changed shape of the cursor. Here we have used the <b>not-allowed</b> cursor. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 115,
            top: 100,
            fill: "#87ceeb",
            radius: 50,
            stroke: "#ace5ee",
            strokeWidth: 5,
            hoverCursor: "not-allowed"
         });

         // Adding it to the canvas
         canvas.add(circle);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

示例 2

演示此效果仅影响实例

在此示例中,我们将hoverCursor键传递给圆形类,这意味着不会为画布中的每个对象更改hoverCursor属性。更改仅会发生在该单个对象上。这在下面的代码示例中进行了说明。

<!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>Creating a circle with not-allowed cursor on hover over objects using FabricJS</h2>
      <p>Move the cursor over the objects. You will get to see the <b>not-allowed</b> cursor when you hover the mouse over the left circle, but not over the right circle. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 115,
            top: 100,
            fill: "#87ceeb",
            radius: 50,
            stroke: "#ace5ee",
            strokeWidth: 5,
            hoverCursor: "not-allowed"
         });
         var circle2 = new fabric.Circle({
            left: 335,
            top: 100,
            fill: "#ffe4e1",
            radius: 50,
            stroke: "#ace5ee",
            strokeWidth: 5
         });

         // Adding it to the canvas
         canvas.add(circle);
         canvas.add(circle2);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

更新于:2022年5月25日

103 次查看

开启您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.