
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Set Padding of Image Using Fabric.js
In this tutorial, we are going to learn how to set the padding of Image 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 padding of Image, we use the padding property.
Syntax
new fabric.Image( element: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | String, { padding: 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 padding is a property.
callback (optional) ? This parameter is a function which is to be called after eventual filters are applied.
Options Keys
padding ? This property accepts a Number value which denotes the padding between an object and its controlling borders.
Default appearance of Image object when padding property is not used
Example
Let's see a code example to understand how the Image object appears when the padding 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 Image object when padding property is not used </h2> <p> You can select the image object to see that there is no padding between the object and its controlling borders </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: 50, stroke: "green", strokeWidth: 5, }); // Add it to the canvas canvas.add(image); </script> </body> </html>
Passing padding property as key
Example
In this example, we are assigning a value to the padding property. In this case, we have assigned it a value of 15. Therefore, there is 15px padding in between the image object and its controlling borders.
<!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 padding property as key</h2> <p> You can select the image object to see that there is 15px padding between the object and its controlling borders </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: 50, stroke: "green", strokeWidth: 5, padding: 15, }); // Add it to the canvas canvas.add(image); </script> </body> </html>