HTML5 and jquery make Web page grayscale image suspension effect

Source: Internet
Author: User
Tags min window

Article Introduction: HTML5 and jquery's grayscale image suspension effect.

In this tutorial, the author uses HTML5 's canvas to do different grayscale processing for the site images. This demo uses the gold combination of HTML5 and jquery to dynamically clone colors to achieve grayscale effects. The whole show is very cool. It is a good practical course to learn HTML5 canvas.

JQuery Code:

<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" POPs "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 + +) {
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>

Online Demo: http://webdesignerwall.com/demo/html5-grayscale/

Http://www.webjx.com/files/soft/jquery.min.zip


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.