Create a grayscale gradient effect for a cool image using HTML5

Source: Internet
Author: User

Once upon a time, grayscale images displayed on the website must be manually converted. Now, using the HTML5 canvas, images can be cleverly converted to gray without having to use image editing software. The following example shows how to use HTML5 and jQuery to dynamically convert a color image to a gray image. Contributor: I would like to thank darsey Clark (my partner at Themify) for contributing jQuery and Javascript code.

 

Example: HTML5 grayscale gradient http://webdesignerwall.com/demo/html5-grayscale/

 


Purpose

The purpose of this example is to show you how to use HTML5 and jQuery to create a grayscale/colored image with the mouse floating effect. Before HTML5 appears, two images, color and grayscale versions are required to achieve this effect. Now HTML5 makes it easier and more efficient to create this effect, because gray images will be generated directly from the original file. I hope you will find this script useful in the design of a showcase or album.



 




 

JQuery code

The following jQuery code looks for the target image and generates a grayscale version. When the mouse is hovering over the image, the code will gradient the grayscale image into a color.

 

[Javascript] <mce: script src = "jquery. min. js" mce_src = "jquery. min. js" type = "text/javascript"> </mce: script>
<Mce: 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 "mce_style =" display: inline-block ">" mirror.clone().addclass('img_grayscale'mirror.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 For (var x = 0; 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 ();
}

// --> </Mce: script>
<Mce: script src = "jquery. min. js" mce_src = "jquery. min. js" type = "text/javascript"> </mce: script>
<Mce: 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 "mce_style =" display: inline-block ">" mirror.clone().addclass('img_grayscale'mirror.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 For (var x = 0; 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 ();
}
 
// --> </Mce: script>

 

How to Use

Use this effect on your site:

Reference jQuery. js
Paste the above Code
Set the target image (for example,. post-img, img,. gallery img, and so on)
You can change the animation speed (for example, 1000 = 1 second)
Compatibility

It can work on any browser that supports HTML5 and Javascript, such as Chrome, Safari, and Firefox. If the browser does not support HTML5, the effect will be returned to the original color image. Note: If the local file does not work on Firefox or Chrome, you must put the HTML code on a Web server.

 


Thanks

The Javascript and HTML5 grayscale gradient code comes from darsey Clark.

 

From Jiang Yujie's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.