Once upon a time, grayscale image has to be manually converted in order to be displayed on the web. Now with HTML5 canvas, images can be manipulated into grayscale without having to use image editing software. I've put together a demo to show you how to use HTML5 & jQuery to dynamically clone color images into grayscale (see demo). Credits: thanks to Darcy Clarke (my Themify's partner) for the jQuery and Javascript code.
The Purpose
This demo is intented to show you how to make a grayscale/color image hover effect with HTML5 and jQuery. To achieve this effect before HTML5, two images are required: a color and a grayscale version. Now HTML 5 made it easier and faster because the grayscale image is generated from the original source. I hope you will find this script useful in your design such as portfolio showcase, photo gallery, etc.
jQuery Code
The jQuery code below will look for the target images and generate a grayscale version. When hovering the image, it will fade the grayscale image into color.
<script src="jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
// On window load. This waits until images have loaded which is essential
$(window).load(function(){
// Fade in images so there isn't a color "pop" document load and then on window load
$(".item img").fadeIn(500);
// clone image
$('.item img').each(function(){
var el = $(this);
el.css({"position":"absolute"}).wrap("<div class='img_wrapper' style='display: inline-block'>").clone().addClass('img_grayscale').css({"position":"absolute","z-index":"998","opacity":"0"}).insertBefore(el).queue(function(){
var el = $(this);
el.parent().css({"width":this.width,"height":this.height});
el.dequeue();
});
this.src = grayscale(this.src);
});
// Fade image
$('.item img').mouseover(function(){
$(this).parent().find('img:first').stop().animate({opacity:1}, 1000);
})
$('.img_grayscale').mouseout(function(){
$(this).stop().animate({opacity:0}, 1000);
});
});
// Grayscale w canvas method
function grayscale(src){
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var imgObj = new Image();
imgObj.src = src;
canvas.width = imgObj.width;
canvas.height = imgObj.height;
ctx.drawImage(imgObj, 0, 0);
var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
for(var y = 0; y < imgPixels.height; y++){
for(var x = 0; x < imgPixels.width; x++){
var i = (y * 4) * imgPixels.width + x * 4;
var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
imgPixels.data[i] = avg;
imgPixels.data[i + 1] = avg;
imgPixels.data[i + 2] = avg;
}
}
ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
return canvas.toDataURL();
}
</script>
How to use it
To apply this to your site:
- include a copy of jquery.js
- paste the code as shown above
- set the target image (eg:
.post-img,img,.gallery img, etc.) - you may change the animation speed (ie. 1000 = 1 second)

Compatibility
It works with any browser that support HTML5 and Javascript such as Chrome, Safari, and Firefox. If HTML5 is not supported, it will fallback to original color image. Note: if it doesn't work with Firefox and Chrome locally. You need to put the HTML file on a web server.
Credits
The Javascript & HTML 5 grayscale is coded by Darcy Clarke.

why do the pictures fade completely away? for me they don’t go to grayscale. they disappear.
For this I might be able to help out, could you paste your JS code? That could help resolve the problem (if not already resolved).
nvm. i guess it had to do wtih opacity. THX!
Hi Elijah – I’m having an issue where the images start 0ut in Colour and fade away to nothing. Please could you post what you did to correct this issue?
Thanks in Advance !
i still get the color pop. any idea how to change that? i also noticed it happens on yours as well.
Got a fix yet? The initial fade in doesn’t work for me and I get this “pop” issue as well. I’ve even tried making different functions to fade it in but hat didn’t resolve the problem (I’m not that experienced with Javascript…), so I guess the error must be somewhere in the HTML (which is exactly the same as used in the demo) or in the JS (which is copied as well).
I copied the code to test if it works for me as well, because sometimes there is still some code that has to be added in order for it to work properly. After that, I change the code so that it fits my design.
I’ve got my fix. The image doesn’t start off with 0 opacity but at 100 and therefor cannot fade in any further from 100 to 100, meaning that it is already fully loaded from the start. Change the initial opacity to 0 and it should be ok. (I used an external CSS sheet)
[code="CSS"]
.img_grayscale {
opacity: 0;
position: absolute;
z-index: 998;
}
[/code]
And in the HTML code, give the image the following:
[code="HTML"]
style="style="opacity: 0;"
[/code]
This should turn the pop into a smooth fade.
Can you explain that a little bit more?
i want the images to go GRAY on mouse over . else nothing should happen .
thanks
change opacity”:”100″
and
// Fade image
$(‘.view-content img’).mouseover(function(){
$(this).parent().find(‘img:first’).stop().animate({opacity:0}, 1000);
})
$(‘.img_grayscale’).mouseout(function(){
$(this).stop().animate({opacity:1}, 1000);
});
});
i did every thing but the thing is not working at all ! Can you explain me what changfes should i do in HTML or CSS . . . . I have just added the JS
could i apply these to a wordpress theme?
thnks my Pro we need more
I tried this and works dam well. Awesome script.
can you show whole working code with html etc…please :)
i am getting difficulties with implementing it
hi… how i can download the codes ?? any body help me please…
i tried everything and it doesn’t work. nothing’s changing. can you summarize it step by step please?
This is absolutely brilliant! I was killing myself trying to figure out how to grayscale some images across all browsers (had only been able to do it on IE and Firefox before coming across this post). One quick comment though:
- In the javascript function referenced above, the method to fade-in the images to avoid the initial “pop” is different from the function that is now in the source code of the demo. Above it reads:
$(“.item img”).fadeIn(500);
But in the demo you use:
$(“.item img”).animate({opacity:1},500);
I was struggling to avoid the pop, and when I changed my code to use .animate with a target opacity, it worked. I also had to set the initial opacity of my images in my stylsheet to something other that 1 (otherwise, as someone else mentioned here, the image can’t really fade from 1 to 1…) In my case I set the initial opacity in my stylesheet to 0.25 to have them sort of “ghosted” at first, and then the opacity in the javascript function to 0.75.
Anyhow, great resource! THANKS!!
I recently made a change to my template on dreamweaver and updated the template to the page where i was using the grayscale and now it is not working . It is saying that the jquery.min.js is not on the local disk do you have any ideas on what i can do to fix the problem?
can somebody show / past / OR send me whole code INCLUDING images tags
It looks interesting but Im new to webdesigning and have no idea how to use it
:)
thanks!
It looks great but I have two problems.
The site is loading with the images in colour and then turning to greyscale.
The images are now pushing down and covering my descriptive text below.
Any ideas?
The most sexiest website and sexiest tutorials and scripts.
I’ve been trying to get this code to work on my Tumblr because I really dislike the random splash of color everywhere in my very minimal theme.
You can check it out at- http://jessicaisabamf.com/
I want all the images I reblog to be black and white, and when hovered over to be color. Is that possible?
I’ve tried several times and I can’t seem to get it right.
https://github.com/Prydie/jQuery-GreyScale-Plugin
this for me is very simply and easy to use.
however thanks for this interesting post!
grazie
Fantastic…
The script works GREAT on FF11 and IE9, really superb!
But it doesn’t fit my needs, cause I need it to work on BACKGROUND-IMAGES, which I use through CSS, like this:
Any idea how to change the script in order to work with bg-images?
I have used HTML5. This is the latest version of HTML. It will help us in our working. Thanks a lot.