D3.js 动画旋转 [英] D3.js animate rotation
问题描述
当我尝试使用 D3.js 库正确执行旋转动画时遇到问题.问题与我想要旋转元素的点有关.
I'm having trouble when I try to correctly execute a rotation animation using the D3.js library. The problem has to do with the point at which I want to rotate the element about.
这是我用来展示我的意思的小提琴(慢动作):http://jsfiddle.net/74mCb/
Here is a fiddle I made to show what I mean (in slow motion): http://jsfiddle.net/74mCb/
问题的根源似乎在这里:
It seems like the source of the problem lies here:
.attr("transform", "rotate(-60, 150,130)");
然后我像这样旋转它:
.attr("transform", "rotate(40 150,130)");
我希望针尖保持在原位(作为旋转中心),有人可以解释一下我做错了什么吗?
I would like the butt of the needle to stay in position (to be the center of rotation), could someone please explain what I am doing wrong?
谢谢!
推荐答案
这有点难掌握(我自己也不是很明白)但是 D3 需要一些帮助来知道如何在代表您的两个字符串之间进行插值旋转.
This is a bit tricky to grasp (I don't fully understand it myself) but D3 needs some help knowing how to interpolate between the two strings that represent your rotation.
function turnNeedle()
{
needle
.transition()
.duration(2000)
.attrTween("transform", tween);
function tween(d, i, a) {
return d3.interpolateString("rotate(-60, 150, 130)", "rotate(60, 150, 130)");
}
}
d
是数据,i
是索引,a
是属性,以防你想让这个数据驱动.
d
is the datum, i
is the index, a
is the attribute in case you want to make this data-driven.
这篇关于D3.js 动画旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!