如何在D3 js中连续旋转任何形状
how to rotate any shape continuously in D3 js?
我试图调用更新函数将文本旋转1度,一旦度再次达到360,旋转角度变为0,因此它将继续旋转。但我认为这不是解决问题的正确方法,也行不通。如果有人知道怎么做,请告诉我。
<script type="text/javascript" src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<script>
var width = 600;
var height = 300;
var holder = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
// draw the text
holder.append("text")
.style("fill", "black")
.style("font-size", "56px")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.attr("transform", "translate(300,150) rotate(0)")
.text("Hi, how r u doing");
// Initial starting angle of the text
for(var i=0;i<=360;i++){
update(i);
if(i==360){i=0;}
}
var n;
// update the element
function update(n) {
// rotate the text
holder.select("text")
.transition()
.duration(2000)
.attr("transform", "translate(300,150) rotate("+n+")");
}
</script>
</body>
</html>
for循环永远不会结束,因为您在计数器i
结束之前将其重置为0。如果删除这一行,代码将没有可见的结果,因为for循环执行得如此之快,在您看到任何内容之前它已经完成了。
一个更好的解决方案是使用setInterval
,例如
var width = 600;
var height = 300;
var holder = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
// draw the text
holder.append("text")
.style("fill", "black")
.style("font-size", "56px")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.attr("transform", "translate(300,150) rotate(0)")
.text("Hi, how r u doing");
// Initial starting angle of the text
var i = 0;
var timeInterval = 10;
setInterval(function(){
i += 1;
update(i % 360)
},timeInterval);
var n;
// update the element
function update(n) {
// rotate the text
holder.select("text")
.attr("transform", "translate(300,150) rotate("+n+")");
}
可以通过调节timeInterval
变量来控制转速
我在这里添加了一个示例JS Fiddle
相关文章:
- 知道为什么我的旋转木马不会自动更改图片吗
- 顺时针旋转Svg元件
- 将鼠标旋转限制为特定的度数
- 旋转后拖动对象
- 禁用旋转木马中的下一个按钮和上一个按钮
- 使用CI和CodeDeploy对node.js应用程序进行连续部署
- 手动创建旋转活动指示器
- 如何在容器中定位旋转的图像
- 连续两页返回Javascript
- 在轴上旋转SVG图像
- 绕z轴旋转时出现iOS陀螺仪错误
- 旋转按钮[HTML][JS]
- 使用 css3 或 js 的连续旋转动画
- 如何使图片连续旋转
- 在图像之间连续淡入淡出和旋转
- Netflix风格的图像连续旋转木马,鼠标悬停,支持Bootstrap
- 如何使这个旋转木马连续
- 如何在D3 js中连续旋转任何形状
- 引导API旋转木马连续
- 如何在jQuery动画中连续旋转子对象