如何使用 FabricJS 建立一個在懸停於物件上時顯示“禁止”游標的矩形?


在本教程中,我們將學習如何使用 FabricJS 建立一個在懸停於物件上時顯示“禁止”游標的矩形。“禁止”是可用的原生游標樣式之一,也可以在 FabricJS 畫布中使用。FabricJS 提供了各種型別的滑鼠游標,例如預設、全滾動、十字準星、列調整大小、行調整大小等,這些游標實際上是在後臺重用了原生游標。`hoverCursor` 屬性設定懸停在畫布物件上時游標的樣式。

語法

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

引數

  • **選項(可選)** − 此引數是一個物件,它為我們的矩形提供了額外的自定義功能。使用此引數,可以更改與物件的許多屬性,其中 `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>Passing the hoverCursor Key to the class</h2>
   <p>Hover over the rectangle to see the not-allowed cursor</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 rectangle object
      var rect = new fabric.Rect({
         left: 50,
         top: 90,
         width: 170,
         height: 70,
         strokeWidth: 3,
         stroke: "#4169e1",
         fill: "pink",
         padding: 15,
         hoverCursor: "not-allowed",
      });

      // Add it to the canvas
      canvas.add(rect);
   </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>Demonstrating that it affects the instance only</h2>
   <p>Hover over the rectangle objects to observe that the not-allowed cursor applies to the left object only. We have not used the <b>hoverCursor</b> property on the right object.</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 rectangle object
      var rect1 = new fabric.Rect({
         left: 50,
         top: 90,
         width: 170,
         height: 70,
         strokeWidth: 3,
         stroke: "#4169e1",
         fill: "pink",
         padding: 15,
         hoverCursor: "not-allowed",
      });

      // Initiate another rectangle object
      var rect2 = new fabric.Rect({
         left: 325,
         top: 90,
         width: 170,
         height: 70,
         strokeWidth: 3,
         stroke: "#ff69b4",
         fill: "#fae7b5",
         padding: 15,
      });

      // Add them to the canvas
      canvas.add(rect1);
      canvas.add(rect2);
   </script>
</body>
</html>

更新於:2022年6月29日

88 次檢視

啟動你的職業生涯

完成課程,獲得認證

開始學習
廣告
© . All rights reserved.