Javascript 使用raphael.js动态观察动画?
raphael.js的新成员,我正在寻找一个关于如何在轨道上围绕太阳移动行星的解释。我一直在尝试创建一条路径,并为围绕它的圆的运动设置动画Javascript 使用raphael.js动态观察动画?,javascript,raphael,Javascript,Raphael,raphael.js的新成员,我正在寻找一个关于如何在轨道上围绕太阳移动行星的解释。我一直在尝试创建一条路径,并为围绕它的圆的运动设置动画 谢谢你给我指明了正确的方向 我的朋友@Kevin Nielsen是对的,你会想要getPointAtLength。有一个很好的小拉斐尔函数,它添加了一个.animateAlong函数,尽管它需要一些修改来处理圆形对象。我把它剥去做你的必需品 假设你认识1609年后的天文学。虽然短半径和长半径的差异在现实中非常小,这就是为什么哥白尼有点不正确。但是您不能使用.
谢谢你给我指明了正确的方向 我的朋友@Kevin Nielsen是对的,你会想要getPointAtLength。有一个很好的小拉斐尔函数,它添加了一个.animateAlong函数,尽管它需要一些修改来处理圆形对象。我把它剥去做你的必需品 假设你认识1609年后的天文学。虽然短半径和长半径的差异在现实中非常小,这就是为什么哥白尼有点不正确。但是您不能使用.ellipse函数,因为您需要椭圆作为路径,以便沿它设置动画。请参阅SVG规范,或者尝试一系列组合,直到它看起来正确为止,就像我所做的那样:
var paper = Raphael("canvas", 500, 500);
var center = {x: 200, y: 100 };
var a = 100;
var b = 80;
//see http://www.w3.org/TR/SVG/paths.html#PathDataEllipticalArcCommands
var ellipse = "M" + (center.x - a) + "," + center.y + " a " + a + "," + b + " 0 1,1 0,0.1";
var orbit = paper.path(ellipse);
现在你想在椭圆的一个焦点上画地球,沿着轨道画月亮。我们将在晚上开始
下面是修改后的AnimateLong函数:
//Adapted from https://github.com/brianblakely/raphael-animate-along/blob/master/raphael-animate-along.js
Raphael.el.animateAlong = function(path, duration, easing, callback) {
var element = this;
element.path = path;
element.pathLen = element.path.getTotalLength();
duration = (typeof duration === "undefined") ? 5000 : duration;
easing = (typeof easing === "undefined") ? "linear" : duration;
//create an "along" function to take a variable from zero to 1 and return coordinates. Note we're using cx and cy specifically for a circle
paper.customAttributes.along = function(v) {
var point = this.path.getPointAtLength(v * this.pathLen),
attrs = {
cx: point.x,
cy: point.y
};
this.rotateWith && (attrs.transform = 'r'+point.alpha);
return attrs;
};
element.attr({along: 0 }).animate({along: 1}, duration, easing, function() {
callback && callback.call(element);
});
};
这是:
moon.animateAlong(orbit, 2000);
我的朋友凯文·尼尔森是对的,你会想要getPointAtLength。有一个很好的小拉斐尔函数,它添加了一个.animateAlong函数,尽管它需要一些修改来处理圆形对象。我把它剥去做你的必需品 假设你认识1609年后的天文学。虽然短半径和长半径的差异在现实中非常小,这就是为什么哥白尼有点不正确。但是您不能使用.ellipse函数,因为您需要椭圆作为路径,以便沿它设置动画。请参阅SVG规范,或者尝试一系列组合,直到它看起来正确为止,就像我所做的那样:
var paper = Raphael("canvas", 500, 500);
var center = {x: 200, y: 100 };
var a = 100;
var b = 80;
//see http://www.w3.org/TR/SVG/paths.html#PathDataEllipticalArcCommands
var ellipse = "M" + (center.x - a) + "," + center.y + " a " + a + "," + b + " 0 1,1 0,0.1";
var orbit = paper.path(ellipse);
现在你想在椭圆的一个焦点上画地球,沿着轨道画月亮。我们将在晚上开始
下面是修改后的AnimateLong函数:
//Adapted from https://github.com/brianblakely/raphael-animate-along/blob/master/raphael-animate-along.js
Raphael.el.animateAlong = function(path, duration, easing, callback) {
var element = this;
element.path = path;
element.pathLen = element.path.getTotalLength();
duration = (typeof duration === "undefined") ? 5000 : duration;
easing = (typeof easing === "undefined") ? "linear" : duration;
//create an "along" function to take a variable from zero to 1 and return coordinates. Note we're using cx and cy specifically for a circle
paper.customAttributes.along = function(v) {
var point = this.path.getPointAtLength(v * this.pathLen),
attrs = {
cx: point.x,
cy: point.y
};
this.rotateWith && (attrs.transform = 'r'+point.alpha);
return attrs;
};
element.attr({along: 0 }).animate({along: 1}, duration, easing, function() {
callback && callback.call(element);
});
};
这是:
moon.animateAlong(orbit, 2000);
@克里斯·威尔逊的答案在金钱上是正确的 我需要的一个小小的修改就是让动画无限期地重复@boom并没有特别要求它,但我可以想象这可能是轨道动画的常见要求,下面是我对Chris版本的.animateAlong的修改: 请注意,我删除了easing和callback参数,因为我不需要它们,并添加了repetitions参数,该参数指定要执行的重复次数 启动无限循环动态观察动画的示例调用如下:
月球.动画长轨道,2000,无限 @Chris Wilson的答案在金钱上是正确的 我需要的一个小小的修改就是让动画无限期地重复@boom并没有特别要求它,但我可以想象这可能是轨道动画的常见要求,下面是我对Chris版本的.animateAlong的修改: 请注意,我删除了easing和callback参数,因为我不需要它们,并添加了repetitions参数,该参数指定要执行的重复次数 启动无限循环动态观察动画的示例调用如下:
月球.动画长轨道,2000,无限 找到正确的道路将是一个具有挑战性的部分。一旦有了这些函数,就可以很容易地使用getTotalLength和getPointAtLength函数,这两个函数都是专门设计用于处理路径元素以查找路径中特定点的元素方法。看看这个:获得正确的路径将是一个挑战性的部分。一旦有了这些函数,就可以很容易地使用getTotalLength和getPointAtLength函数,这两个函数都是专门设计用于处理路径元素以查找路径中特定点的元素方法。看看这个:真不敢相信这竟然连一张赞成票都没有!真棒的解释+提琴,先生。我不得不提到,提琴的轨道不是变速轨道,但它应该是。真不敢相信这甚至没有得到一次投票!真棒的解释+小提琴,先生。我不得不提到小提琴的轨道不是变速的,但它应该是。