Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d3.behavior.drag + keydown event

I'm trying to create a basic D3 component with different user interactions. For instance the drag'n drop behavior should be enabled only if the user press Alt + left mouse button + drag mouse.

How can this be achieved?

like image 984
Sigma6 Avatar asked Dec 14 '25 00:12

Sigma6


1 Answers

You can do something like this:

var g = d3.select("body").append("svg")
.attr("width",200)
.attr("height",200).append("g")

var drag = d3.behavior.drag()
    .on("drag", dragmove);

function dragmove(d) {
  //check whether alt is pressed if not return
  if (!d3.event.sourceEvent.altKey)
    return;
  var x = d3.event.x;
  var y = d3.event.y;
  d3.select(this).attr("transform", "translate(" + x + "," + y + ")");
}

g.append("circle")
.attr("transform", "translate(100, 100)")
.attr("r",10)
//register drag to all circles.     
d3.selectAll("circle").call(drag);

Working code here

Hope this helps!

like image 155
Cyril Cherian Avatar answered Dec 16 '25 21:12

Cyril Cherian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!