Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 animations with transform causes blurred elements on Webkit

Tags:

So I've got some native elements (divs) with various effects applied to them (border-radius, box-shadow and transform: scale()). When I animate them, two weird things happen:

  1. Even though I'm not trying to animate the scale, if I don't put the scale in the animation, it is ignored.
  2. When I put the scale in the animation, Webkit blurs the elements

See the example here: http://jsfiddle.net/trolleymusic/RHeCL/ - the buttons at the bottom will trigger the issues.

The first issue happens in Firefox too, so I'm guessing that it's because that's how the animation spec is supposed to work. Not what I wanted, but ok, I'll live with it.

The second issue is just weird. I know it's to do with 3d transform because if I (just for testing purposes) declare -webkit-perspective or -webkit-transform-style: preserve-3d; on the circle elements, it causes the blur issue as well. My confusion is that I'm not trying to transform the z index as all, and I have also tried the animations using purely translateY instead of translate.

It happens in Chrome (18), Chrome Canary (20) and Safari (5.1.2 & 5.1.4).

So, am I right in what I think is happening? And how can I avoid the blurriness?

Worst-case scenario: I can just use different sizes for the elements instead of scaling them, that's not really a problem - but I thought this would be a more elegant solution and now this issue has cropped up.

like image 733
Trolleymusic Avatar asked May 02 '12 16:05

Trolleymusic


People also ask

Do you need WebKit for animation?

WebKit-based browsers (including Opera 15 and later) still require the -webkit- prefix for animations today, and transforms are only unprefixed in recent versions of Chrome. You will need the prefix for both features.

Does CSS animation affect performance?

The fact is that, in most cases, the performance of CSS-based animations is almost the same as JavaScripted animations — in Firefox at least. Some JavaScript-based animation libraries, like GSAP and Velocity. JS, even claim that they are able to achieve better performance than native CSS transitions/animations.

Are CSS animations good?

CSS animations are great for websites that want to add dynamic, engaging content without placing much more weight on the page. Since they don't require extra scripts, CSS animations are unlikely to slow down your pages.


1 Answers

Refer to this answer as to why it's blurring the element: https://stackoverflow.com/a/4847445/814647

Summary of the above: WebKit is taking the original size/CSS before animating, and treating it as an image, THEN scales it up, producing the blurriness.

Solution: Make initial size the largest scale you're going to, and start it initially with a lower scale (so in your case you'd want to up the size by 5, and set the initial scale to 0.2)

UPDATE

The reason it ignores the current scale from what I understand is because you're not specifically setting JUST the translate (I'm looking up the CSS for it now). When you run -webkit-animation, it's resetting all your current transforms (scale), so you need to make sure that you have your scales in there. I'm looking up the css to change so it only changes just the position:

like image 169
Josh Allen Avatar answered Sep 29 '22 12:09

Josh Allen