HTML 5 joins hands with jQuery to achieve the grayscale gradient effect of a cool image

Source: Internet
Author: User

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.

 
 
  1. <mce:script src="jquery.min.js" mce_src="jquery.min.js" type="text/javascript"></mce:script>  
  2. <mce:script type="text/javascript"><!--  
  3.    
  4.         // On window load. This waits until images have loaded which is essential  
  5.         $(window).load(function(){  
  6.    
  7.                // Fade in images so there isn't a color "pop" document load and then on window load  
  8.                $(".item img").fadeIn(500);  
  9.    
  10.                // clone image  
  11.                $('.item img').each(function(){  
  12.                        var el = $(this);  
  13.                        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(){  
  14.                                var el = $(this);  
  15.                                el.parent().css({"width":this.width,"height":this.height});  
  16.                                el.dequeue();  
  17.                        });  
  18.                        this.src = grayscale(this.src);  
  19.                });  
  20.    
  21.                // Fade image  
  22.                $('.item img').mouseover(function(){  
  23.                        $(this).parent().find('img:first').stop().animate({opacity:1}, 1000);  
  24.                })  
  25.                $('.img_grayscale').mouseout(function(){  
  26.                        $(this).stop().animate({opacity:0}, 1000);  
  27.                });  
  28.         });  
  29.    
  30.         // Grayscale w canvas method  
  31.         function grayscale(src){  
  32.                var canvas = document.createElement('canvas');  
  33.                var ctx = canvas.getContext('2d');  
  34.                var imgObj = new Image();  
  35.                imgObj.src = src;  
  36.                canvas.width = imgObj.width;  
  37.                canvas.height = imgObj.height;  
  38.                ctx.drawImage(imgObj, 0, 0);  
  39.                var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);  
  40.                for(var y = 0; y < imgPixels.height; y++){  
  41.                        for(var x = 0; x < imgPixels.width; x++){  
  42.                                var i = (y * 4) * imgPixels.width + x * 4;  
  43.                                var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;  
  44.                                imgPixels.data[i] = avg;  
  45.                                imgPixels.data[i + 1] = avg;  
  46.                                imgPixels.data[i + 2] = avg;  
  47.                        }  
  48.                }  
  49.                ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);  
  50.                return canvas.toDataURL();  
  51.     }  
  52.    
  53. // --></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

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.