Wednesday, February 18, 2026
HomeLanguagesJavascriptFabric.js getElementOffset() Method

Fabric.js getElementOffset() Method

The getElementOffset() method in Fabric.js is used to find the offsets of an element. The object returned by this method contains the “top” and “left” property that denotes the “top” and “left” offsets of the element respectively. The HTML element in the page has to be selected first to be passed to this method.

Syntax:

getElementOffset(element)

Parameters: This method accepts a single parameter as mentioned above and described below:

  • element: This parameter is used to specify the HTML element for which the offsets have to be found.

Return Value: This method returns an object that contains the “top” and “left” offsets as properties.

The below example demonstrates the getElementOffset() method Fabric.js:

Example: 

HTML




<html>
<head>
  <!-- Adding the Fabric.js library -->
  <script src=
  </script>
  <style>
    /* Define the CSS classes to be used */
    .box1 {
      width: 200px;
      height: 200px;
      background-color: red;
  
      position: absolute;
      top: 200px;
    }
  
    .box2 {
      width: 200px;
      height: 200px;
      background-color: green;
  
      position: absolute;
      left: 185px;
    }
  </style>
</head>
    
<body>
  <h1 style="color: green;">
    neveropen
    </h1>
      
  <h3>
    Fabric.js util.getElementOffset() Method
    </h3>
      
  <div class="box1">
    Element 1
    </div>
      
  <div class="box2">
    Element 2
    </div>
      
  <script>
    // Select the elements to be used
    let elem = 
    document.querySelector('.box1');
    let elem2 = 
    document.querySelector('.box2');
  
    console.log("Left and Top Offsets:")
  
    // Find the offsets of the element
    console.log(
      fabric.util.getElementOffset(elem)
    );
    console.log(
      fabric.util.getElementOffset(elem2)
    );
  </script>
</body>
  
</html>


Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS