I have a simple animation of a wheel spinning. I am trying to control the speed of the spinning wheel using a slider (input range). I have managed to do this, but every time I change the animation the animation restarts (it jumps). Looking for a solution to create a smooth increase of the speed. As the user increases the value of the slider, the wheel rotates with an increased speed.
In the code below, #loading is the spinning wheel.
$(document).on('input', '#slider', function() {
var speed = $(this).val();
$('#speed').html(speed);
$("#loading").css("animation-duration", 50 / speed + "s");
});
#loading {
position: absolute;
left: 0;
right: 0;
margin: auto;
transform-origin: 50% 50%;
animation: rotateRight infinite linear;
animation-duration: 0;
}
@keyframes rotateRight {
100% {
transform: rotate(360deg);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<img id="loading" src="//placehold.it/100">
<input type="range" min="0" max="100" value="0" class="slider" id="slider">
<p>Speed: <span id="speed"></span></p>
Yes, we can!
Here's the approach we'll take:
transform
value of the element, which is returned as amatrix()
matrix()
to arotate
value in degreesThe main issue we need to overcome is creating a new animation keyframes based on the current rotate value - we need to create a new animation with a starting value equal to the current rotate value, and an end value equal to the current rotate value + 360.
In other words, if our element is rotated
90deg
and the speed is changed, we need to create a new@keyframes
of:To create the dynamic animation I'm using the jQuery.keyframes plugin to create dynamic
@keyframes
.While this solution works, I don't believe it's super performant based on how the
jQuery.keyframes
plugin works. For every new animation keyframes the plugin appends an inline<style>
. This results in potentially dozens, hundreds, or even thousands ofkeyframes
being defined. In the example below I'm using thespeed
variable to create the@keyframe
names, so it will create up to 100 unique@keyframes
styles. There are a few optimizations we could make here but that's outside the scope of this solution.Here's the working example:
Classic Question
(with jumping.. now yet )
Version with jQuery
Work draft
save variable of currentanimation
http://www.w3.org/TR/css-animations-1/#interface-animationevent-attributes
I'm not entirely sure that this will be possible without a different approach that doesn't use CSS animations. The issue is that the animation does not normalize whenever you change the speed. It is always animating from 0% of the animation to 100% of the animation. Every time you adjust the
animation-duration
, you're going to re-interpolate using the current position in the animation.In other words, if you change from
animation-duration: 25
to50
att=12
, well the animation was halfway finished (180 degrees); now it's only a quarter finished (90 degrees). You can't controlt
though, that's the browser's. If you could, you would want to sett
to remain where it was in the interpolation, in this example,t=25
, so that you remain at the same percentage complete of the animation that you were, but you stretch the remaining time.I modified your script a little to try and show what I'm describing a little better. It will increment the speed by
0.25
every second between speed 0 and 5. You can kind of see how the problem is that the browser controlledt
is the issue.You can rewrite this in order to control
t
yourself with JavaScript, but I think you'll have to drop the CSS animations.To talk a little bit more to the point of this browser controlled
t
variable, take a look at this article on CSS-Tricks: Myth Busting: CSS Animations vs. JavaScriptThat's your problem, you want to be able to change the duration of your animation, but then also seek to the correct spot in the animation.
TL;DR
No, you can't (according to my tests)
Have you noticed that even when you move your slider from the initial position the box appears to start from a random position? that's because the animation loop has actually been running.
getComputedStyle(loadingElement).getPropertyValue('transform');
this will return a matrix which doesn't give you much just like that but we can calculate the angle of the rotation from that matrix:(using some maths explained here)
Now that we have this value we have to normalize it to have only positive values for the angle, and then we can apply this angle as the base value for
transform: rotate(Xdeg)
so far so good, you can see this working in the code snippet, however even when you do this, and increment/decrement the speed value, the animation loop is already running with a set time scale and you can't reset this loop.
My answer so far is so someone else with a deeper understanding of the animation loop can build from, and maybe come up with a working code.
If you are still reading this you might think, well just drop the animation with
loadingElement.style.removeProperty('animation')
and the assign it again, tried it doesn't work. And what about starting the animation again with a setInterval(...,0) so it runs in the next loop, won't work either.