如何使用FabricJS在物件懸停時建立帶有十字準星游標的圓形?


在本教程中,我們將學習如何使用FabricJS建立一個在物件懸停時顯示**十字準星****游標**的圓形。十字準星是可用的原生游標樣式之一,也可以在FabricJS畫布中使用。FabricJS 提供各種型別的遊標,例如 default、all-scroll、crosshair、col-resize、row-resize 等,這些遊標在底層重用了原生遊標。`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 crosshair cursor on hover over objects using FabricJS</h2>
      <p>Move the cursor inside the circle and observe the cursor. Here we have applied <b>hoverCursor</b> property and assigned it the value 'crosshair'.</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: "#a2006d",
            radius: 50,
            stroke: "#c154c1",
            strokeWidth: 5,
            hoverCursor: "crosshair"
         });

         // Adding it to the canvas
         canvas.add(circle);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

示例2

證明hoverCursor應用於特定物件

在此示例中,我們將`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 crosshair cursor on hover over objects using FabricJS</h2>
      <p> Move the cursor over the objects. You will get to see the <b>crosshair</b> cursor when you hover the mouse over the left circle. </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: "white",
            radius: 50,
            stroke: "#c154c1",
            strokeWidth: 5,
            hoverCursor: "crosshair"
         });
         var cir = new fabric.Circle({
            left: 280,
            top: 100,
            fill: "#a2006d",
            radius: 50,
         });

         // Adding it to the canvas
         canvas.add(circle);
         canvas.add(cir);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

更新於:2022年5月25日

200 次瀏覽

開啟你的職業生涯

完成課程獲得認證

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