D3.js zoom.extent() Function

The zoom.extent() Function in D3.js is used to set the viewport extent to the specified array of points [top-left corner, bottom-right corner].
Syntax:
zoom.extent([extent])
Parameters: This function accepts a single parameter as mentioned above and described below
- extent: This parameter can be defined as specified as a function which returns such an array.
Return Value: This function returns the zoom behaviour.
Below programs illustrate the zoom.extent() function in D3.js
Example 1:
| <!DOCTYPE html>  <html>  <head>      <metacharset="utf-8">  Â   </script> </head>     Â<body>      <center>         <h1style="color: green;">              Geeksforzambiatek          </h1>       Â        <h3>D3.js | zoom.extent() Function</h3>          Â        <divid="GFG"></div>          Â        <script>             var svg = d3.select("#GFG")               .append("svg")                 .attr("width", 300)                 .attr("height", 200)                 .call(d3.zoom()                 .extent([[0, 0], [200, 200]])                 .on("zoom", function () {                    svg.attr(              "transform", d3.event.transform)                 }))               .append("g")              Â            svg               .append("circle")                 .attr("cx", 150)                 .attr("cy", 100)                 .attr("r", 40)                 .style("fill", "green")              Â        </script>      </center> </body>   Â</html>   | 
Output:
Example 2:
| <!DOCTYPE html>  <html>  <head>      <metacharset="utf-8">       Â    </script>       Â    <style>         svg text {               fill: green;               font: 20px sans-serif;               text-anchor: center;           }             Â        rect {           pointer-events: all;         }     </style>      Â</head>      Â<body>      <center>         <h1style="color: green;">              Geeksforzambiatek          </h1>        Â        <h3>D3.js | zoom.extent() Function</h3>           Â        <svg></svg>           Â        <script>             var width = 400;             var height = 200;               Â            var svg = d3.select("svg")               .attr("width", width)               .attr("height", height);                 Â            // The scale used to display the axis.             var scale = d3.scaleLinear()               .range([10, width-20])               .domain([0, 100]);                Â            var shadowScale = scale.copy();               Â            var axis = d3.axisBottom()               .scale(scale);                 Â            var g = svg.append("g")               .attr("transform", "translate(0, 50)")               .call(axis);                 Â            // Standard zoom behavior:             var zoom = d3.zoom()               .extent([[0, 0], [width, height]])               .on("zoom", zoomed);                Â            // Call the Zoom.             var rect = svg.append("rect")               .attr("width", width)               .attr("height", height)               .attr("fill", "none")               .call(zoom);                Â            function zoomed() {               var t = d3.event.transform;               scale.domain(t.rescaleX(shadowScale).domain());               g.call(axis);             }         </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!
 
				 
					



