椭圆路径上的圆的svg动画
svg animation of a circle on an elliptical path
我正在尝试使用svg动画在椭圆路径上旋转圆,我尝试的代码是在圆形路径上旋转它,而不是在我制作的椭圆上,有人能在这个plz上帮助我吗??
<!DOCTYPE html>
<html>
<head>
<title>JavaScript SVG Animation</title>
<style>
/* CSS here. */
</style>
<script>
/* CONSTANTS */
var initialTheta = 0; // The initial rotation angle, in degrees.
var thetaDelta = 0.3; // The amount to rotate the square every "delay" milliseconds, in degrees.
var delay = 10; // The delay between animation stills, in milliseconds. Affects animation smoothness.
var angularLimit = 360; // The maximum number of degrees to rotate the square.
var cx = 200; // circle center
var cy = 150; //circle center
/* GLOBALS */
var theCircle; // Will contain a reference to the square element, as well as other things.
var timer; // Contains the setInterval() object, used to stop the animation.
var pauseEvent = false;
function init()
{
theCircle = document.getElementById("s1"); // Set this custom property after the page loads.
theCircle.currentTheta = initialTheta; // The initial rotation angle to use when the animation starts, stored in
timer = setInterval(doAnim, delay); // Call the doAnim() function every "delay" milliseconds until "timer" is cleared.
}
function doAnim()
{
if (theCircle.currentTheta > angularLimit)
{
clearInterval(timer); // The square has rotated enough, instruct the browser to stop calling the doAnim() function.
return; // No point in continuing; stop now.
}
theCircle.setAttribute("transform", "rotate(" + theCircle.currentTheta + "," + cx + "," + cy +")"); // Rotate the square by a small amount. around given circle point
theCircle.currentTheta += thetaDelta; // Increase the angle that the square will be rotated to, by a small amount.
}
window.onload = function(){
var elplay = document.getElementById("play");
elplay.addEventListener("click", function(){
if(!pauseEvent){
init();
doAnim();
} else{
init();
doAnim();
pauseEvent = false;
}
}, false);
var elstop = document.getElementById("stop");
elstop.addEventListener("click", function(){
theCircle.currentTheta = 0 //initialTheta; // The initial rotation angle to use when the animation starts, stored in
theCircle.setAttribute("transform", "rotate(" + theCircle.currentTheta + "," + cx + "," + cy +")"); // Rotate the square by a small amount. around given circle point
clearInterval(timer); // The square has rotated enough, instruct the browser to stop calling the doAnim() function.
return; // No point in continuing; stop now.
}, false);
var elpause = document.getElementById("pause");
elpause.addEventListener("click", function(){
initialTheta = theCircle.currentTheta;
pauseEvent = true;
clearInterval(timer); // The square has rotated enough, instruct the browser to stop calling the doAnim() function.
return; // No point in continuing; stop now.
}, false);
}
</script>
</head>
<body>
<button id = "play" style="position: absolute;">Play</button>
<button id = "pause" style="position: absolute;left: 65px;">Pause</button>
<button id = "stop" style="position: absolute;left: 135px;">Stop</button>
<svg width="800px" height="800px" viewBox="0 0 800 800">
<g transform="translate(200, 150)">
<ellipse id = "s2" cx = "200" cy = "150" rx = "200" ry = "150" fill = "salmon" stroke = "black" stroke-width = "3"/>
<circle id = "s1" cx = "250" cy = "10" r = "20" fill = "yellow" stroke = "black" stroke-width = "3"/>
</g>
</svg>
</html>
我相信它没有遵循你的路径的原因是因为你的数学有点偏离。你画的是一个椭圆,但你的旋转数学是圆形的。要么纠正你的数学,要么更好地遵循Phrogz的建议,学习SMIL。
这里有一些SVG可以让你开始:
<html>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" width="600" height="600">
<path id="orbit" d="M200 400 A200,200 0 1,1 600,400 A200,200 0 1,1 200,400" fill = "salmon" stroke = "black" stroke-width = "3" />
<circle r = "20" fill = "yellow" stroke = "black" stroke-width = "3">
<animateMotion begin="0s" dur="12s" repeatCount="indefinite" >
<mpath xlink:href="#orbit"/>
</animateMotion>
</circle>
</svg>
</html>
请注意,必须使用路径对象,并且椭圆必须至少由两条圆弧构成。
相关文章:
- 一旦放置在网页中,就无法播放SVG动画
- javascript循环svg动画
- 如何获取SVG动画的当前时间/位置
- 如果不使用HTML按钮,就无法通过编程方式触发SVG动画
- IE9设置SVG动画时的怪异行为
- 只需添加一个类即可实现SVG动画
- Safari上的SVG动画工件
- 如何调整 SVG 动画的大小
- 捕捉.svg动画
- 捕捉 SVG 动画以减慢 FPS 的速度
- 在水平滚动上显示 SVG 动画
- 只有当我使用 vivus.js 到达底部时,我的 svg 动画才会开始
- 完全旋转 1 次后暂停 SVG 动画
- SVG 动画在加载设置超时时不会启动
- 更新 SVG 动画
- 在 SVG 动画中围绕舞台原点以外的其他位置旋转路径
- SVG 动画问题(缩放时 svg 模糊)
- 对齐 svg 动画分组路径
- SVG 动画在以编程方式设置样式时有效,但在仅使用样式表时无效
- svg 动画从动态值转换