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


在本教程中,我們將學習如何使用 FabricJS 建立一個矩形,當滑鼠懸停在物件上時,該矩形會顯示文字游標。“text”是 FabricJS 畫布中可用的原生 cursor 樣式之一。FabricJS 提供了各種型別的滑鼠游標,例如預設、all-scroll、crosshair、col-resize、row-resize 等,這些游標在底層重用了原生游標。`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 text 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: 55,
         top: 90,
         width: 170,
         height: 70,
         fill: "#faf0e6",
         padding: 9,
         stroke: "#9370db",
         strokeWidth: 5,
         hoverCursor: "text",
      });

      // 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 and observe that the text cursor applies to the left rectangle only. We have not applied the <b>hoverCursor</b> property to the rectangle on the right.</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: 55,
         top: 90,
         width: 170,
         height: 70,
         fill: "#faf0e6",
         padding: 9,
         stroke: "#9370db",
         strokeWidth: 5,
         hoverCursor: "text",
      });

      // Initiate another rectangle object
      var rect2 = new fabric.Rect({
         left: 325,
         top: 90,
         width: 170,
         height: 70,
         fill: "#9370db",
         padding: 9,
         stroke: "#ffffff",
         strokeWidth: 5,
      });

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

更新於:2022年6月29日

701 次瀏覽

開啟你的職業生涯

完成課程獲得認證

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