Say Hello to Webkit Filters

Say Hello to Webkit Filters

Tutorial Details

Earlier this month, a new specification, Filter Effects 1.0, was released. It presents some exciting new Photoshop-like effects that we can use in the browser. Even better, Webkit has already landed support (in the nightlies)!


According to the Spec…

“A filter effect is a graphical operation that is applied to an element as it is drawn into the document. It is an image-based effect, in that it takes zero or more images as input, a number of parameters specific to the effect, and then produces an image as output.”

Now, at least at this point, I wouldn’t presume to be able to show you everything that’s possible with these new filters. I’m still learning them myself! That said, I’ll show you a handful of the new filters, how we can use them in our projects, and then, hopefully, we can all brainstorm and learn from each other within the comments. Let’s get started.

Filters are typically associated with images (though they can also be applied to video). As such, for the handful of demos below, we’ll be using the Nettuts+ logo as input.

 Nettuts+ Logo
Nettuts+ Logo

Remember: these effects aren’t yet available in the public releases of Webkit browsers. For now, download Canary when testing these demos.


hue-rotate

Ever played around with the Hue/Saturation panel in Photoshop? Well now you can play around with it in the browser.

img {
   -webkit-filter: hue-rotate(50deg);
}

If specifying this value in degrees seems confusing, just imagine a color wheel. The number of degrees you specify determines where that wheel stops. This means, that 0deg won’t do a thing, while 50deg will turn the dial, accordingly.

In this case, the Nettuts+ logo will take on a blu-ish hue.

Or, let’s say that you want your image to continuously change colors. Likely, in a real-world project, the color transitions will be far more subtle, but for this demo, we’ll be a bit obnoxious.

img {
   -webkit-animation: adjustHue 1s alternate infinite;
}
    
@-webkit-keyframes adjustHue {
   0% { -webkit-filter: hue-rotate(30deg); }
   50% { -webkit-filter: hue-rotate(60deg); }
   100% { -webkit-filter: hue-rotate(90deg); }   
}

Simple enough. View Demo


grayscale

We’ve used a variety of hacks in the past to transition an image from black and white to color in the browser. One technique calls for two images stacked on top of one another. Another option is to use canvas. Or… we can use the grayscale filter.

img {
   -webkit-filter: grayscale(100%);
}

When applying a percentage to the grayscale function, just think to yourself, “On a scale of 0 to 100%, how gray do I want this image to be?

When used in tandem with CSS3 transitions, we can apply a nice and clean hover effect.

img {
  -webkit-transition: -webkit-filter 1s;
}
img:hover {
    -webkit-filter: grayscale(100%); 
}

In the future, you’ll want to provide prefixes for the other browsers, however, it’s not necessary at this point. No need in applying Mozilla transitions to accomodate for a filter that’s only implemented in Webkit (so far).

View Demo


sepia

Enjoy the sepia-flavored Instagram effect? Let’s see what Nettuts+ looked like in the old west.

img {
      -webkit-filter: sepia(100%);
}

Typically, though, this effect is applied to photos. Let’s see how the greatest artist who ever lived looks in sepia.

Excellent.


blur

By passing a radius, we can blur an image in the browser with ease.

img {
      -webkit-filter: blur(2px);
}

Or by upping the blur radius to 50px.


brightness

We use the brightness filter to specify…wait for it…how bright the input image should appear.

img {
      -webkit-filter: brightness(15%);
}

Think of 100% as home base. brightness(100%) keeps the image unchanged. As we reduce this percentage, however, the image will continue to darken.

Don’t forget: you can combine all of these filters.

img {
      -webkit-filter: brightness(60%) sepia(100%);
}

contrast

We can now adjust the contrast of an image quite easily.

img {
      -webkit-filter: contrast(200%);
}

Once again, think of 100% as resting position. We can then reduce or increase this value to adjust the contrast of the image. According to the spec, applying a value of 0% should make the image 100% black, similar to what you might expect from -webkit-filter: brightness(0%);. However, I’m seeing more of a dark gray.

img {
      -webkit-filter: contrast(0%);
}

Now if we up the percentage considerably, to 2000%:

img {
      -webkit-filter: contrast(2000%);
}

Just for fun, let’s create a throbbing Matrix version of the Nettuts+ logo. We’ll combine CSS3 animations and filters.

img {
    -webkit-animation: bluePill 1s alternate infinite;
}
    
@-webkit-keyframes bluePill {
   0% { -webkit-filter: contrast(2000%); }
   100% { -webkit-filter: contrast(100%); }
}

View Demo


invert

Mac users: press Control + Option + Command + 8. Notice how it inverts your screen (of course you noticed). I use this trick late at night when I’m reading on the computer, and my eyes are sore.

By applying a percentage of 100 to the new invert filter, we can achieve the exact same effect.

img {
      -webkit-filter: invert(100%);
}

Note that 0% will leave the image unchanged.

Now, you could technically apply this to, say, the body of your website, and it would work. However, you’ll notice considerable slow down, and lose the ability to scroll the page. AKA – Don’t do it, except for fun.


saturate

In addition to setting grayscale(100%), we could also achieve a similar effect by desaturating the image entirely.

In this case, 100% is the unchanged state, at which point you can either decrease or increase this value. As such, reducing this value to 0% should remove all color from the image.

img {
      -webkit-filter: saturate(0%);
}

Or, by upping the value to 700%:


That’s All For Now

