d3 mouse event return element

Viewed 592

Trying to learn d3 (v5) here

I'm trying to add an event listener to a group of path of voronoi that I generated.

I wanted to use d3 mouse event but i couldn't access the element that trigger the event.

Here is the code:

svg
.append("g")
.attr("class","cells")
.selectAll("path")
.data(voronoi.polygons(vertices))
.enter().append("path")
.attr("d",(d)=>{return "M" + d.join("L") + "Z"})
.on("mousemove",()=>{console.log(this)})

consol.log just gave me the whole window, I also tried using d3.mouse(this), which i saw in some v3,4 examples, but it gave me this error

Uncaught TypeError: t.getBoundingClientRect is not a function
also tried d3.svg.mouse(this)
Uncaught TypeError: d3.svg.mouse is not a function
I also tried d3.touch(this), because... why not and it just return null

I want to try to access the path that the mouse is over, and change the style/attribute. i know i can do it in css, but I'm learning d3, so I would like to know how to do that.
thank you all


github link to the code: https://github.com/Sidchou/d3-exercise

1 Answers

Try to use the standard function, because arrow functions use the outer lexical scope:

.on("mousemove", function(){ console.log(this) })
Related