如何将drop元素拖入svg组

时间:2015-07-13 13:26:29

标签: d3.js

左边有几个圆圈,右边有一个svg组。我想在拖放时将圆圈拖入svg组。将元素放入svg组后,应将其附加到组中。

到目前为止,我已创建了一个可拖动的元素和可拖动的组,但无法找到相同的任何文章。

左边有几个圆圈,右边有一个svg组。我想在拖放时将圆圈拖入svg组。将元素放入svg组后,应将其附加到组中。

到目前为止,我已创建了一个可拖动的元素和可拖动的组,但无法找到相同的任何文章。

演示:http://jsbin.com/wowunoluza/1/edit?html,js,output

<!doctype html>
<html>
    <head>
        <title>Editor</title>
        <meta http-equiv="x-ua-compatible" content="ie=9"/>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
        <link rel="stylesheet" href="<%=request.getContextPath()%>/style.css" />
        <script type="text/javascript">
            window.onload = function ()
            {
                var svgContainer = d3.select("body").append("svg")
                        .attr("width", 800)
                        .attr("height", 803);

                var sidebar = svgContainer.append("rect")
                        .attr("x", 0)
                        .attr("y", 43.5)
                        .attr("width", 69)
                        .attr("height", 620)
                        .attr("stroke-width", 2)
                        .attr("stroke", "#7E7E7E")
                        .style("fill", "none");

                var rect = svgContainer.append("rect")
                        .attr("x", 10)
                        .attr("y", 50)
                        .attr("width", 51)
                        .attr("height", 41)
                        .attr("rx", 10)
                        .attr("stroke-width", 2)
                        .attr("stroke", "#7E7E7E")
                        .style('cursor', 'move')
                        .style("fill", "none");

                //Draw the Circle
                var circle = svgContainer.append("circle")
                        .attr("cx", 35)
                        .attr("cy", 145)
                        .attr("r", 25)
                        .style("stroke-opacity", .9)
                        .style("stroke", "green")
                        .style("stroke-width", 2)
                        .style('cursor', 'move')
                        .style("fill", "white");

                var circle2 = svgContainer.append("circle")
                        .attr("cx", 35)
                        .attr("cy", 225)
                        .style("stroke-opacity", .9)
                        .style("stroke-width", 2)
                        .style("stroke", "red")
                        .style("fill", "white")
                        .style('cursor', 'move')
                        .attr("r", 25);

                var circle3 = svgContainer.append("circle")
                        .attr("id", "circleToClone")
                        .attr("cx", 35)
                        .attr("cy", 310)
                        .attr("r", 25)
                        .style("fill", "white")
                        .style("stroke-width", 2)
                        .style('cursor', 'move')
                        .style("stroke", "#CDB483");

                var dragGroup = d3.behavior.drag()
                        .origin(function () {
                            var g = this;
                            return {x: d3.transform(g.getAttribute("transform")).translate[0],
                                y: d3.transform(g.getAttribute("transform")).translate[1]};
                        })
                        .on("drag", function (d, i) {

                            g = this;
                            console.log(g);
                            translate = d3.transform(g.getAttribute("transform")).translate;
                            x = d3.event.dx + translate[0],
                                    y = d3.event.dy + translate[1];
                            d3.select(g).attr("transform", "translate(" + x + "," + y + ")");
                            d3.event.sourceEvent.stopPropagation();
                        });

                var group = svgContainer.append("g")
                        .attr("id", "mygroup")
                        .call(dragGroup)
                        .style('cursor', 'move')
                        .attr("transform", "translate(20, 20)");

                group.append("rect")
                        .attr("x", 250)
                        .attr("y", 250)
                        .attr("width", 151)
                        .attr("height", 141)
                        .attr("rx", 10)
                        .attr("stroke-width", 2)
                        .attr("stroke", "#7E7E7E")
                        .style("fill", "white");

                group.append("circle")
                        .attr("cx", 330)
                        .attr("cy", 330)
                        .attr("r", 25)
                        .style("fill", "white")
                        .style("stroke-width", 1)
                        .style("stroke", "red");


                var drag = d3.behavior.drag()
                        .origin(function ()
                        {
                            var t = d3.select(this);
                            return {x: t.attr("cx"), y: t.attr("cy")};
                        })

                        .on('dragend', function (d) {
                            var mouseCoordinates = d3.mouse(this);
                            if (mouseCoordinates[0] > 170) {
                                //Append new element
                                var circle2 = d3.select("svg").append("circle")
                                        .classed("drg", true)
                                        .attr("cx", 100)
                                        .attr("cy", 100)
                                        .attr("r", 20)
                                        .attr("cx", mouseCoordinates[0])
                                        .attr("cy", mouseCoordinates[1])
                                        .style("fill", "white")
                                        .style("stroke-width", 2)
                                        .style("stroke", "#CDB483");
                            }
                        });
                circle.call(drag);
                circle2.call(drag);
                circle3.call(drag);
            };
        </script>
    </head>
    <body>
        <div id="container">
            <div id="header" style="margin-bottom: 0;">
                <h1 id="title">Editor</h1>
                <div id="footer"></div>
            </div>
        </div>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

当&#34;掉线&#34;您可以使用d3.transform获取g元素的当前翻译,然后检查鼠标坐标是否位于组中的矩形内。

然后你可以在group附加一个新的圆圈,其中的坐标由鼠标的坐标和群组的平移确定,并且它的笔划由圆圈的笔划决定被拖到小组的。

所以你的.on("dragend"函数现在应该是这样的:

var mouseCoordinates = d3.mouse(this);
var groupTransform = d3.transform(group.attr("transform"));
var groupX = groupTransform.translate[0];
var groupY = groupTransform.translate[1];
var rect = group.select("rect");
var rectX = +rect.attr("x");
var rectY = +rect.attr("y");
var rectWidth = +rect.attr("width");
var rectHeight = +rect.attr("height");

if (mouseCoordinates[0] > groupX + rectX 
    && mouseCoordinates[0] < groupX + rectX + rectWidth
    && mouseCoordinates[1] > groupY + rectY
    && mouseCoordinates[1] < groupY + rectY + rectHeight) {
    //Append new element
    var newCircle =   group.append("circle")
        .classed("drg", true)
        .attr("cx", mouseCoordinates[0] - groupX)
        .attr("cy", mouseCoordinates[1] - groupY)
        .attr("r", 20)
        .style("fill", "white")
        .style("stroke-width", 2)
        .style("stroke", d3.select(this).style("stroke"));
}

您可以更改许多内容并添加到代码中以使其更好,但这应该可以帮助您入门。