Open In App

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

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:


Article Tags :