Olical / react-faux-dom

DOM like structure that renders to React (unmaintained, archived)
http://oli.me.uk/2015/09/09/d3-within-react-the-right-way/
The Unlicense
1.21k stars 87 forks source link

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

Closed sungwoncho closed 8 years ago

sungwoncho commented 8 years ago

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 commented 8 years ago

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

Olical commented 8 years ago

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 commented 8 years ago

@sungwoncho I am using the d3 event for now:

...
      .on('mousemove', function() {
        console.log(d3.event.layerX, d3.event.layerY;);
      });
...
sungwoncho commented 8 years ago

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

Olical commented 8 years ago

Any thoughts on this? Reproducible as a test?

sungwoncho commented 8 years ago

@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 commented 8 years ago

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.

Xalio08 commented 7 years ago

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 commented 6 years ago

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