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


在本教程中,我們將使用 FabricJS 建立一個在懸停於物件上時顯示禁止游標的橢圓形。“禁止”是可用的原生游標樣式之一,它也可以在 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 not-allowed cursor on hover over objects using FabricJS</h2>
      <p>Hover the mouse over the object to see the <b>not-allowed</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: 90,
            top: 100,
            fill: "#87ceeb",
            stroke: "#ace5ee",
            strokeWidth: 5,
            hoverCursor: "not-allowed",
            rx: 80,
            ry: 50,
         });

         // 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 not-allowed cursor on hover over objects using FabricJS</h2>
      <p>Hover the mouse over the objects. The left ellipse would show the <b>not-allowed</b> cursor. We haven't applied the hoverCursor property to 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: 90,
            top: 100,
            fill: "#87ceeb",
            stroke: "#ace5ee",
            strokeWidth: 5,
            hoverCursor: "not-allowed",
            rx: 80,
            ry: 50,
         });

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

         // 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日

87 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.