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 |