简体   繁体   English

织物js动画速度变化,同时动画

[英]Fabric js animation speed change while animating

i want to animate object that starts slow speed, becoming faster, and then getting back to slow speed. 我想动画对象开始慢速,变得更快,然后回到慢速。 simulation of what i want to achive: 模拟我想要实现的目标:

sec: 1, duration: 5000, sec: 2, duration: 4000, 
sec: 3, duration: 3000, sec: 4, duration: 2000, 
sec: 5, duration: 1000, sec: 6, duration: 2000, 
sec: 7, duration: 3000, sec: 8, duration: 4000, 
sec: 9, duration: 5000

i have tried to following code: 我试过以下代码:

var rand = fabric.util.getRandomInt(4, 7);

function animateWheel(i) {
    if (i == rand)
        return;
    var _duration = (rand -i) * 1000;
    obj.animate('angle', 360 * i, {
        duration: _duration,
        onChange: canvas.renderAll.bind(canvas),
        onComplete: function () { animateWheel(i + 1); },
        easing: fabric.util.ease.easeOutCubic()
    });
}
animateWheel(1);

but the problem is that every animation start with speed 0 and there is "jumps" it is not smooth, please see code example: https://jsfiddle.net/o7ohgayw/ 但问题是每个动画都以速度0开始并且有“跳跃”它不顺畅,请参阅代码示例: https//jsfiddle.net/o7ohgayw/

There's a couple problems here - the main one (the one that probably kept you from figuring it out on your own) is that you're calling the easing function instead of referencing it. 这里有几个问题 - 主要问题(可能让你无法自己解决问题的那个问题)就是你正在调用缓动函数而不是引用它。 This is a very common mistake for beginners, especially in Javascript. 对于初学者来说这是一个非常常见的错误,特别是在Javascript中。

What most of your code is doing is reinventing the wheel, when Fabric already has nice easing functions built in to do what you want. 大多数代码正在做的是重新发明轮子,当Fabric已经内置了很好的缓动函数来做你想做的事情。

Try this instead ( fiddle here ): 试试这个( 在这里小提琴 ):

function animateWheel(i, j) {    
    obj.animate('angle', 360 * i, {
        duration: j,
        onChange: canvas.renderAll.bind(canvas),
        easing: fabric.util.ease.easeInOutCubic
    });
}

animateWheel(10, 2500);

Now animateWheel will take two parameters - how many times you want it to rotate, and the duration of the rotation. 现在animateWheel将采用两个参数 - 您希望它旋转多少次,以及旋转的持续时间。 Fabric will handle the rest. 面料将处理其余部分。 If you don't like the way it speeds up and slows down, experiment with the other In/Out Easing Functions . 如果您不喜欢它加速和减速的方式,请尝试其他进/出缓和功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM