How to create the instance of fabric.Image from a URL string using FabricJS?


In this tutorial, we are going to learn how to create the instance of fabric.Image from a URL string 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 create the instance of fabric.Image from a URL string, we use the fromURL method.

Syntax

fromURL(url: String, callback: function, imgOptions: Object)

Parameters

  • url − This parameter accepts a String which denotes the URL to create an image from.

  • callback (optional) − This parameter is a function which is called as soon as the image is created. To this function, the newly created image is passed as the first argument. The second argument is a boolean value that indicates whether an error has occurred or not. This parameter is optional.

  • imgOptions (optional) − This parameter is an optional Object which provides additional customizations to our image. Using this parameter origin, stroke width and a lot of other properties can be changed related to the image object.

Without using the fromURL method

Example

Let’s see a code example of how the Image object appears when the fromURL method is not used. In this case, we only need to create an instance of fabric.Image and add it to our canvas.

<!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>Without using the fromURL method</h2> <p>You can see that the image object has been added to the canvas</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 fromURL method

Example

In this example, we have used the fromURL method to demonstrate that we can create an image object even when we don’t have the image element. In this case, we only need the URL of the image and pass the callback function the already created fabric.Image object as the first argument and then add it to the canvas.

<!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 fromURL method</h2> <p> You can see that the image object can be created from the image URL itself </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Calling fabric.Image.fromURL and passing the url of our desired image fabric.Image.fromURL( "https://www.tutorialspoint.com/images/logo.png", function(Img) { canvas.add(Img); } ); </script> </body> </html>

Conclusion

In this tutorial, we used two examples to demonstrate how you can create the instance of fabric.Image from a URL string using FabricJS.

Updated on: 26-Oct-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements