Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requestAnimationFrame for begining a CSS transition

Does it make sense to request an animation frame for beginning a CSS transition?

For example, the Mozilla CSS transitions page includes a link to this jsfiddle example:

CSS:

#foo{
    border-radius:50px;
    width:50px;
    height:50px;
    background:#c00;
    position:absolute;
    top:0;
    left:0;
    -moz-transition: all 1s;
    -webkit-transition: all 1s;  
    -ms-transition: all 1s;  
    -o-transition: all 1s;  
    transition: all 1s;  
}

JavaScript:

var f = document.getElementById('foo');
document.addEventListener('click', function(ev){
    f.style.left = (ev.clientX-25)+'px';
    f.style.top = (ev.clientY-25)+'px';
},false);    

Does rewriting this example as follows make any sense? (See this jsfiddle)

JavaScript:

var rAF = window.mozRequestAnimationFrame ||
          window.webkitRequestAnimationFrame;
var f = document.getElementById('foo');
document.addEventListener('click', function(ev){
    rAF(function() {
        f.style.left = (ev.clientX-25)+'px';
        f.style.top = (ev.clientY-25)+'px';
    });
},false);

Thanks for any answer.

like image 625
erilem Avatar asked Oct 25 '12 20:10

erilem


People also ask

When should you use requestAnimationFrame?

requestAnimationFrame() is 1 shot. You should call this method whenever you're ready to update your animation onscreen. This will request that your animation function be called before the browser performs the next repaint.

How do you trigger transition CSS?

Triggering transitions You can trigger CSS transitions directly with pseudo classes like :hover (activates when the mouse goes over an element), :focus (activates when a user tabs onto an element, or when a user clicks into an input element), or :active (activates when user clicks on the element).

Why is requestAnimationFrame better than setInterval?

Another reason requestAnimationFrame is so useful is the fact that it gives you a time variable which you can use to calculate the amount of time between frames. This is not something that setInterval will do and since setInterval is not 100% accurate it is very possible your animation will get messed up over time.

How do you restart an animation in CSS?

The key to restarting a CSS animation is to set the animation-name of an animation to 'none' and then setting it back to the original animation. As you can see this requires two steps. One to switch it to a different animation and one to set it back.


2 Answers

I know question is old, but this may be useful for somebody.

rAF(requestAnimationFrame) is useless in this example. Because you are sliding element with css transition and calling rAF once. You have to loop prop changes with calling rAF frame by frame for optimize CPU,GPU performance by browser.

Main idea about rAF:

When you want to animate dom elements without css animations, you have to change elements' style properties (like width, height, top, left etc.) periodically.

Without rAF, you have to use setTimeout, setInterval functions to change props according to time. But browsers don't optimize these processes and it can be painful for CPU,GPU performance.

When you use rAF, modern browsers can optimize animation performance with less CPU, GPU usage.

Useful Links:

requestAnimationFrame for Smart Animating

requestAnimationFrame

like image 191
Cihan Tuncer Avatar answered Oct 13 '22 01:10

Cihan Tuncer


No it does not. In modern browsers all animations, CSS transitions, CSS animations, SMIL, element.animate are controlled by the same internal engine. Browsers will start your animation on the next available frame. RAF and native animate have different threads. RAF is meant for JavaScript animations. That rewrite will just wait few milliseconds until setting those changes. After that your animation will be controlled by the internal engine.

like image 39
Mircea Avatar answered Oct 13 '22 00:10

Mircea