Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavascriptD3.js interpolateTransformCss() Function

D3.js interpolateTransformCss() Function

The d3.interpolateTransformCss() function in D3.js is used to return the interpolator function between two given CssTransform properties. Several properties of Transform like translate, rotate, skewX and scale can be used here.

Syntax:

d3.interpolateTransformCss(a, b); 

Parameters:

  • a: It is a string of CSS Transform property.
  • b: It is a string of CSS Transform property.

Returns: It returns the interpolator function.

Below given are a few examples of the above function.

Example 1:

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>
</style>
<body>
  <!--Fetching from CDN of D3.js -->
  <script type = "text/javascript"
          src = "https:// d3js.org/d3.v4.min.js">
  </script>
  <script>
// It gives the intermediate value between two given properties
    console.log(d3.interpolateTransformCss(
    "translateX(10px) scale(1.5)",
    "translateY(15px) scale(2)"
  )(0.5))
// It gives interpolate value with 0 part of "a" and 1 part of "b"
    console.log(d3.interpolateTransformCss(
    "translateX(10px) scale(1.5)",
    "translateY(15px) scale(2)"
  )(1))
// It gives interpolate value with 0.2 part of "a" and 0.8 part of "b"
    console.log(d3.interpolateTransformCss(
    "translateX(10px) scale(1.5)",
    "translateY(15px) scale(2)"
  )(0.8))
 </script>
</body>
</html>


Output:

Example 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{
    margin-bottom: 40px;
    color: white;
    border: 2px solid black;
    width: 150px;
    height: 150px;
    background-color: green;
    transform: scale();
  }
  div{
    height: 100vh;
    display:flex;
    margin-left: 60px;
    align-items: center;
  }
</style>
<body>
  D3.interpolateTransformCss()
  <div>
    <div class="box1">
    </div>
    <div class="box2">
    </div>
    <button>Clickme</button>
  </div>
  <!--fetching from CDN of D3.js -->
  <script type = "text/javascript"
          src = "https://d3js.org/d3.v4.min.js">
   </script>
  <script>
    let box1=document.querySelector(".box1");
    let btn=document.querySelector("button");
    let interpolateFunc=d3.interpolateTransformCss(
    "translateY(15px) scale(1.5)",
    "translateX(3px) rotate(10deg)"
  )(0.5);
  func=()=>{
    box1.style.transform=interpolateFunc;
  }
  btn.addEventListener("click", func);
  </script>
</body>
</html>


Output:

Before click:

After click:

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

Most Popular

Recent Comments