Friday, September 21, 2012

Animating CSS3 image filters

Animating CSS3 image filters

In recent articles I've shown how to create cross-browser image filter effects with CSS and SVG: converting color photographs to black and white and sepia-tone, as well as blurring them. The next obvious step is to animate these effects.

Right now, these effects can be fully transitioned only in the most recent builds of Chrome; other browsers will show a "flick" between two filtered states, with no animation. As a rule, I don't show web development techniques until they are completely duplicable in at least two different browsers, but in this case, the effects are so neat (and expected to be fully supported in all browser versions so soon) that I'm making an exception.

You can see how your browser performs with animated filter effects in the example at the top of this article (a variation on my earlier "Animated Card Fan" effect, with individual transitions on each card when it is hovered over).

Greyscale to Color Image Transition

An obvious use of an animated filter is to transition an image from greyscale to color on mouseover, an ideal effect for a portfolio or image gallery.

The code is surprisingly simple: first, we set up the image:

<img src=roma.jpg alt=Roma id=roma class=colorshift>

Then turn it into greyscale, using the technique I discussed in the earlier article:

img#roma {
    -webkit-filter: grayscale(1);
    -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%);
    -ms-filter: grayscale(100%); -o-filter: grayscale(100%);
    filter: url(desaturate.svg#greyscale);
    filter: gray; filter: grayscale(100%); 

To transition the effect, we must set the greyscale filter to the opposite value for CSS3, and to none for other browsers:

img#roma:hover {
    -webkit-filter: grayscale(0);
    -webkit-filter: grayscale(0); -moz-filter: grayscale(0);
    -ms-filter: grayscale(0); -o-filter: grayscale(0);
    filter: none; filter: grayscale(0); 

To transition this effect smoothly, we must add CSS3. Since I'm doing this to several images, I'll make it a class:

img.colorshift {
    -webkit-transition: 1.2s all ease-in; -o-transition: 1.2s all ease-in;
    -moz-transition: 1.2s all ease-in; transition: 1.2s all ease-in;

In Chrome, you'll find that the black and white image transitions into color, in other current browsers, the change happens instantaneously.

Sharpening A Blurry Image With CSS3 Filters

Transitioning a blur takes very much the same approach: I've covered the "off" setting in the previous article, so I won't repeat it here.

Sepia Tone To Color Image

Colorizing a sepiatone image takes much the same approach as greyscale-to-color. The markup:

<img src=bike.jpg alt="Bike on a Roman street" id=bike class=colorshift>

The setup CSS:

img#bike {
    -webkit-filter: sepia(100%); -moz-filter: sepia(100%);
    -ms-filter: sepia(100%); -o-filter: sepia(100%);
    filter: url(sepia.svg#sepia); -webkit-filter: sepia(1);
    background-color: #5E2612; filter: alpha(opacity = 50);
    filter: sepia(100%); zoom:1;

The hover state CSS:

img#bike:hover {
    -webkit-filter: sepia(0); -moz-filter: sepia(0);
    -ms-filter: sepia(0); -o-filter: sepia(0); -webkit-filter: sepia(1);
    filter: alpha(opacity = 100); filter: sepia(0); zoom:1; filter: none;