javascript - Reset position in dragended event -


i'm trying reset position of group if it's not dragged in dropzone.

it works when try drag second time, group moves dragged @ first.

i think it's related origin.

here's code , fiddle

   function init(){        var drag = d3.behavior.drag()                     .origin(function (d) { return d; })                     .on("dragstart", dragstarted)                     .on("drag", dragged)                     .on("dragend", dragended);         entities = svg.selectall("g")                      .data([{ x: 750, y: 100 }])                      .enter()                      .append("g")                      .attr("class","entity-group")                      .attr("transform", function (d) { return "translate(" + d.x + "," + d.y + ")"; })                      .attr("initial-x", function (d) { return d.x })                      .attr("initial-y", function (d) { return d.y })                      .call(drag);    }     function dragstarted(d) {         d3.event.sourceevent.stoppropagation();         d3.select(this)           .classed("dragging", true);     }      function dragged(d) {         d.x += d3.event.dx;         d.y += d3.event.dy;          d3.select(this)            .attr("transform", function (d) { return "translate(" + d.x + "," + d.y + ")" });     }      function dragended(d) {         d3.select(this)           .classed("dragging", false);          if (d.x > dropzonewidth) {             var entity = d3.select(this);             var x = entity.attr("initial-x");             var y = entity.attr("initial-y");              entity.attr("transform", function (d) { return "translate(" + x + "," + y + ")"; });         }     } 

i have tried

d3.behavior.drag()            .origin(function (d) {                    var t = d3.select(this);                    return {                             x: d3.transform(t.attr("transform")).translate[0],                             y: d3.transform(t.attr("transform")).translate[1]                         };                     }) 

the problem += in drag handler. i'd use d3.mouse against parent container:

function dragged(d) {     var coors = d3.mouse(this.parentnode);     d.x = coors[0];     d.y = coors[1];      d3.select(this)        .attr("transform", function (d) { return "translate(" + d.x + "," + d.y + ")" }); } 

full code:

<!doctype html>  <html>      <head>      <script data-require="d3@3.5.3" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>      <link rel="stylesheet" href="style.css" />      <script src="script.js"></script>    </head>      <body>      <script>      var dropzonewidth = 200;            var svg = d3.select('body')        .append('svg')        .attr('width', 500)        .attr('height', 500)                    function init(){         var drag = d3.behavior.drag()                      .origin(function (d) { return d; })                      .on("dragstart", dragstarted)                      .on("drag", dragged)                      .on("dragend", dragended);           entities = svg.selectall("g")                            .data([{ x: 100, y: 100 }])                            .enter()                            .append("g")                            .attr("class","entity-group")                            .attr("transform", function (d) { return "translate(" + d.x + "," + d.y + ")"; })                            .attr("initial-x", function (d) { return d.x })                            .attr("initial-y", function (d) { return d.y })                            .call(drag);                                                      entities                            .append('circle')                            .style('fill', 'black')                            .attr('r', 15)     }       function dragstarted(d) {          d3.event.sourceevent.stoppropagation();          d3.select(this)            .classed("dragging", true);      }        function dragged(d) {          var coors = d3.mouse(this.parentnode);          d.x = coors[0];          d.y = coors[1];            d3.select(this)             .attr("transform", function (d) { return "translate(" + d.x + "," + d.y + ")" });      }        function dragended(d) {          d3.select(this)            .classed("dragging", false);            if (d.x > dropzonewidth) {              var entity = d3.select(this);              var x = entity.attr("initial-x");              var y = entity.attr("initial-y");                entity.attr("transform", function (d) { return "translate(" + x + "," + y + ")"; });          }      }                init();              </script>    </body>    </html>


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -