如何使用 FabricJS 沿 Y 轴裁剪图像?
在本教程中,我们将学习如何使用 FabricJS 沿 y 轴裁剪图像。我们可以通过创建 fabric.Image 的实例来创建图像对象。由于它是 FabricJS 的基本元素之一,因此我们也可以通过应用角度、不透明度等属性轻松自定义它。为了沿 y 轴裁剪图像,我们使用 cropY 属性。
语法
new fabric.Image( element: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | String, { cropY: Number }: Object, callback: function)
参数
element − 此参数接受HTMLImageElement、HTMLCanvasElement、HTMLVideoElement 或 String,表示图像元素。字符串应为 URL,并将加载为图像。
options (可选) − 此参数是一个对象,它为我们的对象提供额外的自定义。使用此参数,可以更改与图像对象相关的原点、笔触宽度和许多其他属性,其中 cropY 是一个属性。
callback (可选) − 此参数是一个函数,在应用最终过滤器后将调用该函数。
选项键
cropY − 此属性接受一个数字值,表示沿 y 轴从原始图像大小裁剪的图像(以像素为单位)。
图像对象的默认外观
示例
让我们看一个代码示例,说明当 cropY 属性未使用时图像对象的外观。我们可以看到,沿 y 轴没有图像裁剪。
<!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>Default appearance of Image object</h2> <p>You can see that there is no image crop along the y-axis</p> <canvas id="canvas"></canvas> <img src="https://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, }); // Add it to the canvas canvas.add(image); </script> </body> </html>
使用 cropY 属性
示例
在此示例中,我们使用了 cropY 属性并为其分配了 25 的数值。因此,图像沿 y 轴裁剪了 25 像素。
<!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 cropY property</h2> <p>You can see that there is a 25px image crop along the y-axis</p> <canvas id="canvas"></canvas> <img src="https://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, cropY: 25, }); // Add it to the canvas canvas.add(image); </script> </body> </html>
结论
在本教程中,我们使用了两个示例来演示如何使用 FabricJS 沿 Y 轴裁剪图像。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP