为什么Raphael.js的动画设置到不正确的坐标

Why is Raphael.js animating to incorrect coordinates?

本文关键字:不正确 坐标 设置 动画 Raphael js 为什么      更新时间:2023-09-26

我试图在Raphael.js中制作一个简单的动画,将paper.text对象从当前位置移动到另一个位置。以下是我的一些代码(太多了,无法全部发布):

songPos = getSongPosition(this, charIndex);
letter.path.animate({x: songPos.x, y: songPos.y, "font-size": this.correctFontSize}, 500, onAnimationComplete);

这是上面代码中引用的Letter对象:

function Letter(args)
{
    this.letter = args.letter || "A";
    this.correct = args.correct || false;
    this.transformation = args.transformation || "";
    this.initX = args.x || 0;
    this.initY = args.y || 0;
    this.path = null;
}
Letter.prototype.buildPath = function()
{
    this.path = paper.text(this.initX, this.initY, this.letter);
    if(this.transformation)
    {
        this.path.transform(this.transformation);
    }
    return this;
};

问题是,我正在打印getSongPosition返回的xy值,它们是正确的,但animate方法将我的文本对象发送到屏幕外的某个地方。我也尝试过将动画属性设置为{x: 0, y: 0},但仍然得到了相同的结果。如果有必要,我可以发布更多的代码。

如有任何帮助,我们将不胜感激。

更新1:我的程序的一部分要求我能够将对象移动到特定的坐标。一些物体会被旋转,而另一些则不会,所以我写了这样的文章:

Letter.prototype.getMoveTransform = function(x, y)
{
    var bbox = this.path.getBBox(true);
    var dx = x - bbox.x;
    var dy = y - bbox.y;
    return "T" + dx + "," + dy + (this.transformation == null ? "" : this.transformation);
};

我相信这是我问题的根源。该函数用于生成将旋转对象移动到(x,y)所需的变换。我不知道为什么每次动画都要重新旋转对象。

更新2:我不知怎么解决了我的问题。我会发布我的解决方案,但我不明白为什么这些一开始就有效/不起作用了。

this.path.getBBox(true);应为this.path.getBBox()或this.path.getBBox)(false);

每次都需要得到变换后的位置来计算dx和dy