- 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 set the position of Image from left using FabricJS?
In this tutorial, we are going to learn how to set the position of Image from left 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 set the position of Image from left, we use the left property.
Syntax
new fabric.Image( element: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | String, { left: 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 left is a property.
callback (optional) − This parameter is a function which is to be called after eventual filters are applied.
Options Keys
left − This property accepts a Number which allows us to set the distance from left of the canvas for image.
Default appearance of the Image object
Example
Let’s see a code example to understand how the image object looks when the left property is not used.
<!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 the Image object</h2> <p>You can see the default appearance of Image object</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, }); // Add it to the canvas canvas.add(image); </script> </body> </html>
Passing the left property as key with a custom value
Example
In this example, we are assigning a value of 100 to the left property. This will make sure that our image object is placed at a 100px distance from the left.
<!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>Passing the left property as key with a custom value</h2> <p> You can see that the Image object is placed at a distance of 100px from the left </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: 70, left: 100, }); // Add it to the canvas canvas.add(image); </script> </body> </html>
- Related Articles
- How to set the position of Textbox from left using FabricJS?
- How to set the position of Image from top using FabricJS?
- How to set the position of an Ellipse from left using FabricJS?
- How to set the position of a Circle from left using FabricJS?
- How to set the position of a Triangle from left using FabricJS?
- How to set the position of Rectangle from top using FabricJS?
- How to set the position of Textbox from top using FabricJS?
- How to set the position of a Triangle from top using FabricJS?
- How to set the opacity of Image using FabricJS?
- How to set the padding of Image using FabricJS?
- How to set Image objects properties from options using FabricJS?
- How to set the angle of an Image using FabricJS?
- How to crop the left offset in a cloned image using FabricJS?
- How to set the border colour of Image in FabricJS?
- FabricJS – How to get the position of Image object with respect to origin?
