Saturday, October 25, 2025
HomeLanguagesJavascriptD3.js zoom.on() Function

D3.js zoom.on() Function

The zoom.on() Function in D3.js is used to set the event listener for the specified typenames and returns the zoom behaviour. If an event listener was already registered for the same type and name, the existing listener is removed before the new listener is added.

Syntax:

zoom.on(typenames[, listener])

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

  • typenames: This parameter is the string containing one or more typename separated by whitespace.
  • listener: This parameter is an optional parameter and it is a function.

Return Value: This function returns the zoom behaviour.

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

Example 1:




<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8">
   
    <script src="https://d3js.org/d3.v4.min.js"> 
   </script> 
      
</head> 
     
<body> 
    <center>
        <h1 style="color: green;"> 
            Geeksforneveropen 
        </h1> 
       
        <h3>D3.js | zoom.on() Function</h3>
           
        <div id="GFG"></div>
           
        <script>
            var svg = d3.select("#GFG")
              .append("svg")
                .attr("width", 300)
                .attr("height", 250)
                .call(d3.zoom().on("zoom", function () {
                   svg.attr("transform", d3.event.transform)
                }))
              .append("g")
               
            svg
              .append("circle")
                .attr("cx", 150)
                .attr("cy", 125)
                .attr("r", 40)
                .style("fill", "#dc73ff")
               
        </script> 
    </center>
</body> 
   
</html> 


Output:

Example 2:




<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8">
  
    <script src="https://d3js.org/d3.v4.min.js"> 
   </script> 
      
</head> 
     
<body> 
    <center>
        <h1 style="color: green;"> 
            Geeksforneveropen 
        </h1> 
       
        <h3>D3.js | zoom.on() Function</h3>
           
        <div id="GFG"></div>
           
        <script>
            var svg = d3.select("#GFG")
                .append("svg")
                .attr("width", 300)
                .attr("height", 250)
                .call(d3.zoom().on("zoom", function () {
                    svg.attr("transform", d3.event.transform)
                }))
                .append("g")
              
            svg.append("rect")
                .attr("id", "shape")
                .attr("width", 50)
                .attr("height", 33)
                .attr("x", 125)
                .attr("y", 75)
                .style("fill", "green");
               
        </script> 
    </center>
</body> 
   
</html> 


Output:

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

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS