gpt4 book ai didi

javascript - D3.js 向前和向后补间到特定点

转载 作者:行者123 更新时间:2023-11-28 04:47:39 28 4
gpt4 key购买 nike

我刚刚开始在 D3 中使用补间,一开始看起来很简单,但现在我正在努力在当前的代码中实现这一目标:

  • 补间到特定点,例如:从 0,0 到 lineData[4]
  • 从 lineData[4] 补间回到点 0,0

(可能使用按钮来控制)

这是我目前的代码:https://jsfiddle.net/uozko2bw/2/

//The data for our line
var lineData = [
{ "x": 1, "y": 5}, { "x": 20, "y": 20},
{ "x": 40, "y": 10}, { "x": 60, "y": 40},
{ "x": 80, "y": 5}, { "x": 100, "y": 60}];

var lineFunction = d3.line()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; })
.curve(d3.curveCardinal);

//The SVG Container
var svgContainer = d3.select("body").append("svg")
.attr("width", 200)
.attr("height", 200);

//The line SVG Path we draw
var lineGraph = svgContainer.append("path")
.attr("d", lineFunction(lineData))
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("fill", "none");

var circle = svgContainer.append("circle")
.attr("r", 5)
.attr("transform", "translate(" + lineData[0].x + "," + lineData[0].y +
")");

transition();

function transition() {
circle.transition()
.duration(10000)
.attrTween("transform", translateAlong(lineGraph.node()))
.each("end", transition);
}

function translateAlong(path) {
var l = path.getTotalLength();

return function(d, i, a) {
return function(t) {
var p = path.getPointAtLength(t * l);
return "translate(" + p.x + "," + p.y + ")";
};
};
}

最佳答案

interpolator 中的t通常从 0 到 1:

The returned interpolator will then be invoked for each frame of the transition, in order, being passed the eased time t, typically in the range [0, 1].

因此,您可以简单地调用另一个插值器,其中 t 从 1 变为 0:

var p = path.getPointAtLength((1-t) * l);

这是演示:

//The data for our line
var lineData = [
{ "x": 1, "y": 5}, { "x": 20, "y": 20},
{ "x": 40, "y": 10}, { "x": 60, "y": 40},
{ "x": 80, "y": 5}, { "x": 100, "y": 60}];

var lineFunction = d3.line()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; })
.curve(d3.curveCardinal);

//The SVG Container
var svgContainer = d3.select("body").append("svg")
.attr("width", 200)
.attr("height", 200);

//The line SVG Path we draw
var lineGraph = svgContainer.append("path")
.attr("d", lineFunction(lineData))
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("fill", "none");

var circle = svgContainer.append("circle")
.attr("r", 5)
.attr("transform", "translate(" + lineData[0].x + "," + lineData[0].y + ")");

transition();

/*
svgContainer.on("click", function() {
transition();
});*/

function transition() {
circle.transition()
.duration(10000)
.attrTween("transform", translateAlong(lineGraph.node()))
.on("end", transition2);
}

function transition2() {
circle.transition()
.duration(10000)
.attrTween("transform", translateAlong2(lineGraph.node()));
}

function translateAlong(path) {
var l = path.getTotalLength();

return function(d, i, a) {
return function(t) {
var p = path.getPointAtLength(t * l);
return "translate(" + p.x + "," + p.y + ")";
};
};
}

function translateAlong2(path) {
var l = path.getTotalLength();

return function(d, i, a) {
return function(t) {
var p = path.getPointAtLength((1-t) * l);
return "translate(" + p.x + "," + p.y + ")";
};
};
}
<script src="https://d3js.org/d3.v4.min.js"></script>

PS:在 D3 v4 中,是 .on("end"...,而不是 each("end"...

PPS:我的解决方案中有很多重复。我会给你做干作业。

关于javascript - D3.js 向前和向后补间到特定点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43217569/

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