views:

45

answers:

2

Is there any known/simple/open-source library that provide a javascript function that will switch colored picture that is displayed in an html page into a black and white

That can be used in all the most used browsers (IE, FireFox, Chrome)?

I mean something like:

<html>
...
<img id="myPic" src="pic.jpg">
...
<script type="text/javascript">
function onEvent(){
  var pic = document.getElementById("myPic");
  magicFunctionToBlackAndWhite(pic);
}
</script>
</html>

looking for that magicFunctionToBlackAndWhite()

A: 
Tatu Ulmanen
+1  A: 

There's no one solution that works across all browsers, but you can combine different solutions:

For IE, use the following CSS: filter: Gray

A lot of other browsers supports canvas, so you should be able to use this javascript code for that.

Of course it should't be too much of a hassle to make an image b/w in a serverside language such as .net, so you could always have a javascript that in principle does something like the following:

var imgs = document.getElementsByTagName('img');
for(var i = 0; i < imgs.length; i++) {
    imgs[i].src = 'convertimage.aspx?img=' + imgs[i].src;
}

...and then have all of the magic happening on the server.

David Hedlund
ok, looks like the example here(http://www.permadi.com/tutorial/jsCanvasGrayscale/index.html)will do the job.Although I thought there is something more elegant like when changing the transpernecy of an image we can use:element.style.opacity
Guy Roth