Open In App
Related Articles

Fabric.js Canvas backgroundColor Property

Improve Article
Improve
Save Article
Save
Like Article
Like

In this article, we are going to see how to set the background color of a Canvas in Fabric.js using the backgroundColor 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 color of the Canvas using the backgroundColor property.

Syntax:

fabric.Canvas(canvasElement, {
    backgroundColor: String
});

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

  • backgroundColor: It is a string that specifies the background color of the Canvas.

 

The below example illustrates the use of Fabric.js Canvas backgroundColor property in JavaScript:

Example:

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;">
            GeeksforGeeks
        </h1>
  
        <b>
            Fabric.js | Canvas backgroundColor Property
        </b>
    </div>
  
    <canvas id="canvas" width="500" height="300"
        style="border:1px solid #000000">
    </canvas>
  
    <script>
  
        // Initiate a Canvas instance 
        var canvas = new fabric.Canvas("canvas", {
                  
            // Set the background color 
            // of the Canvas
            backgroundColor: "green"
        });
    </script>
</body>
  
</html>


Output:


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks 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!

Last Updated : 30 Jul, 2021
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials