gpt4 book ai didi

javascript - 沿 d3 中的路径拖动

转载 作者:行者123 更新时间:2023-12-05 02:20:41 25 4
gpt4 key购买 nike

我使用 d3 和 React 绘制了一条路径。在这条路径上,我有多个圆圈,只能沿着那条路径拖动。但是,我当前的实现仅(某种程度上)在该路径上有一个圆圈时起作用。

(在 dragStart 上,无论位置如何,它都会移动到路径上的长度 0,并且每当我拖动第二个圆圈时,它都会从前一个圆圈的位置开始)。

我的问题是:如何在 d3 中沿着一条路径拖动多个圆圈(或 w.e)?有没有办法根据当前圆的cx和cy获取路径上的currentLength位置?

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

最佳答案

这是对 this example 的快速修改,这使得圆圈可拖动:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

path {
fill: none;
stroke: #000;
stroke-width: 1.5px;
}

line {
fill: none;
stroke: red;
stroke-width: 1.5px;
}

circle {
fill: red;
}

</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>

var points = [[600,276],[586,393],[378,388],[589,148],[346,227],[365,108]];

var width = 960,
height = 500;

var line = d3.svg.line()
.interpolate("cardinal");

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

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

var path = svg.append("path")
.datum(points)
.attr("d", line);

var line = svg.append("line");

var circle = svg.append("circle")
.attr("transform", "translate(" + points[0] + ")")
.attr("r", 7)
.call(drag);

svg.append("circle")
.attr("transform", "translate(" + points[5] + ")")
.attr("r", 7)
.call(drag);

function dragged(d) {
var m = d3.mouse(svg.node()),
p = closestPoint(path.node(), m);

d3.select(this)
.attr("transform", "translate(" + p[0] + "," + p[1] + ")")
}

function closestPoint(pathNode, point) {
var pathLength = pathNode.getTotalLength(),
precision = 8,
best,
bestLength,
bestDistance = Infinity;

// linear scan for coarse approximation
for (var scan, scanLength = 0, scanDistance; scanLength <= pathLength; scanLength += precision) {
if ((scanDistance = distance2(scan = pathNode.getPointAtLength(scanLength))) < bestDistance) {
best = scan, bestLength = scanLength, bestDistance = scanDistance;
}
}

// binary search for precise estimate
precision /= 2;
while (precision > 0.5) {
var before,
after,
beforeLength,
afterLength,
beforeDistance,
afterDistance;
if ((beforeLength = bestLength - precision) >= 0 && (beforeDistance = distance2(before = pathNode.getPointAtLength(beforeLength))) < bestDistance) {
best = before, bestLength = beforeLength, bestDistance = beforeDistance;
} else if ((afterLength = bestLength + precision) <= pathLength && (afterDistance = distance2(after = pathNode.getPointAtLength(afterLength))) < bestDistance) {
best = after, bestLength = afterLength, bestDistance = afterDistance;
} else {
precision /= 2;
}
}

best = [best.x, best.y];
best.distance = Math.sqrt(bestDistance);
return best;

function distance2(p) {
var dx = p.x - point[0],
dy = p.y - point[1];
return dx * dx + dy * dy;
}
}

</script>

关于javascript - 沿 d3 中的路径拖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38249275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com