Once upon a time, grayscale images displayed on the website must be manually converted. Now with the HTML 5 canvas, images can be cleverly converted to gray without having to use image editing software. The following example shows how to use HTML 5 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.
BKJIA recommendation topic: detailed explanation of next-generation Web development standards in HTML 5
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.
- <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">").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();
- }
-
- // --></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, etc)
◆ 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.
Link: http://blog.csdn.net/hfahe/archive/2011/02/25/6208765.aspx