Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

d3.mouse always returns [NaN, NaN] #66

Closed
sungwoncho opened this issue Aug 25, 2016 · 9 comments
Closed

d3.mouse always returns [NaN, NaN] #66

sungwoncho opened this issue Aug 25, 2016 · 9 comments

Comments

@sungwoncho
Copy link

sungwoncho commented Aug 25, 2016

Version: latest react-faux-dom (2.7.1) and React 15.1.0.

When I do:

    const node = ReactFauxDOM.createElement('svg');

    const svg = d3.select(node)
        .attr('width', width + margin.left + margin.right)
        .attr('height', h + margin.top + margin.bottom)
      .append('g')
        .attr('transform', `translate(${margin.left}, ${margin.top})`);
    ...

    svg.append('rect')
      .attr('width', width)
      .attr('height', h)
      .style('fill', 'none')
      .style('pointer-events', 'all')
      .on('mousemove', function() {
        const pos = d3.mouse(this);
        console.log(pos);
      });

pos is always [NaN, NaN].

I can confirm that this inside the mousemove callback is a DOM element (e.g. Element {nodeName: "rect", parentNode: Element, childNodes: Array[0], eventListeners: Object, text: ""…}).

Any ideas why?

@Olical
Copy link
Owner

Olical commented Aug 25, 2016

Are you sure your width and height are correct? width + margin.left + margin.right evaluates to a number and not NaN?

@Olical
Copy link
Owner

Olical commented Aug 25, 2016

Also, I'd try setting a breakpoint, maybe around here, and looking for anything suspicious. d3.mouse uses a function called point which accesses getBoundingClientRect, something that was added pretty recently.

@BeshoyLouka
Copy link

@sungwoncho I am using the d3 event for now:

...
      .on('mousemove', function() {
        console.log(d3.event.layerX, d3.event.layerY;);
      });
...

@sungwoncho
Copy link
Author

I will try again soon and close if not a bug.

@Olical
Copy link
Owner

Olical commented Sep 8, 2016

Any thoughts on this? Reproducible as a test?

@sungwoncho
Copy link
Author

@Olical I am currently not working on a feature involving this bug and have some other priorities. But I will get back to this one.

@Olical
Copy link
Owner

Olical commented Sep 12, 2016

Since it doesn't seem to be a widespread issue (maybe a rare edge case?) I'll close this for now. Feel free to open it again if you hit it. A test that reproduces it would be brilliant, but no pressure.

Thanks for the initial report anyway! I'll keep this in mind.

@Olical Olical closed this as completed Sep 12, 2016
@Xalio08
Copy link

Xalio08 commented Jun 1, 2017

I have a similar problem. It might be related to the fact that this is not a real d3 node.
d3.mouse(this) returns [NaN,NaN], whereas d3.mouse(d3.select('svg').select('g').node()) returns the correct value.
@BeshoyLouka workaround is good though.

@P1zz4br0etch3n
Copy link

This also breaks zoom support because x and y in the transform object in d3 zoom event are also NaN.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants