如何使用 FabricJS 查找图像的原始大小?
在本教程中,我们将学习如何使用 FabricJS 查找图像的原始大小。我们可以通过创建fabric.Image的实例来创建一个Image对象。由于它是 FabricJS 的基本元素之一,我们还可以通过应用角度、不透明度等属性轻松自定义它。为了找到图像的原始大小,我们使用 getOriginalSize 方法。
语法
getOriginalSize(): Object
使用getOriginalSize方法
示例
在这个例子中,我们使用了getOriginalSize方法来获取图像的宽度和高度值。这里,返回的宽度和高度值分别是 311 和 82。
<!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>Using the getOriginalSize method</h2>
<p>
You can open the console from dev tools to see that the logged output contains the height and width of the Image
</p>
<canvas id="canvas"></canvas>
<img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="display: none" />
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiating the image element
var imageElement = document.getElementById("img1");
// Initiate an Image object
var image = new fabric.Image(imageElement, {
top: 50,
left: 110,
skewX: 15,
});
// Add it to the canvas
canvas.add(image);
// Using the getOriginalSize method
console.log(
"The original size of the Image object is: ",
image.getOriginalSize()
);
</script>
</body>
</html>
使用 getOriginalSize 方法和 cropX 属性
示例
让我们看一下当 getOriginalSize 方法与 cropX 属性结合使用时记录的输出的代码示例。在这里,我们将值 50 传递给cropX。因此,我们的 Image 对象将在 x 方向上从原始图像大小裁剪 50px。然而,当我们使用getOriginalSize方法时,我们返回的宽度和高度值分别为311和82,这进一步证明getOriginalSize只会返回原始大小图片。
<!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>Using the getOriginalSize method along with cropX property</h2>
<p>
You can open the console from dev tools to see that the original size of the image will be returned regardless of having applied image cropping in xdirection
</p>
<canvas id="canvas"></canvas>
<img
src="https://www.tutorialspoint.com/images/logo.png"
id="img1"
style="display: none"
/>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiating the image element
var imageElement = document.getElementById("img1");
// Initiate an Image object
var image = new fabric.Image(imageElement, {
top: 50,
left: 110,
skewX: 15,
cropX: 50,
});
// Add it to the canvas
canvas.add(image);
// Using the getOriginalSize method
console.log(
"The original size of the Image object is: ",
image.getOriginalSize()
);
</script>
</body>
</html>
以上就是如何使用 FabricJS 查找图像的原始大小?的详细内容,更多请关注双恒网络其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。



