How to create a canvas with text cursor on hover over objects using FabricJS?

In this article, we are going to create a canvas with a text cursor on hover using FabricJS. The text cursor is one of the native cursor styles available which can be used in the FabricJS canvas.

FabricJS provides various types of cursors such as default, all-scroll, crosshair, col-resize, row-resize, etc. that reuse the native cursor under the hood. The hoverCursor property sets the style of the cursor when hovered over a canvas object.

Syntax

new fabric.Canvas(element: HTMLElement|String, { hoverCursor: String }: Object)

Parameters

  • element ? This parameter is the <canvas> element itself which can be derived using document.getElementById() or the id of the <canvas> element itself. The FabricJS canvas will be initialized on this element.

  • options (optional) ? This parameter is an Object which provides additional customizations to our canvas. Using this parameter, properties such as cursor, border width, and many other canvas-related properties can be changed, of which hoverCursor is a property that sets the default cursor value when hovering over an object on the canvas.

Example 1: Passing the hoverCursor in Constructor

The hoverCursor property accepts a String which determines the name of the cursor to be used when hovering over canvas objects. Let's see a code example to create a canvas with a text cursor on hover in 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>Canvas with text cursor on hover over objects using FabricJS</h2>
   <p>Hover the mouse over the circle to see how the cursor style changes.</p>
   <canvas id="canvas"></canvas>
   <script>
      // Initiate a canvas instance with text cursor on hover
      var canvas = new fabric.Canvas("canvas", {
         hoverCursor: "text"
      });
      
      // Creating an instance of the fabric.Circle class
      var cir = new fabric.Circle({
         radius: 60,
         fill: "#ffc87c",
         left: 30,
         top: 20
      });
      
      // Adding it to the canvas
      canvas.add(cir);
      canvas.setWidth(400);
      canvas.setHeight(250);
   </script>
</body>
</html>

Example 2: Setting hoverCursor Using Dot Notation

In this example, we have a circle object and a rectangle object. By assigning hoverCursor the value "text" using dot notation, when we hover over any object in the canvas, our cursor will change to the text cursor type.

<!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>Canvas with text cursor on hover over objects using FabricJS</h2>
   <p>Hover the mouse over the objects to see how the cursor style changes.</p>
   <canvas id="canvas"></canvas>
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      
      // Set hover cursor using dot notation
      canvas.hoverCursor = "text";
      
      // Creating an instance of the fabric.Circle class
      var cir = new fabric.Circle({
         radius: 50,
         fill: "#f8de7e",
         left: 30,
         top: 20
      });
      
      // Creating an instance of the fabric.Rect class
      var rect = new fabric.Rect({
         left: 170,
         top: 150,
         width: 60,
         height: 80,
         fill: "#87cefa",
         angle: 90
      });
      
      // Adding objects to the canvas
      canvas.add(cir);
      canvas.add(rect);
      canvas.setWidth(400);
      canvas.setHeight(250);
   </script>
</body>
</html>

Key Points

  • The hoverCursor property can be set during canvas initialization or using dot notation
  • The "text" cursor appears as an I-beam cursor, commonly used for text selection
  • This property affects all objects on the canvas unless overridden at the object level
  • Other cursor options include "pointer", "crosshair", "move", "grab", etc.

Conclusion

The hoverCursor property in FabricJS allows you to customize cursor behavior when hovering over canvas objects. You can set it during canvas initialization or modify it later using dot notation to enhance user interaction experience.

Updated on: 2026-03-15T23:19:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements