HTML5 and jquery make Web page grayscale image suspension effect

Source: Internet
Author: User

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>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.