首页  >  文章  >  web前端  >  如何使用 FabricJS 创建带有不允许的光标的画布?

如何使用 FabricJS 创建带有不允许的光标的画布?

WBOY
WBOY转载
2023-09-08 10:21:03847浏览

如何使用 FabricJS 创建带有不允许的光标的画布?

在本文中,我们将使用 FabricJS 创建一个带有不允许的光标的画布。不允许的游标可用于指示不会执行已请求的任何操作。 not-allowed 是可用的原生光标样式之一,也可以在 FabricJS 画布中使用。

FabricJS 提供各种类型的光标,如默认、全滚动、十字准线、列调整大小、行调整大小等等,它们正在重用本机光标底层。根据操作系统的不同,每个游标看起来都略有不同。

语法

new fabric.Canvas(element: HTMLElement|String, { defaultCursor: String }: Object)

参数

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

  • 选项(可选) - 此参数是一个对象,它提供对我们的画布进行额外的定制。使用这个参数可以改变画布相关的颜色、光标、边框宽度等很多属性,其中defaultCursor是一个属性,通过它我们可以设置我们想要的光标类型。

示例 1

defaultCursor 属性接受一个字符串,该字符串确定要在画布上使用的光标的名称。我们将其设置为not-allowed,以使用不允许的游标。让我们看一个示例,在 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>Canvas with not-allowed cursor using FabricJS</h2>
   <p>Bring the cursor inside the canvas to see the changed shape of cursor</p>
   <canvas id="canvas"></canvas>
   <script>
      //Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas", {
         defaultCursor: "not-allowed"
      });
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
   </script>
</body>
</html>

示例 2

在此示例中,我们将在画布上添加一个圆圈以及不允许的光标

<!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>Canvas with not-allowed cursor using FabricJS</h2>
   <p>Here we have added a circle to the canvas along with the not-allowed cursor</p>
   <canvas id="canvas"></canvas>
   <script>
      //Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas", {
         defaultCursor: "not-allowed"
      });
      // Initiate a Circle instance
      var circle = new fabric.Circle({
         radius: 50,
         fill: "green"
      });
      // Render the circle in canvas
      canvas.add(circle);
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
   </script>
</body>
</html>

以上是如何使用 FabricJS 创建带有不允许的光标的画布?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除