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


d3.js interpolateHsl()用法及代碼示例

D3.js的d3.interpolatorHsl()函數用於返回兩種給定顏色的HSL顏色空間插值器函數。給出的顏色不得采用HSL格式。

用法:

d3.interpolateHsl(a, b);

參數:它帶有兩個參數:

  • a:這是一個顏色名稱。紅色或綠色。
  • b:這也是一個顏色名稱。紅色或綠色。

返回值:它返回一個內插器函數。

下麵給出了上述函數的一些示例。



範例1:在控製台中使用該函數。

<!DOCTYPE html> 
<html lang="en"> 
<head> 
  <meta charset="UTF-8"> 
  <meta name="viewport" 
        content="width=device-width,  
                 initial-scale=1.0"> 
  <title>Document</title> 
</head> 
<style> 
</style> 
<body> 
  <!--fetching from CDN of D3.js -->
  <script type = "text/javascript" 
          src = "https://d3js.org/d3.v4.min.js"> 
  </script> 
  <script> 
    console.log("Type of the function is:", 
 typeof(d3.interpolateHsl("white", "red"))) 
    console.log(d3.interpolateHsl("white", "red")(0.9)) 
    console.log(d3.interpolateHsl("white", "red")(0.4)) 
  </script> 
</body> 
</html>

輸出:

範例2:在HTML中使用該函數。

<!DOCTYPE html> 
<html lang="en"> 
<head> 
  <meta charset="UTF-8"> 
  <meta name="viewport" 
        content="width=device-width, 
                 initial-scale=1.0"> 
  <title>Document</title> 
</head> 
<style> 
  div{ 
    width:100px; 
    height:100px; 
  } 
</style> 
<body> 
  D3.interpolateHsl() 
  <div class="b1"> 
  </div> 
  <div class="b2"> 
  </div> 
  <!--fetching from CDN of D3.js -->
  <script type = "text/javascript" 
          src = "https://d3js.org/d3.v4.min.js"> 
  </script> 
  <script> 
    let color=d3.interpolateHsl("white", "green")(0.2); 
    let color2=d3.interpolateHsl("red", "green")(0.8); 
    let b1=document.querySelector(".b1"); 
    let b2=document.querySelector(".b2"); 
    b1.style.backgroundColor=color; 
    b2.style.backgroundColor=color2; 
  </script> 
</body> 
</html>

輸出:




相關用法


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