如何使用 FabricJS 在物件懸停時建立帶有等待游標的圓形?


在本教程中,我們將學習如何使用 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 wait cursor on hover over objects using FabricJS</h2>
      <p>Hover the mouse over the object to see changed shape of the cursor. Here we have used the <b>hoverCursor</b> property to show a <b>wait</b> cursor on hover. </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: "wait"
         });

         // 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 wait cursor on hover over objects using FabricJS</h2>
      <p>Move the cursor over the objects. You will get to see the <b>wait</b> cursor when you hover the mouse over the left circle. We have not applied the <b>hoverCursor</b> property on the right circle which shows the default <b>move</b> cursor on hover. </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: "wait"
         });
         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日

瀏覽量:132

啟動您的職業生涯

完成課程獲得認證

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