简体   繁体   English

d3js缩放+拖动导致冲突

[英]d3js zoom + drag causes conflict

I am trying to apply the zoom behaviour to a svg where certain elements in the svg are already bound with drag behaviour. 我正在尝试将缩放行为应用于svg,其中svg中的某些元素已经绑定了拖动行为。 without the zoom behaviour drag works fine, without the drag behaviour zoom works fine. 如果没有缩放行为,则拖动效果很好,如果没有拖动行为,则缩放效果很好。 When I have both of them it conflicts. 当我同时拥有它们时,就会发生冲突。 When I drag a circle all the other circles starts to drag with it. 当我拖动一个圆时,所有其他圆都开始随之拖动。

Here is the fiddle . 这是小提琴

can anyone help me to figure out the issue? 谁能帮我解决这个问题?

<svg height="600" width="600" style="background: black">

<g>
    <rect x="0" y="0" , width="600" height="40" style="fill:gold;"></rect>
    <circle id='drag' cx="15" cy="20" init-cx="15" init-cy="20" r="10"
            style="stroke: white; stroke-width: 2px; fill:blue"/>

</g>

<g id="playground">
    <g>
        <circle class='top' cx="180" cy="120" r="30" style="stroke: white; stroke-width: 2px; fill:white"/>
    </g>
    <g>
        <circle class='top' cx="200" cy="220" r="30" style="stroke: white; stroke-width: 2px; fill:white"/>
    </g>
    <g>
        <circle class='top' cx="320" cy="150" r="50" style="stroke: white; stroke-width: 2px; fill:white"/>
    </g>
</g>

var zoom = d3.behavior.zoom()
        .scaleExtent([-1, 8])
        .on("zoom", function () {

            var graph = d3.select('svg');

            graph
                .select('#playground')
                .selectAll('circle')
                .select(function () {
                    return this.parentNode;
                })
                .attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
        });


    var move = d3.behavior.drag()

        .on('drag', function () {

            console.log('dragging');

            var curr = d3.select(this)
                .attr({
                    cx: d3.mouse(this)[0],
                    cy: d3.mouse(this)[1]
                })


        })
        .on('dragend', function () {

            var curr = d3.select(this);

            d3.select('#playground')
                .append('circle')
                .attr({
                    cx: curr.attr('cx'),
                    cy: curr.attr('cy'),
                    r: curr.attr('r')
                })
                .style({
                    fill: 'white',
                    stroke: 'red',
                    'stroke-width': '2px'
                })
            ;

            curr.attr({
                cx: curr.attr('init-cx'),
                cy: curr.attr('init-cx')
            });
        })
        ;


    d3.select('#drag').call(move);

    d3.select('.top')
        .call(d3.behavior.drag().on('drag', function () {
            d3.select(this)
                .attr({
                    cx: d3.mouse(this)[0],
                    cy: d3.mouse(this)[1]
                })
            ;
        }));
    d3.select('svg').call(zoom);

I have implemented individual dragging of nodes working along with overall zooming and panning functionality. 我已经实现了单个拖动节点以及整体缩放和平移功能。 Used stopPropagation of the event in dragstart event of circles. 在圆的dragstart事件中使用了事件的stopPropagation

Hope this helps. 希望这可以帮助。

var move = d3.behavior.drag()
    .on("dragstart", function() {
        d3.event.sourceEvent.stopPropagation();
    });

Working JSFiddle 工作中的JSFiddle

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

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