Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create a canvas with Image using FabricJS?
In this tutorial, we are going to learn how to create a canvas with 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.
Syntax
new fabric.Image(element, options, callback)
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.
callback (optional) ? This parameter is a function which is to be called after eventual filters are applied.
Creating an Instance of fabric.Image() and Adding it to Canvas
Let's see a code example of how we can add an Image object to our canvas. The only required parameter is the image element whereas the second and third arguments are the optional options object and optional callback function respectively. Since adding image needs an image HTML element, thus we have an img tag used for the same.
Example
<!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>Creating an instance of fabric.Image() and adding it to our canvas</h2>
<p>You can see that the image has been added</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, {
opacity: 1,
left: 110,
top: 50,
});
// Add it to the canvas
canvas.add(image);
</script>
</body>
</html>
Manipulating the Image Object Using the set Method
In this example, we have assigned the properties to the Image object by using the set method which is a setter for values. Any property related to stroke, strokeWidth, angle, scaling, rotation, etc., can be mutated by using this method.
Example
<!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>Manipulating the Image object by using the set method</h2>
<p>You can see the Image object in canvas now with set values</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);
// Set the properties
image.set("stroke", "grey");
image.set("strokeWidth", 10);
image.set("opacity", 0.8);
image.set("top", 50);
// Add it to the canvas
canvas.add(image);
</script>
</body>
</html>
Key Properties for Image Customization
FabricJS provides numerous properties to customize image objects:
- opacity - Controls transparency (0.0 to 1.0)
- left/top - Positioning on canvas
- stroke/strokeWidth - Border styling
- angle - Rotation angle in degrees
- scaleX/scaleY - Scaling factors
Conclusion
FabricJS makes it simple to add and manipulate images on canvas. Use the fabric.Image constructor with an HTML image element, and customize properties either during initialization or using the set method.
