function circleDragv2(width, height, radius) {
function onDragStart(event, d) {
const circle = d3.select(this).select('circle');
const tooltip = d3.select(this).select('text');
circle
.raise()
.attr('fill', 'yellow')
.attr('stroke', 'black');
tooltip
.attr('opacity', 1)
.attr('x', d.x)
.attr('y', d.y - radius - 5)
.text(`(${Math.round(d.x)}, ${Math.round(d.y)})`);
}
function onDrag(event, d) {
d.x = Math.max(radius, Math.min(width - radius, event.x));
d.y = Math.max(radius, Math.min(height - radius, event.y));
const circle = d3.select(this).select('circle');
const tooltip = d3.select(this).select('text');
circle
.attr('cx', d.x)
.attr('cy', d.y);
tooltip
.attr('x', d.x)
.attr('y', d.y - radius - 5)
.text(`(${Math.round(d.x)}, ${Math.round(d.y)})`);
}
function onDragEnd(event, d) {
const circle = d3.select(this).select('circle');
const tooltip = d3.select(this).select('text');
circle
.attr('fill', d.color)
.attr('stroke', 'none');
tooltip.attr('opacity', 0);
}
const drag = d3.drag()
.on('start', onDragStart)
.on('drag', onDrag)
.on('end', onDragEnd);
return drag;
}