Stay tuned to this article over the course of the next week. As these techniques are still super new, we all need time to figure out how to use them. I’ll update this article as I learn more!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Ryan

    Shut up Jeffrey Way, you choad.

    • http://www.jeffrey-way.com Jeffrey Way
      Author

      Thanks, Ryan.

  • http://blake.ws blake

    Nice touch with the Bieber pic Jeffrey lol

  • http://dave-mate.com Muhammad Asif

    Wow it’s is pretty nice feature I wish all browser take it soon (even IE) so it’s really comfort me with hover effects.

  • Chris O.

    Hey Jeff,

    Great article. Thanks for Bieber-rolling us too… That never gets old around here.

  • http://www.websanjal.com Kismat Konnection

    Thanks for the article, it really helped me………… Thank You once again

  • http://dave-mate.com Muhammad Asif

    Hi, Jeffrey Way It won’t work for me, I mean I check your examples they won’t work! Well I using Chrome 18.something (latest) but Seem it’s not working!

    • http://www.diaryofanappdeveloper.com Duncan McDougall

      He explains on line 2 that this is just the nightly build of Chrome at this stage. You’ll have to download Chrome Canary to test them in the wild.

      http://tools.google.com/dlpage/chromesxs

  • http://ianrathbone.com Ian Rathbone (@ianrathbone)

    Worked in chrome 18.0.1025.162 m for me.

    I can think of a couple of real world situations I’d use this already. Thanks for sharing!

  • http://lol.com lol

    I suppose you hate Justin Bieber but you simply advertising him to be more popular

    • http://www.jeffrey-way.com Jeffrey Way
      Author

      Thanks

  • http://www.marianarlt.de Marian Arlt

    Plain awesome! This brings so many new possibilities on all levels, some of we probably can’t even imagine yet. Really would love to see implementation soon. Tho I can already see a return of the 90s color-chaos on some sites haha…
    And the fallback is completely not to worry about either (except for design/layout reasons, mind that everybody!)

  • Dice

    Thanks Ryan!

  • Claudio

    Yeah, good examples! Thanks, but next time avoid to use pictures of omosexual like

    http://d2o0t5hpnwv4c1.cloudfront.net/1111_filters/justin-bieber.jpg

  • Anonymous

    Next time use normal picture. Thanks for your tutorial. It kinda sux when you use like this pictures. I don’t hate anyone… But use something more natural like nature :P.

  • http://www.netsi.dk/wordpress Sten Hougaard

    Hi Jeffrey,
    I made a simple webapp based on Google AngularJS to illustrate in an interactive way, these nice CSS3 filters Your and the blog post by Aaron Lumsden (http://www.aaronlumsden.com/articles/css3-filters/) write about.

    I am fascinated by all the features provided in CSS3 to manipulate the rendering of elements, something which few years ago would only be possible in programs like Adobe Photoshop :-)

    Find the webapp here: http://dl.dropbox.com/u/3260327/angular/CSS3ImageManipulation.html

    /Sten

  • http://ondeweb.in Shreyo

    any idea to make these effects work on other browsers?

  • http://renwalker.com Ren Walker

    So filter + border-radius = okay. BUT…border-radius on a containing div + filter on the image = not working.

    Real world example:

    <img src="http://placekitten.com/500/600&quot; class="round_image2"/>

    <a href="#" class="round_image" >
    <img src="http://placekitten.com/400/500&quot; />
    </a>

  • http://renwalker.com Ren Walker

    So filter + border-radius = okay. BUT…border-radius on a containing div + filter on the image = not working.
    Real world example:

    HTML:

    <img src="http://placekitten.com/500/600&quot; class="round_image2"/>
    <a href="#" class="round_image" >
    <img src="http://placekitten.com/400/500&quot; />
    </a>

    CSS:

    .round_image {
    border-radius: 50%;
    display: block;
    height: 220px;
    width: 220px;
    border: 3px solid #ddd;
    overflow: hidden;
    text-align: center;
    img {
    max-width: 100%;
    -webkit-filter: grayscale(1);
    }
    }

    .round_image2 {
    border-radius: 50%;
    max-width: 220px;
    -webkit-filter: grayscale(1);
    }

    View on CodePen: http://codepen.io/renwalker/pen/qnyzs

  • http://stramaxon.com Deepak Kamat

    That’s a great article :D
    But I think you put the wrong photo in the Sepia section, Bieber the Greatest Artist ??? X( Why not put photo of Michael Jackson – The King of Pop and The Greatest Artist…

  • Gaurav

    Thanks a lot!!! Looking for this

  • http://prettycleverdesign.com Ryan

    FYI, anyone running into flickering in Safari when transitioning from -webkit-filter: grayscale(0%); to -webkit-filter: grayscale(100%), and not happy with the “washed out” results of setting -webkit-backface-visibility: hidden;, use -webkit-filter: saturate(0%); instead.

    Example (transition from grey to color on hover with flickr):

    img {
    -webkit-filter: grayscale(100%);
    -webkit-transition:-webkit-filter 0.25s;
    }
    img:hover {
    -webkit-filter: grayscale(0%);
    }

    With -webkit-backface-visibility:hidden; (greyscale looks washed out):

    img {
    -webkit-filter: grayscale(100%);
    -webkit-transition:-webkit-filter 0.25s;
    -webkit-backface-visibility:hidden;
    }
    img:hover {
    -webkit-filter: grayscale(0%);
    }

    Neither issue above:

    img {
    -webkit-filter: saturate(0%);
    -webkit-transition:-webkit-filter 0.25s;
    }
    img:hover {
    -webkit-filter: saturate(100%);
    }

  • http://www.facebook.com/bob.alpakar Bob Alpakar

    Great tutorial with wrong image demonstration.

  • http://www.facebook.com/tony.macfarlane.9 Tony MacFarlane

    Wondering why this only works in Chrome. With the “-webkit” prefix, it should work in Safari too, right? I’ve seen these filters work fine in Safari– on MailChimp, for instance.

  • http://twitter.com/LymonLemon Lymon Lemon

    Cool but WTF is the point? That’s what Photoshop is for, period.

    • http://www.facebook.com/jesus.bejarano.948 Jesus Bejarano

      At this point PS desing is almost dead

  • http://www.facebook.com/niebes Nuno Neves

    Thumbs up for choosing justin bieber as the photo example mate. You just got a lot of comments about the kid and almost none about article. But what do i know? The number of comments is what counts right?

  • http://www.facebook.com/MarioErmando Erman Sayın

    GAY

  • icrawler

    The tut is nice, though I’m slightly annoyed by the Bieber picture…

  • http://www.facebook.com/husien.adel Husien Adel Arnous

    very nice

  • ken

    Bieber photo. But WHY? [facepalm]

  • wildemedia

    The image used here has completely put me off reading it. It has lost all credibility in my opinion. Use pictures of kittens! Everyone loves kittens. :D