- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to crop an image along the X-axis using FabricJS?
In this tutorial, we are going to learn how to crop an image along the x-axis using FabricJS. We can create an Image object by creating an instance of fabric.Image. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. In order to crop an image along the x-axis, we use the cropX property.
Syntax
new fabric.Image( element: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | String, { cropX: Number }: Object, callback: function)
Parameters
element − This parameter accepts HTMLImageElement, HTMLCanvasElement, HTMLVideoElement or String which denotes the image element. The String should be a URL and would be loaded as an image.
options (optional) − This parameter is an Object which provides additional customizations to our object. Using this parameter origin, stroke width and a lot of other properties can be changed related to the image object of which cropX is a property.
callback (optional) − This parameter is a function which is to be called after eventual filters are applied.
Options Keys
cropX − This property accepts a Number value which denotes the image crop in pixels along the x-axis, from the original image size.
Default appearance of Image object
Example
Let’s see a code example of how the Image object appears when cropX property is not used. As we can see, there is no image crop along the X-axis.
<!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 x-axis</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, }); // Add it to the canvas canvas.add(image); </script> </body> </html>
Using the cropX property
Example
In this example, we have used the cropX property and assigned it a number value of 25. Therefore, the image crop is 25 pixels along the x-axis.
<!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 cropX property</h2> <p>You can see that there is a 25px image crop along the x-axis</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, cropX: 25, }); // Add it to the canvas canvas.add(image); </script> </body> </html>
Conclusion
In this tutorial, we used two examples to demonstrate how you can crop an image along the X-axis using FabricJS.
- Related Articles
- How to crop an image along the Y-axis using FabricJS?
- How to check if an Image has crop applied using FabricJS?
- How to crop the height in a cloned image using FabricJS?
- How to crop the width in a cloned image using FabricJS?
- How to crop an image using canvas?
- How to crop the left offset in a cloned image using FabricJS?
- How to crop the top offset in a cloned image using FabricJS?
- How to crop an image using crop() function in Node Jimp?
- How to crop an image to the given rectangle using imagecrop() function using PHP?
- Find the mirror image of the point $( 4,\ 5)$ along $x-axis$.
- How to add image smoothing for an Image using FabricJS?
- Transform the element along with x-axis using CSS
- How to crop an image automatically using imagecropauto() function in PHP?
- How to straighten an Image object using FabricJS?
- How to change the source of an Image using FabricJS?
