Skip to content
Related Articles

Related Articles

Improve Article

D3.js zoom.on() Function

  • Last Updated : 07 Sep, 2020

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:

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!

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;"
            Geeksforgeeks 
        </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;"
            Geeksforgeeks 
        </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:




My Personal Notes arrow_drop_up
Recommended Articles
Page :