Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

D3.js zoom.interpolate() Function

  • Last Updated : 25 Oct, 2020

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.


Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!


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:


<!DOCTYPE html>
    <meta charset="utf-8">
    <script src=
        rect {
            fill: blue;
            opacity: 0.5;
            stroke: black;
            stroke-width: 1px;
        svg {
            border: 1px solid;
            font: 13px sans-serif;
        <h1 style="color: green;">
        <h3>D3.js | zoom.interpolate() Function</h3>
        <div id="GFG">
            var width = 400;
            var height = 250;
            var lengt = 30;
            var numrects = 100;
            var rects = [];
            for (var i = 0; i < numrects; i++)
                    'x': 1 + Math.floor(Math.random() * width),
                    'y': 1 + Math.floor(Math.random() * height),
                    'h': 3 + Math.floor(Math.random() * lengt),
                    'w': 3 + Math.floor(Math.random() * lengt)
            var zoomed = d3.zoom()
                .on('zoom', function (d) {
            var svg ='#GFG')
                .attr('width', width)
                .attr('height', height)
            var g = svg.append('g')
                .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)


Example 2:


<!DOCTYPE html> 
    <meta charset="utf-8">
     <script src=""
    <script src=
        #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);
        <h1 style="color: green;"
        <h3>D3.js | zoom.interpolate() Function</h3>
        <div id="GFG"></div>
            var width = 300,
              height = 200;
            var container ="#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])
              .translateExtent([[0, 0], [width, height]])
              .on("zoom", zoomed);
            function zoomed() {
            change = d3.event.transform;
            group.attr("transform", "translate("
            + [change.x, change.y] + ")scale(" + change.k + ")")
             (3 / change.k) + "px");


My Personal Notes arrow_drop_up