Wednesday, June 17, 2026
HomeLanguagesJavascriptFabric.js setAlpha() Method

Fabric.js setAlpha() Method

The setAlpha() method is used to set the value of alpha channel for the specified color.

Syntax:

setAlpha(alpha)

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

  • alpha: This parameter holds the specified alpha channel value.

Return Value: This method returns specified color value in the format of RGBA along with the given alpha channel value.

Example 1:

HTML




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


Output:

{"_source":[255,0,0,0.4]}

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)";
 var E = "rgba(10, 40, 13, 0.9)";
  
 // Calling the setAlpha() function over
 // the above specified color values
 console.log(new fabric.Color(A).setAlpha(0));
 console.log(new fabric.Color(B).setAlpha(0.4));
 console.log(new fabric.Color(C).setAlpha(0.2));
 console.log(new fabric.Color(D).setAlpha(0.7));
 console.log(new fabric.Color(E).setAlpha(0.9));
</script>
  
</body>
  
</html>


Output:

{"_source":[0,0,0,0]}
{"_source":[72,59,55,0.4]}
{"_source":[46,24,20,0.2]}
{"_source":[10,40,13,0.7]}
{"_source":[10,40,13,0.9]}
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

1 COMMENT

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6898 POSTS0 COMMENTS
Nicole Veronica
12014 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS