【问题标题】:How to use Raphael .animateWith()如何使用拉斐尔 .animateWith()
【发布时间】:2013-04-08 18:46:39
【问题描述】:

有没有人在 Raphael 中使用 .animateWith() 成功地保持快速动画同步?它的记录很差。该库的创建者有一个video demonstration,但我找不到代码。

我有一个 Javascript 节拍器,它由一条线(节拍器的臂)和一个梯形“重量”组成,该重量最终会上下移动以表示速度。我有一个工作小提琴here,有问题的行是:

        var ticktock = Raphael.animation({
            "50%": { transform:"R20 " + x + "," + y, easing: "sinoid", callback: function() { tick(this, repeats, done); }},
            "100%": { transform:"R-20 " + x + "," + y, easing: "sinoid", callback: function() { tick(this, repeats, done); }}
        }, interval).repeat(repeats / 2);
        arm.animate(ticktock);
        weight.animateWith(arm, ticktock, ticktock);    

如果您检查小提琴并给它一个高节奏和大约 20 个滴答声,您应该会看到重量滞后于手臂。 (如果不是,请尝试几次——墨菲定律等。)我认为这正是 animateWith() 所阻止的。可能是我用错了。

如果您查看 .animateWith() 函数的 Raphael source,您会看到它同步了每个动画 FWIW 的 .start 参数。

【问题讨论】:

    标签: javascript animation raphael


    【解决方案1】:

    来自Raphael documentation:

    参数:

    element - 要与之同步的 [object] 元素

    anim - 要与之同步的 [object] 动画

    animation - [object] 动画对象,参见 Raphael.animation

    所以我认为您的代码只需要分离 动画 并将其传递给.animateWith() 的第二个参数:

    动画部分只是:

    {
        "50%": { transform:"R20 " + x + "," + y, easing: "sinoid", callback: animationDone },
        "100%": { transform:"R-20 " + x + "," + y, easing: "sinoid", callback: animationDone }
    } 
    

    所以你可以这样做:

    var animationDone = function() { 
        tick(this, repeats, done); 
    };
    
    var ticktockAnimationParam = {
        "50%": { transform:"R20 " + x + "," + y, easing: "sinoid", callback: animationDone },
        "100%": { transform:"R-20 " + x + "," + y, easing: "sinoid", callback: animationDone }
    };
    var ticktock = Raphael.animation(ticktockAnimationParam, interval).repeat(repeats / 2);
    arm.animate(ticktock);
    weight.animateWith(arm, ticktockAnimationParam, ticktock);    
    

    见小提琴:http://jsfiddle.net/wDwsW/7/

    仅供参考,我将回调函数移到了外面,而不是使用匿名函数。

    【讨论】:

    • 感谢您的关注。恐怕我在 Chrome 中仍然会在高频率时出现相当严重的错位——尝试每分钟 280 次节拍。该文档对我来说意义不大。该函数的源似乎与某些动画数组中动画的“开始”属性相匹配。也许它只是不起作用?
    • @ChrisWilson 为我工作顺利。也使用 Chrome。我的机器是 3.2Ghz Intel i5,在 OSX 10.8.3 上运行 8gb RAM
    • 我不是 100% 肯定 animateWith() 工作完美,但接受和奖励赏金 bc 这似乎是正确的用法,这是问题所在。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多