Используя d3 и React, я нарисовал путь. На этом пути у меня есть несколько кругов, которые можно перетаскивать только по этому пути. Однако моя текущая реализация (вроде как) работает только тогда, когда на этом пути есть один круг.
(При dragStart он перемещается на длину 0 по пути независимо от положения, и всякий раз, когда я перетаскиваю второй круг, он начинается с позиции предыдущего круга).
Мой вопрос: как я могу перетащить несколько кругов (или мы) по пути в d3? Есть ли способ получить позицию currentLength на пути на основе cx и cy текущего круга?
var currentLength = 0;
class MyComponent extends Component {
constructor(props) {
super(props)
currentLength = 0;
}
componentDidMount() {
var drag = d3.behavior.drag()
.on('drag', this.move);
var g = d3.select(this._base);
var circle = g.selectAll('circle').data(this.props.data);
var onEnter = circle.enter();
onEnter.append('circle')
.attr({
r: 10,
cx: (d) => d.x,
cy: (d) => d.y
})
.style('fill', 'blue')
.call(drag);
}
move(d) {
currentLength += d3.event.dx + d3.event.dy
if (currentLength < 0) {
currentLength = 0
}
var pointAtCurrentLength = d3.select('#path').node().getPointAtLength(currentLength)
this.cx.baseVal.value = pointAtCurrentLength.x;
this.cy.baseVal.value = pointAtCurrentLength.y;
}
render() {
return <g ref={(c)=>this._base=c}></g>
}
}
Что-то похожее на это, только с возможностью перетаскивания и несколькими кругами: http://bl.ocks.org/mbostock/1705868< /а>
move
к своему классу, поэтому назначение this.cy/this.cx, скорее всего, не сработает. - person Michael Camden   schedule 07.07.2016