Skip to content
Related Articles

Related Articles

Improve Article

D3.js zoom.translateExtent() Function

  • Last Updated : 07 Sep, 2020
Geek Week

The zoom.translateExtent() Function in D3.js is used to set the translate extent to the specified array of points [[top-left corner, bottom-right corner]] and returns this zoom behaviour.



Parameters: This function accepts a single parameter as mentioned above and described below

  • extent: This parameter is used to set the wheel delta function to the specified function.

Return Value: This function returns the zoom behaviour.

Below programs illustrate the zoom.translateExtent() function in D3.js

Example 1:

<!DOCTYPE html> 
    <meta charset="utf-8">
    <script src=
    <script src=
        <h1 style="color: green;"
  D3.js | zoom.translateExtent() Function
        <div id="GFG"></div>
            var height = 300;
            var width = 300;
            var zoom = d3.zoom()
               .translateExtent([[0, 0], [300, 300]])
                .on("zoom", zoomed);
            var svg ="#GFG")
                    .attr("width", height)
                    .attr("height", width)
                    .attr("x", 0)
                    .attr("y", 0)
                    .attr("height", 200)
                    .attr("width", 400)
                    .style("fill", "green");
                var circle = svg.append("circle")
                    .attr("cx", 100)
                    .attr("cy", 100)
                    .attr("r", 20)
                    .style("fill", "red")
                function zoomed() {
                    "transform", d3.event.transform);


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.translateExtent() Function</h3>
            <div id="GFG"></div>
                var width = 300,
                    height = 200;
                var container =
                var svg = 
                      "id", "svg").attr(
                      "width", width).attr(
                      "height", height);
                var group = svg.append("g");
                var shape = group.append(
                  "id", "shape").attr(
                  "width", 150).attr(
                  "height", 100).attr(
                  "x", 75).attr("y", 50);
                zoom = d3
                    .scaleExtent([1, 3])
                        [0, 0],
                        [width, height],
                    .on("zoom", zoomed);
                function zoomed() {
                    change = d3.event.transform;
                    group.attr("transform", "translate(" +
                    [change.x, change.y] + ")scale(" + 
                    change.k + ")");
                        "stroke-width", 3 / change.k + "px");


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!

My Personal Notes arrow_drop_up
Recommended Articles
Page :