Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the speed (duration) of an anime.js event using a slider?

I am using anime.js to animate an element that is being bounced back in forth from the edges of its container. I want to be able to adjust the speed of this animation using a range slider that I have elsewhere on the page.

My problem is that while the duration is adjusted, it appears that the slider instantiates completely and does not continue animating to where it originally was suppposed to. I want it to go from the far left to the far right, but when I resize it the animation will only go from the place where it was resized to the end of the container.

These have been my attempts when calling the onchange method of my slider.

    function adjustSpeed() {
    alternate.duration = 300;  
    } 

and

function adjustSpeed() {
var alternate = anime({
    targets: '#alternate .el',
    translateX: width,
    direction: 'alternate',
    loop: true,
    duration: 300,
    easing: 'linear',
});

}

Any help would be very much appreciated.

like image 860
Zachary Jordan Avatar asked Sep 20 '25 18:09

Zachary Jordan


2 Answers

I just came across this issue and I've found a better yet not perfect solution. AnimeJS has another static speed property that is by default set to 1. If you change this speed, the animation speed changes, though the animation "jumps" and it doesn't look smooth.

For example, if you want the speed to be 0.5x the original speed, set anime.speed = 0.5.

I'll update this answer if I come up with a better solution.

like image 123
timetofly Avatar answered Sep 22 '25 07:09

timetofly


A dirty solution:

Manage frame manually by anime.tick combining with requestAnimationFrame, here're the demo:

https://codesandbox.io/s/anime-js-speed-adjustment-lm0ui?file=/src/index.js

The code is basically self-explained, if you have any further question, let me know.

like image 41
Losses Don Avatar answered Sep 22 '25 09:09

Losses Don



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!