- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
This Mike Bostock 的例子对我很有用。它显示了一个点沿着曲线闭合路径无限流动。代码非常简单,除了最后几行我不明白。我在下面写下相关代码:
transition();
function transition() {
circle.transition()
.duration(10000)
.attrTween("transform", translateAlong(path.node()))
.each("end", transition);
}
// Returns an attrTween for translating along the specified path element.
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 + ")";
};
};
}
为什么会出现三个嵌套函数?我知道外层函数translateAlong
应该返回一个t
(时间)的函数,但是我不明白中间函数(d, i ,一)
。我只测试过它不能被省略,但它的参数可以。
最佳答案
我们有 3 个函数。让我们命名它们的级别:
function translateAlong(path) {//1st level here
var l = path.getTotalLength();
return function(d, i, a) {//2nd level here
return function(t) {//3rd and last level
var p = path.getPointAtLength(t * l);
return "translate(" + p.x + "," + p.y + ")";
};
};
}
为了回答您的问题,了解这...非常重要
.attrTween("transform", translateAlong(path.node()))
... 将调用 translateAlong
立即 并获取其返回值。这是哪个:
function(d, i, a) {//2nd level here
return function(t) {//3rd and last level
var p = path.getPointAtLength(t * l);
return "translate(" + p.x + "," + p.y + ")";
};
};
现在,让我们转到 D3 v3 API (您链接中的版本)。它说:
transition.attrTween(name, tween)
Transitions the value of the attribute with the specified name according to the specified tween function. The starting and ending value of the transition are determined by tween; the tween function is invoked when the transition starts on each element, being passed the current datum d, the current index i and the current attribute value a, with the this context as the current DOM element. (emphasis mine)
正如您在我以粗体显示的部分中的参数名称所看到的(即使 Bostock 从不使用它们,这也解释了您的正确观察“它的参数可以[被省略]”) ,补间函数正是您要询问的函数:
return function(d, i, a) {//this is the tween
return function(t) {
var p = path.getPointAtLength(t * l);
return "translate(" + p.x + "," + p.y + ")";
};
};
值得一提的是,考虑到 JavaScript 闭包的工作方式,变量 l
(var l = path.getTotalLength();
) 可由补间函数访问。
关于javascript - 了解为什么在 .attrTween 中使用三个嵌套函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57440953/
我正在基于示例 here 在 d3 v3 中创建旭日形.我无法理解为什么 attrTween() 在以下情况下不起作用。 path.transition() .duration(750) .a
我正致力于通过 d3.svg.arc 函数创建“带”。我知道 .transition()没有很好的默认弧插值器,所以我使用 attrTween 定义了自己的插值器.这是我的代码的相关部分: var b
我试图模仿 Mike Bostock 的例子 Extending Arcs .我的代码与 Mike 的非常相似,但我的代码与他的不同。 下面是 JavaScript 代码: E
我正在尝试创建动画弧形图。我已经能够创建我想要的动画类型,但我想添加延迟。 我使用 .attrTween 方法创建动画。 这是我的延迟尝试代码的主要部分: svg.selectAll(".arcs")
This Mike Bostock 的例子对我很有用。它显示了一个点沿着曲线闭合路径无限流动。代码非常简单,除了最后几行我不明白。我在下面写下相关代码: transition(); function
我正在尝试理解 attrTween。我知道进行此方形移动的简单方法就是使用 attr,但该示例的目的是让我了解 attrTween。下面的例子没有做任何事情,但是控制台也没有出现js错误,所以我不确定
我正在尝试在 d3 上创建一个接口(interface)。我找到了圆弧动画代码(http://bl.ocks.org/mbostock/5100636),我想稍微改变一下(没有函数 arcTween(
我想了解如何在 D3 中使用函数 attrTween。我正在尝试根据以下示例实现饼图:http://bl.ocks.org/mbostock/5100636 . 但是当我到达过渡部分时遇到了问题。
我有一个关于 attrTween 的问题(有时是 tween())。 我将自定义补间函数理解为 在“attrTween('d' ”参数之后,我定义了自定义函数。因此,我编写了如下自定义函数。
我正在尝试将以下圆环图从 d3.js v3 升级到 d3.js v4。 http://bl.ocks.org/dbuezas/9306799 我已经用新版本中的等效方法替换了所有旧版本的方法名称。 d
我是一名优秀的程序员,十分优秀!