Saturday, October 18, 2025
HomeLanguagesJavascriptFabric.js toGrayscale() Method

Fabric.js toGrayscale() Method

The toGrayscale() method is used to convert any type of the specified color representation to it’s grayscale representation.

Syntax:

toGrayscale()

Parameters: This method does not accept any parameter.

Return Value: This method returns color representation in grayscale representation.

Example 1:

HTML




<!DOCTYPE html>
<html>
    <head>
        <!-- Adding the FabricJS library -->
        <script src=
        </script>
    </head>
      
    <body>
        <script type="text/javascript">
        // Calling the toGrayscale() function over 
        // the specified color values
        console.log(new fabric.Color("hsl(0, 100%, 50%)")
                                      .toGrayscale());
        console.log(new fabric.Color("hsla(10, 40%, 53%, 0.6)")
                                      .toGrayscale());
        </script>
    </body>
   
</html>


Output:

{"_source":[77,77,77,1]}
{"_source":[125,125,125,0.6]}

Example 2:

HTML




<!DOCTYPE html>
<html>
  
<head>
  <!-- Adding the FabricJS library -->
  <script src=
  </script>
</head>
  
<body>
    <script type="text/javascript">
    // Initializing some color values in different color format
    var A = "hex(fff00f)";
    var B = "hsl(12, 13%, 25%)";
    var C = "hsla(10, 40%, 13%, 0.6)";
    var D = "rgb(10, 40, 13)";
  
    // Calling the toGrayscale() function over 
    // the above specified color values
    console.log(new fabric.Color(A).toGrayscale());
    console.log(new fabric.Color(B).toGrayscale());
    console.log(new fabric.Color(C).toGrayscale());
    console.log(new fabric.Color(D).toGrayscale());
    </script>
  
</body>
  
</html>


Output:

{"_source":[0,0,0,1]}
{"_source":[62,62,62,1]}
{"_source":[30,30,30,0.6]}
{"_source":[28,28,28,1]}
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

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS