admin管理员组文章数量:1394977
On this Fiddle, the functions does the job perfectly, but I need to tweak the junction slightly.
- the click even needs to be a addClass (dark/light)(true of false) to the parent div.
- If dark addClass Bright.
- If light image addClass dark.
Is there a way to make this function doing what I need?
getImageBrightness(this.src,function(brightness) {
document.getElementsByTagName('pre')[0].innerHTML = "Brightness: "+brightness;
});
jsfiddle/s7Wx2/
On this Fiddle, the functions does the job perfectly, but I need to tweak the junction slightly.
- the click even needs to be a addClass (dark/light)(true of false) to the parent div.
- If dark addClass Bright.
- If light image addClass dark.
Is there a way to make this function doing what I need?
getImageBrightness(this.src,function(brightness) {
document.getElementsByTagName('pre')[0].innerHTML = "Brightness: "+brightness;
});
jsfiddle/s7Wx2/
Share Improve this question edited Jul 5, 2016 at 20:13 DDfrontenderLover asked Jul 5, 2016 at 20:03 DDfrontenderLoverDDfrontenderLover 4801 gold badge7 silver badges23 bronze badges 10- 2 So add an if and add the class based on the value of the brightness variable. – epascarello Commented Jul 5, 2016 at 20:06
- @epascarello thanks, I thought so, I can try but not sure I can do it. – DDfrontenderLover Commented Jul 5, 2016 at 20:13
-
What do you think is plicated? Break it up into twi steos. First use a basic
if/else
to get the classname to add. After that, it is adding the classname to the element – epascarello Commented Jul 5, 2016 at 20:15 - You need to determine what you consider the line between dark and light. It may be 255/2 => 127.5... Which is the straith half.... OR less... Depending on your judgement. Then pare. – Louys Patrice Bessette Commented Jul 5, 2016 at 20:16
- @LouysPatriceBessette something like this?? if (x < imgs.length){ $('pre').addClass('dark'); $('pre').removeClass('light'); }else{ $('pre').removeClass('dark'); $('pre').addClass('light'); } – DDfrontenderLover Commented Jul 5, 2016 at 20:24
1 Answer
Reset to default 8Working Fiddle Here.
In your HTML, I only added id
to your img
.
I played a little your pure javascript getImageBrightness
... But not that much.
I used "127.5" as the middle line between dark and light.
I suggest you evaluate it... Since the human eye isn't as mathematical as scripts.
;)
Here is the script:
var thisImg;
function getImageBrightness(image,callback) {
var thisImgID = image.attr("id");
var img = document.createElement("img");
img.src = image.attr("src");
img.style.display = "none";
document.body.appendChild(img);
var colorSum = 0;
img.onload = function() {
// create canvas
var canvas = document.createElement("canvas");
canvas.width = this.width;
canvas.height = this.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(this,0,0);
var imageData = ctx.getImageData(0,0,canvas.width,canvas.height);
var data = imageData.data;
var r,g,b,avg;
for(var x = 0, len = data.length; x < len; x+=4) {
r = data[x];
g = data[x+1];
b = data[x+2];
avg = Math.floor((r+g+b)/3);
colorSum += avg;
}
var brightness = Math.floor(colorSum / (this.width*this.height));
callback(thisImgID, brightness);
}
}
$("img").on("click", function(){
thisImg = $(this)
getImageBrightness( $(this),function( thisImgID, brightness ) {
document.getElementsByTagName('pre')[0].innerHTML = "Brightness: "+brightness+"<br><br>- Red border means class added -> dark,<br>- yellow border mean class added -> light";
if(brightness<127.5){
$("#"+thisImgID).addClass("dark");
}else{
$("#"+thisImgID).addClass("light");
}
});
});
CSS:
.dark{
border:3px solid red;
}
.light{
border:3px solid yellow;
}
本文标签: javascriptDetect if image is darklight and addClass (darklight) to parentStack Overflow
版权声明:本文标题:javascript - Detect if image is darklight and addClass (darklight) to parent - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744110088a2591250.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论