Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create a canvas with help cursor on hover over objects using FabricJS?
In this article, we are going to create a canvas with a help cursor on hover using FabricJS. The help cursor is one of the native cursor styles available which can be used in FabricJS canvas applications. 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 color, cursor, border width, and a lot of other properties can be changed related to the canvas, of which hoverCursor is a property with which we can set the default cursor value when hovering over an object on the canvas.
Example 1: Passing the hoverCursor key to the class
The hoverCursor property accepts a String which determines the name of the cursor to be used on hovering over the canvas object. Let's see a code example to create a canvas with a help 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 help 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
var canvas = new fabric.Canvas("canvas", {
hoverCursor: "help",
});
// Creating an instance of the fabric.Circle class
var cir = new fabric.Circle({
radius: 60,
fill: "#ff91a4",
left: 30,
top: 20,
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
Example 2: Setting the hoverCursor using dot notation
In this example, we have a circle object and a rectangle object. By assigning hoverCursor the value "help", when we hover over any object in the canvas, our cursor will change into the help 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 help cursor on hover over objects using FabricJS</h2>
<p>Here we have two objects on the canvas. 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");
canvas.hoverCursor = "help";
// Creating an instance of the fabric.Circle class
var cir = new fabric.Circle({
radius: 50,
fill: "#9ab973",
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: "#4682b4",
angle: 90,
});
// Adding it to the canvas
canvas.add(cir);
canvas.add(rect);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
Key Points
- The
hoverCursorproperty can be set during canvas initialization or using dot notation - The help cursor typically displays as a question mark cursor to indicate help is available
- This property applies to all objects on the canvas when set at the canvas level
- You can override the cursor for individual objects by setting their own
hoverCursorproperty
Conclusion
The hoverCursor property in FabricJS allows you to customize the cursor appearance when hovering over canvas objects. Use "help" as the cursor value to provide visual feedback that help or additional information is available for interactive elements.
