demosthenes.info

I’m Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with , and . To receive more information, including news, updates, and tips, you should follow me on Twitter.

featured articles

popular favourites

Florence, ItalyAncient Aqueduct, ItalyBike on a Roman Street

Animating CSS3 Image Filters

In recent articles I’ve shown how to create cross-browser image with and : 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 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:

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

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

  1. img#roma {
  2. -webkit-filter: grayscale(1);
  3. -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%);
  4. -ms-filter: grayscale(100%); -o-filter: grayscale(100%);
  5. filter: url(desaturate.svg#greyscale);
  6. filter: gray; filter: grayscale(100%); 
  7. }

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

  1. img#roma:hover {
  2. -webkit-filter: grayscale(0);
  3. -webkit-filter: grayscale(0); -moz-filter: grayscale(0);
  4. -ms-filter: grayscale(0); -o-filter: grayscale(0);
  5. filter: none; filter: grayscale(0); 
  6. }

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

  1. img.colorshift {
  2. -webkit-transition: 1.2s all ease-in; -o-transition: 1.2s all ease-in;
  3. -moz-transition: 1.2s all ease-in; transition: 1.2s all ease-in;
  4. }

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:

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

The setup CSS:

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

The hover state CSS:

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

You must be signed up in order to leave comments.

web developer guide

featured comment

by Aisling Brock in New Business Card Design

what i'm reading

A Feast for Crows: A Song of Ice and Fire: Book Four
A Feast for Crows: A Song of Ice and Fire: Book Four

what i'm watching

Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]
Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]

what i'm playing

Borderlands
Borderlands

what i'm hearing

Planets
Planets

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.