简体   繁体   English

d3拖动事件中的XY坐标正在反转

[英]The XY coordinates in a d3 drag event are inverting

I have a force layout with the following structure: 我的部队布局如下:

<svg width="300" height="220">
    <g class="scaleWrapper" transform="scale(0.3)">
        <g class="transformWrapper" transform="translate(-110, -80)">
           <g class="backgroundWrapper">
               <rect class="backgroundRect" width="300" height="220"></rect>
           </g>
           <g class="forceNode"></g>
           <g class="forceNode"></g>
           <g class="forceLink"></g>
        </g>
    </g>
</svg>

I also have a drag behavior linked to the rect to drag it around (scaling is handled through a separate slider). 我还具有与rect关联的拖动行为,以将其四处拖动(缩放通过单独的滑块处理)。

        let transformElement = d3.select('.transformWrapper');
        let svgBackground = transformElement.append('g')
            .classed('backgroundWrapper', true);

        function originFunction() {
            let d = d3.select('.transformWrapper');
            return {
                x: d.attr('x'),
                y: d.attr('y')
            };
        }

        let svgDrag = d3.behavior.drag()
            .origin(originFunction)
            .on('dragstart', function(){
                d3.event.sourceEvent.stopPropagation();
            })
            .on('drag', function(){
                transformElement.attr("transform", `translate(${d3.event.x}, ${d3.event.y})`);
            });

        svgBackground.call(svgDrag);

It mostly works, but it jumps around as i drag it. 它通常可以工作,但是随着我拖动它会跳来跳去。 I did a log and saw that the d3.event object is alternating between relative XY coordinates and absolute ones, here's a sample of what I'm seeing: 我做了一个日志,发现d3.event对象在相对XY坐标和绝对XY坐标之间交替,这是我所看到的示例:

-111 -80
-29 -6
-110 -80
-29 -5

I don't see any other elements that have behavior bound to them. 我看不到其他任何绑定有行为​​的元素。 All the d3.event objects have the 'drag' type property and the same srcElement. 所有d3.event对象均具有'drag'类型属性和相同的srcElement。 How can I silence the events that are returning the relative positions? 如何使返回相对位置的事件静音?

Found the problem, it's the same bug as in d3.event.y has strange values during drag behavior 发现了问题,它与d3.event.y中的错误相同, 在拖动行为期间具有奇怪的值

Moving the drag behavior to the transform group fixed it right up. 将拖动行为移至变换组可将其正确固定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM