如何使用 FabricJS 在对象悬停时创建带有等待光标的椭圆?


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

语法

new fabric.Ellipse({ 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 an Ellipse with wait cursor on hover over objects using FabricJS</h2>
      <p>Hover the mouse over the object to see the <b>wait</b> cursor. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");

         // Initiate an ellipse instance
         var ellipse = new fabric.Ellipse({
            left: 115,
            top: 100,
            fill: "#87ceeb",
            rx: 80,
            ry: 50,
            stroke: "#ace5ee",
            strokeWidth: 5,
            hoverCursor: "wait",
         });

         // Adding it to the canvas
         canvas.add(ellipse);
         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 an Ellipse with wait cursor on hover over objects using FabricJS</h2>
      <p>Hover the mouse over the objects. You will get to see the <b>wait</b> cursor over the left ellipse. We haven't applied the <b>hoverCursor</b> property on the right ellipse. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");

         // Initiate an ellipse instance
         var ellipseOne = new fabric.Ellipse({
            left: 115,
            top: 100,
            fill: "#87ceeb",
            rx: 80,
            ry: 50,
            stroke: "#ace5ee",
            strokeWidth: 5,
            hoverCursor: "wait",
         });

         // Initiate another ellipse instance
         var ellipseTwo = new fabric.Ellipse({
            fill: "#ffe4e1",
            left: 335,
            top: 100,
            rx: 80,
            ry: 50,
            stroke: "#ace5ee",
            strokeWidth: 5,
         });

         // Add them to the canvas
         canvas.add(ellipseOne);
         canvas.add(ellipseTwo);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

更新于: 2022年5月24日

115 次查看

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.