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: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | String, options: 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.

  • 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 our canvas

Example

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.

<!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 by using the set method

Example

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.

<!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>

Conclusion

In this tutorial, we used two examples to demonstrate how you can create a canvas with Image using FabricJS.

Updated on: 26-Oct-2022

495 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements