D3.js zoom.interpolate() Function

The zoom.interpolate() function in D3.js library is used to set the interpolation factory for zoom transitions to the specified function. To apply direct interpolation between two views, try d3.interpolate instead.
Syntax:
zoom.interpolate([interpolate])
Parameters: This function accepts a single parameter as mentioned above and described below.
- interpolate: This parameter is the duration for zoom transitions.
Return Value: This function returns the zoom behavior.
Below program illustrate the zoom.interpolate() function in D3.js library.
Example 1:
HTML
<!DOCTYPE html><html>Â
<head>Â Â Â Â <meta charset="utf-8">Â
    <script src=    </script>Â
    <style>        rect {            fill: blue;            opacity: 0.5;            stroke: black;            stroke-width: 1px;        }Â
        svg {            border: 1px solid;            font: 13px sans-serif;        }    </style>Â
</head>Â
<body>    <center>        <h1 style="color: green;">            Geeksforzambiatek        </h1>Â
        <h3>D3.js | zoom.interpolate() Function</h3>Â
        <div id="GFG">        </div>Â
        <script>            var width = 400;            var height = 250;            var length = 30;Â
            var numrects = 100;            var rects = [];Â
            for (var i = 0; i < numrects; i++)                rects.push({                    'x': 1 + Math.floor(Math.random() * width),                    'y': 1 + Math.floor(Math.random() * height),                    'h': 3 + Math.floor(Math.random() * length),                    'w': 3 + Math.floor(Math.random() * length)                });Â
            var zoomed = d3.zoom()                .interpolate(d3.interpolate)                .on('zoom', function (d) {                    g.attr('transform',                        d3.event.transform);                });Â
Â
            var svg = d3.select('#GFG')                .append('svg')                .attr('width', width)                .attr('height', height)Â
            var g = svg.append('g')Â
            g.selectAll('rect')                .data(rects)                .enter()                .append('rect')                .attr('x', function (d) { return d.x; })                .attr('y', function (d) { return d.y; })                .attr('height', function (d) { return d.h; })                .attr('width', function (d) { return d.w; })                .classed('no-zoom', true)Â
            svg.call(zoomed);        </script>    </center></body>Â
</html> |
Output:
Example 2:
HTML
<!DOCTYPE html> <html> <head>     <meta charset="utf-8">    </script>            <script src=    </script>           <style>        #svg {            background-color: rgb(149, 160, 230);        }        #shape {            fill: rgb(232, 7, 228);            stroke: white;            stroke-width: 3px;        }        #shape:hover {            fill: rgb(13, 214, 30);        }    </style></head>        <body>     <center>        <h1 style="color: green;">             Geeksforzambiatek         </h1>                  <h3>D3.js | zoom.interpolate() Function</h3>                   <div id="GFG"></div>                   <script>            var width = 300,              height = 200;                           var container = d3.select("#GFG").append("div");                           var svg = container.append("svg")                .attr("id", "svg")                .attr("width", width)                .attr("height", height);                           var group = svg.append("g");                           var shape = group.append("rect")                   .attr("id", "shape")                   .attr("width", 50)                   .attr("height", 33)                   .attr("x", 125)                   .attr("y", 75);                           zoom = d3.zoom()              .interpolate([1, 3])              .interpolate(d3.interpolateZoom)              .translateExtent([[0, 0], [width, height]])              .on("zoom", zoomed);                           svg.call(zoom);                           function zoomed() {            change = d3.event.transform;            group.attr("transform", "translate("            + [change.x, change.y] + ")scale(" + change.k + ")")            group.select("#shape").style("stroke-width",              (3 / change.k) + "px");            }        </script>     </center></body>   </html> |
Output:
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek 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!




