Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptFabric.js Canvas backgroundImage Property

Fabric.js Canvas backgroundImage Property

In this article, we are going to see how to set the background image of a Canvas in Fabric.js using the backgroundImage property. The Canvas in Fabric.js is used as a wrapper over the native canvas object provided by HTML. It provides high-level access to the underlying canvas allowing it to have an object model, allow parsing for SVG files, and allowing the canvas to be interacted with in an intuitive manner.

Approach: To make it possible we are going to use a JavaScript library called Fabric.js. After importing the library, we will create the canvas block in the body tag. After this, we will initialize an instance of the canvas object provided by Fabric.js and change the background image of the canvas using the backgroundImage property.

Syntax:

fabric.Canvas(canvasElement, {
    backgroundImage: String | Fabric.Image
});

Parameters: This property accepts a single parameter as mentioned above and described below.

  • backgroundImage: It is a string containing the image URL or a Fabric.Image object that specifies the background image of the Canvas.

 

The below examples illustrate the use of Fabric.js Canvas backgroundImage property in JavaScript.

Example 1: This example uses a string URL to change the background image of the Canvas.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <!-- Adding the FabricJS library -->
    <script src=
    </script>
</head>
  
<body>
    <div style="text-align:center;width:500px;">
        <h1 style="color:green;">
            neveropen
        </h1>
        <b>
            Fabric.js | Canvas backgroundImage Property
        </b>
    </div>
  
    <canvas id="canvas" width="660" height="250" 
        style="border:1px solid #000000">
    </canvas>
  
    <script>
  
        // Initiate a Canvas instance 
        let canvas = new fabric.Canvas("canvas", {
  
            // Set the background image 
            // of the Canvas
            backgroundImage:
        });
    </script>
</body>
  
</html>


Output:

Example 2: This example uses a Fabric.Image object to change the background image of the Canvas.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <!-- Adding the FabricJS library -->
    <script src=
    </script>
</head>
  
<body>
    <div style="text-align: center;
              width:660px;">
        <h1 style="color:green;">
            neveropen
        </h1>
        <b>
            Fabric.js | Canvas backgroundImage Property
        </b>
    </div>
  
    <img src=
        id="my_img" style="display:none">
  
    <canvas id="canvas" width="660" height="250" 
        style="border:1px solid #000000">
    </canvas>
  
    <script>
  
        // Select the image from the document
        let selectedImage =
            document.querySelector("#my_img");
  
        // Create a Fabric.Image object
        let bg_img = new fabric.Image(selectedImage);
  
        // Initiate a Canvas instance 
        let canvas = new fabric.Canvas("canvas", {
  
            // Set the background image
            // of the Canvas to the above
            // Fabric.Image object
            backgroundImage: bg_img
        });
    </script>
</body>
  
</html>


Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments