Created
December 4, 2017 11:24
-
-
Save SilencerWeb/5e194117d5ccd0a14a13ce5de5b03fd4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var img = new Image(); | |
var canvas = document.createElement('canvas'); | |
var ctx = canvas.getContext('2d'); | |
img.src = imageUrl; | |
img.onload = function () { | |
canvas.width = img.width; | |
canvas.height = 100; | |
ctx.drawImage(img, 0, 0); | |
img.style.display = 'none'; | |
var result = 0, | |
mediumValue = 0, | |
bright, | |
pixelCount = 0; | |
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); | |
var data = imageData.data; | |
for (var i = 0; i < data.length; i += 4 * 10) { | |
mediumValue = (data[ i ] + data[ i + 1 ] + data[ i + 2 ]) / 3; | |
result += mediumValue; | |
pixelCount++; | |
} | |
bright = result / pixelCount; | |
if (bright > 200) { | |
$('header').addClass('header_gray'); | |
} else { | |
if ($('header').hasClass('header_gray')) { | |
$('header').removeClass('header_gray'); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment