为什么我的 Raphael JS 动画不循环播放?

发布于 2024-10-01 06:23:14 字数 548 浏览 9 评论 0原文

你好呀 我使用 Raphael 框架制作了这个动画。我希望星星(logoStar)无限旋转,但它只运行一次。有人可以帮忙吗?谢谢

window.onload = function () {
buildLogo();
}

var buildLogo = function () {
    var logo = Raphael("title",800,236);

    var logoStar = logo.path("M12.245 131.057L16.039 138.743L24.521 139.974L18.383 145.958L19.832 154.406L12.245 150.418L4.658 154.406L6.108 145.958L-0.03 139.974L8.452 138.743").attr({fill:"#fff",stroke:"none"});

    var starSpin = function () {
        logoStar.animate({rotation: "360"}, 5000, starSpin);
    }
    starSpin();
}

Hi there
I've made this animation using the Raphael framework. I want the star (logoStar) to spin indefinitely but it only runs once. Can anyone help? Thanks

window.onload = function () {
buildLogo();
}

var buildLogo = function () {
    var logo = Raphael("title",800,236);

    var logoStar = logo.path("M12.245 131.057L16.039 138.743L24.521 139.974L18.383 145.958L19.832 154.406L12.245 150.418L4.658 154.406L6.108 145.958L-0.03 139.974L8.452 138.743").attr({fill:"#fff",stroke:"none"});

    var starSpin = function () {
        logoStar.animate({rotation: "360"}, 5000, starSpin);
    }
    starSpin();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

酒浓于脸红 2024-10-08 06:23:14
var starSpin = function () {
    logoStar.attr({rotation: 0}).animate({rotation: 360}, 5000, starSpin);
}

从360°到360°的动画看起来没有动画,所以之前需要将旋转重置为零。

var starSpin = function () {
    logoStar.attr({rotation: 0}).animate({rotation: 360}, 5000, starSpin);
}

Animation from 360° to 360° looks like there no animation, so you need to reset rotation to zero before.

故事未完 2024-10-08 06:23:14

自从版本 2 推出以来,拥有无限循环动画的真正方法是这样的:

var spin = Raphael.animation({transform: "r360"}, 2500).repeat(Infinity);
myElement.animate(spin);

Infinity 是一个 Javascript 中的属性,因此不要将其作为字符串输入。

这是一个有效的小提琴

Since version 2 came out, the real way to have an infinitely looping animation is this:

var spin = Raphael.animation({transform: "r360"}, 2500).repeat(Infinity);
myElement.animate(spin);

Infinity is a property in Javascript, so don't enter it as a string.

Here's a working fiddle.

﹉夏雨初晴づ 2024-10-08 06:23:14

只是一个快速观察:

从版本 2 开始,“旋转”似乎不再是 Atrr 的一部分,因此您不能在“动画”中使用它,但您可以将其替换为“transform:”r“+(某种程度上)"..

例如:

 element.animate( {transform: "r" + (-90)}, 2000, 'bounce');

Just a quick observation:

Looks like "Rotation" isn't part of the Atrr anymore since ver 2, so you can't use it in "animate", but you can replace that with "transform: "r" + (some degree)"..

eg:

 element.animate( {transform: "r" + (-90)}, 2000, 'bounce');
寂寞清仓 2024-10-08 06:23:14

我同意 Michael Mao 的观点,你应该加入

logoStar.animate({rotation: "360"}, 5000, starSpin);

一个循环。

但像 while(true) 这样的循环并不总是一个好主意。它会占用太多 CPU,并且某些浏览器可能会警告用户脚本导致页面运行缓慢。也许最好在重新执行动画之前添加超时。尝试看看什么最有效;)

I agree with Michael Mao, you should put

logoStar.animate({rotation: "360"}, 5000, starSpin);

in a loop.

But loops like this while(true) are not always a good idea. It will take too much CPU and some browsers may warn the user that a script is causing the page to run slowly. Maybe it is best to add a timeout before re-executing animate. Just try and see what works best ;)

生死何惧 2024-10-08 06:23:14

我不完全确定,但我认为当您尝试在匿名函数中使用starSpin时,它尚未定义。

尝试将:

var starSpin = function () {
    logoStar.animate({rotation: "360"}, 5000, starSpin);
}
starSpin();

更改为

function starSpin() {
    logoStar.animate({rotation: "360"}, 5000, starSpin);
}
starSpin();

编辑
事实证明,根本不是这样的——我不知道问题是什么(希望对拉斐尔图书馆有更多了解的人可以提供帮助),但是一个kludgey-but-work的解决方案是在调用回调中的整个构造函数之前清除有问题的元素(我警告过你这是杂乱的)。

参见: http://jsfiddle.net/rbf5x/1/

I'm not entirely sure, but I think that starSpin is not yet defined when you try to use it in the anonymous function.

Try changing:

var starSpin = function () {
    logoStar.animate({rotation: "360"}, 5000, starSpin);
}
starSpin();

to

function starSpin() {
    logoStar.animate({rotation: "360"}, 5000, starSpin);
}
starSpin();

EDIT
It turns out that that's not it at all -- I do not know what the problem is (hopefully someone who has more exposure to the Raphael library can help out there) but a kludgey-but-works solution is to clear the element in question before calling the entire construct function in the callback (I warned you it was kludgey).

SEE: http://jsfiddle.net/rbf5x/1/

春花秋月 2024-10-08 06:23:14

我不太确定,但是如果你这样做怎么办:

while(true){starSpin();}

听起来该方法只执行一次,所以它“旋转”一次......

I am not very sure but what if you do this:

while(true){starSpin();}

It sounds like the method is only executed once, so it "spins" once...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文