D3.js mouse() Function Last Updated : 17 Oct, 2024 Comments Improve Suggest changes Like Article Like Report The d3.mouse() function in D3.js is used to return the x-coordinate and y-coordinate of the current event when clicked. Syntax:d3.mouse(container);Parameters: This function accepts a single parameter as mentioned above and described below:container: It is the name of the container or the HTML tag to which the event is attached.Return Values: It returns the array of coordinates x and y.Example1: Below examples illustrate the D3.js mouse() function In JavaScript: HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" path1tent="width=device-width, initial-scale=1.0" /> <title>D3.js mouse() Function</title> </head> <style> div { width: 200px; height: 200px; background-color: green; } </style> <body> <div></div> <script src="https://d3js.org/d3.v4.min.js"> </script> <script src="https://d3js.org/d3-selection.v1.min.js"> </script> <script> let btn = document.querySelector("div"); let div = d3.select("div"); div.on("click", createDot); function createDot() { // Using d3.mouse() function let pos = d3.mouse(this); console.log(pos); } </script> </body> </html> Output:Example 2: HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" path1tent="width=device-width, initial-scale=1.0" /> <title>D3.js mouse() Function</title> </head> <style> .div { width: 200px; height: 200px; background-color: green; overflow: hidden; } div { background-color: red; width: 10px; height: 10px; } </style> <body> <h2>click on the box</h2> <div class="div"></div> <script src="https://d3js.org/d3.v4.min.js"> </script> <script src="https://d3js.org/d3-selection.v1.min.js"> </script> <script> let btn = document.querySelector("div"); var div = d3.select("div"); div.on("click", createDot); function createDot() { // Using d3.mouse() function let pos = d3.mouse(this); console.log(pos); d3.select("div") .append("div") .style("background-color", "white") .style("position", "absolute") .style("margin-left", `${pos[0] - 10}px`) .style("margin-right", `${pos[0] - 10}px`) .style("margin-top", `${pos[1] - 10}px`) .style("margin-bottom", `${pos[1] - 10}px`); } </script> </body> </html> Output:Before clicking the box:After clicking the box: Comment More infoAdvertise with us Next Article D3.js mouse() Function T tarun007 Follow Improve Article Tags : JavaScript Web Technologies D3.js Similar Reads D3.js zoom() Function The d3.zoom() Function in D3.js is used to create a new zoom behaviour. It is used to apply the zoom transformation on a selected element. Syntax:d3.zoom();Parameters: This function does not accept any parameter.Return Value: This function returns the zoom behaviour.Below programs illustrate the d3. 2 min read D3.js namespace() Function The d3.namespace() function is used to return an object that contains space and local attributes that describe the full namespace URL and the local name. If there is a colon in the name than the string on the left side of the colon is a namespace prefix. Syntax: d3.namespace(name); Parameters: This 1 min read D3.js brush.move() Function The brush.move() Function in D3.js is used to set the active selection of the brush on the specified group. Syntax: brush.move(group, selection); Parameters: This function accepts a single parameter as mentioned above and described below group: This parameter is the specified group on which brush is 3 min read D3.js selector() Function The d3.selector() function is used to return a function that returns the very first descendant of the element given as the parameter. Syntax: d3.selector(selector) Parameters: This function takes only one parameter which is given above and described below: selector: This is the string of the element 2 min read D3.js | Path.moveTo() Function D3.js is mostly used for making of graph and visualizing data on the HTML svg elements. D3 somehow is related to Data Driven Documents. The Path.moveTo() function is used to move a point inside the svg element. This library is also capable enough to draw simulations, 2D graphs and 3D graphs and used 2 min read Like