Open In App

Fabric.js Image backgroundColor Property

Improve
Improve
Like Article
Like
Save
Share
Report

Fabric.js is a javascript library that is used to work with canvas. Image is one of the class of fabric.js that is used to create image instances. The backgroundColor property of the image is used to set the background color of the image.

Approach: First import the fabric.js library. After importing the library, create a canvas block in the body tag which will contain the image. After this, initialize an instance of Canvas and image class provided by Fabric.JS and set the background color of the image using the backgroundColor property of the image object.

Syntax:

fabric.Image(image, {
   backgroundColor:"string"
});

Parameters: The above function takes two parameters as mentioned above and described below:

  • image: This parameter hold the image.
  • backgroundColor: This sets the background color of the image.

Example 1: This example uses FabricJS to set the background color of the canvas image as shown in the below given example.




<!DOCTYPE html> 
<html
  
<head>     
    <!-- Adding the FabricJS library --> 
    <script src
    </script
</head
  
<body
    <h1 style="color: green;">GeeksforGeeks</h1
    <b>Fabric.js | Image backgroundColor Property </b
  
    <canvas id="canvas" width="400" height="300"
        style="border:2px solid #000000"
    </canvas
  
    <img src =
    width="100" 
    height="100"
    id="my-image">
      
    <script
      
        // Create the instance of canvas object
        var canvas = new fabric.Canvas("canvas"); 
        var img= document.getElementById('my-image');
        var imgInstance = new fabric.Image(img, {
            opacity:0.8,
            backgroundColor:"yellow"
        });
        canvas.add(imgInstance);
    </script
</body
  
</html>


Output:

Example 2:

HTML




<!DOCTYPE html> 
<html
  
<head
    <!-- Adding the FabricJS library --> 
    <script src
    </script
</head
  
<body
    <h1 style="color: green;">GeeksforGeeks</h1
    <b>Fabric.js | Image backgroundColor Property </b
  
    <canvas id="canvas" width="300" height="200"
        style="border:2px solid #000000"
    </canvas
  
    <img src =
    width="100" 
    height="100"
    id="my-image"
    style="display:none">
    <br>
    <button onclick="func()">Clickme</button>
    <script
      
        // Create the instance of canvas object
        var canvas = new fabric.Canvas("canvas"); 
        var img= document.getElementById('my-image');
        var imgInstance = new fabric.Image(img, {
        });
        canvas.add(imgInstance);
        func=()=>{
            // Create the instance of canvas object
            var canvas = new fabric.Canvas("canvas"); 
            var img= document.getElementById('my-image');
            var imgInstance = new fabric.Image(img, {
                opacity:0.4,
                backgroundColor:"green"
            });
            canvas.clear();
            canvas.add(imgInstance);
        }
    </script
</body
  
</html>


Output:

  • Before clicking the clickme button:

  • After clicking the clickme button:



Last Updated : 31 Aug, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads