使用raphael.js进行轨道动画?

时间:2013-03-11 14:32:20

标签: javascript raphael

raphael.js的新手,我正在寻找关于如何在轨道上围绕太阳移动行星的解释。我试图创建一条路径,并围绕它围绕圆圈的运动制作动画。

感谢您指出正确的方向!

2 个答案:

答案 0 :(得分:7)

我的朋友@Kevin Nielsen是对的,你会想要“getPointAtLength”。有一个很好的小Raphael函数here,它添加了一个.animateAlong()函数,虽然它需要一些修改来处理圆形对象。我把它剥夺了你的必需品。

假设你认识到1609年后的天文学,you'll want elliptical orbits。 (虽然短半径和长半径的差异实际上很小,这就是为什么哥白尼有点偏离标记。)但是你不能使用.ellipse()函数,因为你需要椭圆作为路径为了沿着它动画。请参阅elliptical arc的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);

现在你想在椭圆的一个焦点和沿着小径的月亮上绘制地球。我们将在perigee开始。

var focus = Math.pow(a*a - b*b, 0.5);

var palebluedot = paper.circle(center.x - focus, center.y, 25)
    .attr({
        stroke: 0,
        fill: "blue"    
    });

var moon = paper.circle(center.x - a, center.y, 10)
.attr({
    stroke: 0,
    fill: "#CCC"
});

这是你修改过的“animateAlong”功能:

//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);

jsFiddle

答案 1 :(得分:1)

@Chris Wilson的回答是正确的。

我需要的一个小修改是让动画无限重复。 @boom没有特别要求它,但我可以想象这可能是轨道动画的常见要求,这是我对Chris的.animateAlong()版本的修改:

Raphael.el.animateAlong = function(path, duration, repetitions) {
    var element = this;
    element.path = path;
    element.pathLen = element.path.getTotalLength();    
    duration = (typeof duration === "undefined") ? 5000 : duration;
    repetitions = (typeof repetitions === "undefined") ? 1 : repetitions;

    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});
    var anim = Raphael.animation({along: 1}, duration);
    element.animate(anim.repeat(repetitions)); 
};

请注意,我已经删除了缓动和回调参数(因为我不需要它们)并添加了repetitions参数,该参数指定了要执行的重复次数。

示例调用(启动无限循环轨道动画)是:

moon.animateAlong(orbit, 2000, Infinity);