当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Fabric.js toGrayscale()用法及代码示例


toGrayscale()方法用于将任何类型的指定颜色表示形式转换为其灰度表示形式。

用法:

toGrayscale()

参数:此方法不接受任何参数。

返回值:此方法以灰度表示形式返回颜色表示形式。

范例1:



HTML


<!DOCTYPE html>
<html>
    <head>
        <!-- Adding the FabricJS library -->
        <script src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js">
        </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>

输出:

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

范例2:

HTML


<!DOCTYPE html>
<html>
  
<head>
  <!-- Adding the FabricJS library -->
  <script src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js">
  </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>

输出:

{"_source":[0,0,0,1]}
{"_source":[62,62,62,1]}
{"_source":[30,30,30,0.6]}
{"_source":[28,28,28,1]}

相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 Fabric.js toGrayscale() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。