- new event called
drag
d3.drag()
is similar tod3.zoom()
in the previous example
https://bl.ocks.org/codetricity/7ced6f3b3c95d64e7d7467e430716a63
var svg = d3.select("body").append('svg')
.attr('width', '400')
.attr('height', '400')
.style('border', 'solid 1px');
var circle = svg.append("circle")
.attr("cx", 50)
.attr("cy", 50)
.attr('r', 20);
var dragHandler = d3.drag()
.on('drag', dragged);
The function can have any name.
function dragged() {
var current = d3.select(this);
current
.attr('cx', d3.event.x)
.attr('cy', d3.event.y);
console.log(`${d3.event.x}, ${d3.event.y}`);
}
dragHandler(circle);
longer example is here.
Note that the color scheme in the longer example above is no longer valid as of d3 v5.