Table of Contents
Syntax
Parameters
Home Web Front-end JS Tutorial How to create a rectangle that helps the cursor hover over an object using FabricJS?

How to create a rectangle that helps the cursor hover over an object using FabricJS?

Aug 27, 2023 pm 11:33 PM

如何使用 FabricJS 创建一个帮助光标悬停在对象上的矩形?

In this tutorial, we will use FabricJS to create a rectangle with a help cursor hovering over the object. "help" is one of the native cursor styles available and can also be used in the FabricJS canvas. FabricJS provides various types of cursors such as default, full scroll, crosshair, column resize, row resize, etc. which actually reuse the native cursor under the hood. hoverCursor Property sets the style when the cursor is hovering over the canvas object.

Syntax

new fabric.Rect({ hoverCursor: String }: Object)
Copy after login

Parameters

  • Options (optional) - This parameter is a ## that provides additional customization #Object to our rectangle. Using this parameter, you can change the properties of the object related to the hoverCursor property, such as color, cursor, stroke width, and many other properties.

  • < /ul>Option Key

    • hoverCursor - This property accepts a string that determines the hover The name of the cursor used when resting on the canvas object. Using this we can set the default cursor value when hovering over this rectangular object on the canvas.

    Example 1

    Pass the key to hoverCursor class

    By default, when we hover the mouse in the canvas When on a rectangular object, the cursor type is "Move". Let's look at a code example of creating a canvas using

    >help Hover the cursor over a rectangle object in FabricJS.

    &lt;!DOCTYPE html&gt;
    &lt;html&gt;
    &lt;head&gt;
       &lt;!-- Adding the Fabric JS Library--&gt;
       &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js&quot;&gt;&lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
       &lt;h2&gt;Passing the hoverCursor Key to the class&lt;/h2&gt;
       &lt;p&gt;Hover over the rectangle to see the help cursor&lt;/p&gt;
       &lt;canvas id=&quot;canvas&quot;&gt;&lt;/canvas&gt;
       &lt;script&gt;
          // Initiate a canvas instance
          var canvas = new fabric.Canvas(&quot;canvas&quot;);
          canvas.setWidth(document.body.scrollWidth);
          canvas.setHeight(250);
    
          // Initiate a rectangle object
          var rect = new fabric.Rect({
             left: 55,
             top: 90,
             width: 170,
             height: 70,
             fill: &quot;#faf0e6&quot;,
             padding: 9,
             stroke: &quot;#9370db&quot;,
             strokeWidth: 5,
             hoverCursor: &quot;help&quot;,
          });
    
          // Add them to the canvas
          canvas.add(rect);
       &lt;/script&gt;
    &lt;/body&gt;
    &lt;/html&gt;
    Copy after login

    Example 2

    Demonstrate that this only affects instances

    In this example we pass the

    hoverCursor key to the Rectangle class , which means that the hoverCursor property of each object in the canvas will not change. Changes occur only for that single object. The following code example illustrates this -

    &lt;!DOCTYPE html&gt;
    &lt;html&gt;
    &lt;head&gt;
       &lt;!-- Adding the Fabric JS Library--&gt;
       &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js&quot;&gt;&lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
       &lt;h2&gt;Demonstrating that it affects the instance only&lt;/h2&gt;
       &lt;p&gt;Hover over the rectangle objects and observe that the help cursor applies to the left object only. We have not used the &lt;b&gt;hoverCursor&lt;/b&gt; property on the right object.&lt;/p&gt;
       &lt;canvas id=&quot;canvas&quot;&gt;&lt;/canvas&gt;
       &lt;script&gt;
          // Initiate a canvas instance
          var canvas = new fabric.Canvas(&quot;canvas&quot;);
          canvas.setWidth(document.body.scrollWidth);
          canvas.setHeight(250);
    
          // Initiate a rectangle object
          var rect1 = new fabric.Rect({
             left: 55,
             top: 90,
             width: 170,
             height: 70,
             fill: &quot;#faf0e6&quot;,
             padding: 9,
             stroke: &quot;#9370db&quot;,
             strokeWidth: 5,
             hoverCursor: &quot;help&quot;,
          });
    
          // Initiate another rectangle object
          var rect2 = new fabric.Rect({
             left: 325,
             top: 90,
             width: 170,
             height: 70,
             fill: &quot;#9370db&quot;,
             padding: 9,
             stroke: &quot;#e6e6fa&quot;,
             strokeWidth: 5,
          });
    
          // Add them to the canvas
          canvas.add(rect1);
          canvas.add(rect2);
       &lt;/script&gt;
    &lt;/body&gt;
    &lt;/html&gt;
    Copy after login

    The above is the detailed content of How to create a rectangle that helps the cursor hover over an object using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

Example Colors JSON File

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

8 Stunning jQuery Page Layout Plugins

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

Build Your Own AJAX Web Applications

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

What is 'this' in JavaScript?

Improve Your jQuery Knowledge with the Source Viewer Improve Your jQuery Knowledge with the Source Viewer Mar 05, 2025 am 12:54 AM

Improve Your jQuery Knowledge with the Source Viewer

10 Mobile Cheat Sheets for Mobile Development 10 Mobile Cheat Sheets for Mobile Development Mar 05, 2025 am 12:43 AM

10 Mobile Cheat Sheets for Mobile Development

See all articles