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: