Вызов плагина, параметром передается селектор, для которого нужно применить grayScale.
$(document).ready(function() {
makeGray('body div img');
});
Подключаемый файл, где собственно описана вся логика работы.
var grayTryCount = 0;
var timerID = false;
function makeGray(selector) {
if (grayTryCount <= 3) {
grayTryCount +=1;
$(selector).each(function(){
if ((this.complete) && (!$(this).hasClass('makeGrayDone')) && (!$(this).hasClass('gray'))){
makeCopy(this);
} else {
if (!timerID) {
timerID = setTimeout(function() {makeGray('body div img')}, 1000);
}
}
});
}
}
function makeCopy(img) {
var canvas2DSupported = !!window.CanvasRenderingContext2D;
if (canvas2DSupported) {
var newImg = makeCanvasCopy(img);
} else {
var newImg = makeIMGCopy(img);
}
var offset = $(img).offset();
$(img).parent().append(newImg);
$(img).addClass('makeGrayDone');
$(newImg).offset(offset);
$(newImg).hover(hoverIn, hoverOut);
}
function makeIMGCopy(imgObj) {
var newImg = document.createElement('img');
newImg.src = $(imgObj).attr('src');
$(newImg).attr('class', 'gray');
newImg.style.filter ='progid:DXImageTransform.Microsoft.BasicImage(grayScale=1)';
return newImg;
}
function makeCanvasCopy(imgObj) {
var newImg = document.createElement('img');
var canvas = document.createElement('canvas');
var canvasContext = canvas.getContext('2d');
var imgW = imgObj.width;
var imgH = imgObj.height;
canvas.width = imgW;
canvas.height = imgH;
canvasContext.drawImage(imgObj, 0, 0);
var imgPixels = canvasContext.getImageData(0, 0, imgW, imgH);
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;
}
}
canvasContext.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
newImg.src = canvas.toDataURL();
$(newImg).attr('class', 'gray');
return newImg;
}
function createCanvas(imgObj) {
var canvas = document.createElement('canvas');
var canvasContext = canvas.getContext('2d');
var imgW = imgObj.width;
var imgH = imgObj.height;
canvas.width = imgW;
canvas.height = imgH;
canvasContext.drawImage(imgObj, 0, 0);
var imgPixels = canvasContext.getImageData(0, 0, imgW, imgH);
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;
}
}
canvasContext.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
imgObj.src = canvas.toDataURL();
//return canvas.toDataURL();
}
function hoverIn(obj) {
$(this).animate({
opacity: 0
},
{duration: 500, step: function(now, fx) {
// Every step of the opacity animation we'll get the current
// opacity as the 'now' argument.
var opacity = Math.round(now * 100);
$(fx.elem).css('-ms-filter', 'progid:DXImageTransform.Microsoft.Alpha(Opacity=' + opacity + ')');
}
});
}
function hoverOut() {
$(this).animate({
opacity: 1
},
{duration: 500, step: function(now, fx) {
var opacity = Math.round(now * 100);
$(fx.elem).css('-ms-filter', 'progid:DXImageTransform.Microsoft.Alpha(Opacity=' + opacity + ')');
}
});
}