d3.js - how to drag element with mouse move -


after appending or creating new circle on drag, want able drag circle around. tried following code using .call(d3.behavior.drag()...) don't know why isn't working

preview: http://jsbin.com/vabowofiqe/1/edit?html,output

code:

    var svgcontainer = d3.select("body").append("svg")                             .attr("width", 800)                             .attr("height", 803);                      //draw 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");                      function move() {                         d3.select(this)                                 .attr('x', d3.event.x)                                 .attr('y', d3.event.y);                     };                      var drag = d3.behavior.drag()                             .origin(function ()                             {                                 var t = d3.select(this);                                 return {x: t.attr("x"), y: t.attr("y")};                             })                              .on('dragend', function (d) {                                 var mousecoordinates = d3.mouse(this);                                 if (mousecoordinates[0] > 170) {                                     //append new element                                     var newcircle = 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")  //calling drag behavior after clonning         .call(                                                     d3.behavior.drag()                                                     .on('drag', move).origin(function () {                                                 var t = d3.select(this);                                                 return {x: t.attr("cx"), y: t.attr("cy")};                                             }));                                     ;                                 }                             });                     circle.call(drag); 

a svg circle element doesn't have x , y properties, cx , cy properties can used position them. should change move function accordingly.

function move() {     d3.select(this)         .attr('cx', d3.event.x)         .attr('cy', d3.event.y); }; 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -