jQuery or CSS3 transitions? Speed comparison + brief empirical explanation.
css3.bradshawenterprises.com
css3.bradshawenterprises.com
A lot of this gain is likely from using requestAnimationFrame, but there could be other optimizations in D3, such as a single timer loop for all transitions. So, I think it's misleading to say you can't do fast transitions in JavaScript. It might just be a need to optimize jQuery.
But still, I definitely prefer to use CSS transitions when possible, because the performance is generally better and I like the declarative syntax.
http://stackoverflow.com/questions/8239235/smoothly-animate-...
But recently I went through Raphael's source an I saw an implementation on requestAnimationFrame, perhaps Mike can shed some light on what is different. Great work on d3.js by the way!
I'm hoping that if nothing else this post prompts the jQuery team to have a look at their animation function.
Some projects are already doing this:
http://www.benbarnett.net/2010/09/01/enhancing-jquerys-anima...
Sounds like it should go into mainline jquery pretty soon.
Keep in mind, the specs are rather large. When people talk about CSS "animations" there really talking about at least 3 specs, maybe 4. CSS 2D transforms, CSS 3D transforms, CSS transitions and CSS animation. There is a lot of work to get it right and tested in a nice JS wrapper, not to mention making it work with an existing JS based API.
The CSS transition is performing a single tween on a CSS rule; the div tags just inherit the result. The jQuery test is performing inline style tweens ... on every div! First of all you're performing redundant/duplicate calculations. Second of all, you're throwing away all performance gains you had by having shared css render style objects.
A better test would of been to update the style sheet via JS.
On an old laptop, changing the opacity, or set a different color of the same amount of divs takes about 50ms. The next timeout or interrupt, is on average 200ms later. The first screen draw is the slowest with 350ms.
I used absolute position for the divs, so that the browser doesn't have to recalculate the page after each update.
I agree though, this contrived example would surely work better with canvas.
From his explanation, browsers are smart enough to not only batch (how I'm thinking of it) css animations to all elements, but they also know what part of the screen will change and only have to redraw that.
The jQuery one just spends too much resources changing the properties of all those boxes to have any time to actually redraw the screen.
It also might get more people to realise that jQuery isn't the answer to everything, and animation is definitely not one of it's strong points (right now anyway!)
CSS3 transitions and keyframes are hardware accelerated and in most cases native to the browser.
Doing anything with javascript and the DOM is single threaded and expensive.
If you are using jQuery, look at cssHooks for nice fallbacks to js animations if you feature detect for particular effects.
Animations using javascript were great when we didn't have anything else, but nowadays they should only be used in browsers without support.
What would be really cool would be a DOM method that tied into the animation code that powers transitions.