當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Fabric.js toHex()用法及代碼示例

toHex()方法用於將任何類型的指定顏色表示形式轉換為它的HEX(十六進製)表示形式。

用法:

toHex()

參數:此方法不接受任何參數。

返回值:此方法以十六進製格式返回顏色表示。

範例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 toHex() function over  
 // the specified rgb and rgba color format 
 console.log(new fabric.Color('rgb(10, 20, 30)').toHex()); 
 console.log(new fabric.Color('rgba(22, 255, 120, 0.9)').toHex()); 
</script> 
  
</body> 
  
</html>

輸出:

0A141E
16FF78

範例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 = "rgb(0, 12, 50)"; 
 var B = "rgba(10, 13, 250, 0.1)"; 
 var C = "hsl(0, 100%, 50%)"; 
 var D = "hsla(10%, 0, 0, 0.2)"; 
  
 // Calling the toHex() function over  
 // the above specified color values 
 console.log(new fabric.Color(A).toHex()); 
 console.log(new fabric.Color(B).toHex()); 
 console.log(new fabric.Color(C).toHex()); 
 console.log(new fabric.Color(D).toHex()); 
</script> 
  
</body> 
  
</html>

輸出:

000C32
0A0DFA
FF0000
000000

相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Fabric.js toHex() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。