Fabric.js getElementOffset() 方法
getElementOffset() 方法 在Fabric.js中用于查找元素的偏移量。此方法返回的对象包含“top”和“left”属性,分别表示元素的“top”和“left”偏移量。页面中的HTML元素必须首先被选中后才能传递给此方法。
语法:
getElementOffset(element)
参数: 此方法接受一个参数,如上所述并在下面进行描述:
- element: 此参数用于指定要查找偏移量的HTML元素。
返回值: 此方法返回包含“top”和“left”偏移量的对象。
下面的示例演示了 getElementOffset() 方法在Fabric.js中的使用:
示例:
<html>
<head>
<!-- Adding the Fabric.js library -->
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js">
</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;">
GeeksforGeeks
</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>
输出: