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


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

D3.js的d3.interpolatorHslLong()函數與interpolateHsl()函數幾乎相同,兩者之間的唯一區別是該函數不使用兩種色調之間的最短路徑。

用法:

d3.interpolateHslLong(a, b);

參數:它帶有兩個參數:

  • a:它是字符串類型的顏色的名稱。
  • b:它也是顏色的名稱。

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

以下是d3.interpolateHslLong()函數的一些示例。



範例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> 
    // Printing the return type of the function 
    console.log("Type of the function is:", 
 typeof(d3.interpolateHslLong("green", "yellow"))) 
    console.log( 
"A RGB string:", d3.interpolateHslLong("white", "yellow")(0.435)) 
    console.log( 
"A RGB string", d3.interpolateHslLong("white", "green")(0.888996)) 
  </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> 
  .box1, .box2{ 
    display:flex; 
    margin-right:10px; 
    width:100px; 
    height:100px; 
  } 
  div{ 
    display:flex; 
  } 
</style> 
<body> 
  D3.interpolateHslLong() 
  <div> 
    <div class="box1"> 
    </div> 
    <div class="box2"> 
    </div> 
  </div> 
  <!--fetching from CDN of D3.js -->
  <script type = "text/javascript" 
          src = "https://d3js.org/d3.v4.min.js"> 
</script> 
  <script> 
    let b1=document.querySelector(".box1"); 
    let b2=document.querySelector(".box2"); 
    let color=d3.interpolateHslLong("white", "red", "green")(0.2); 
    let color2=d3.interpolateHslLong("red", "green")(0.8); 
    b1.style.backgroundColor=color; 
    b2.style.backgroundColor=color2; 
  </script> 
</body> 
</html>

輸出:




相關用法


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