简体   繁体   English

如何更改图像仅一部分的亮度(html / css / js)?

[英]How can I change the brightness of only part of an image (html/css/js)?

I have an image with tags underneath that correspond to different parts of the image. 我有一个带有标签的图像,这些标签对应于图像的不同部分。 For instance, the image might be a dog, and one of the tags is 'nose', which corresponds to the portion of the image around the dogs nose. 例如,图像可能是一条狗,而标签之一是“鼻子”,它对应于图像在狗鼻子周围的部分。 When the user hovers over a tag, I want the brightness around this corresponding part of the image to increase. 当用户将鼠标悬停在标签上时,我希望图像相应部分周围的亮度增加。 How can I do this? 我怎样才能做到这一点?

 function fixLabel(c) { var x_coor = document.getElementById('x').textContent; var y_coor = document.getElementById('y').textContent; var x2_coor = document.getElementById('x2').textContent; var y2_coor = document.getElementById('y2').textContent; var width = document.getElementById('w').textContent; var height = document.getElementById('h').textContent; var tag = document.createElement('input'); // Create a `input` element, tag.setAttribute('type', 'text'); // Set it's `type` attribute, tag.setAttribute('name', 'loc:' + round_2_decimals(x_coor) + "-" + round_2_decimals(y_coor) + "-" + round_2_decimals(x2_coor) + "-" + round_2_decimals(y2_coor) + "-" + round_2_decimals(width) + "-" + round_2_decimals(height)); /** *Here's the area: How do I attach a mouseover function so the image's *brightness increases whenever I hover over the tag? */ tag.onmouseover = function() {}; var br = document.createElement('br'); // Create a `br` element, var button_div = document.getElementById('fix_label_area'); button_div.appendChild(br); button_div.appendChild(tag); button_div.appendChild(br); }; 
 <div> <p id='x'></p> <p id='y'></p> <p id='x2'></p> <p id='y2'></p> <p id='w'></p> <p id='h'></p> <br/> <button id='fix_label' onclick="fixLabel()">Fix Label Here</button> </div> <form action="" method="POST" id="canvas"> <div id='img_area'> <img src="someImageFile.jpg" id="imageId" width="350" height="350" /> </div> <div id='label_area'> <input type="submit" name="submit" value="Label" id='input'> </div> <div id='fix_label_area'></div> </form> 

Personally Canvas and SVG are more powerful, but you can use CSS to clip a circle and opacity to dim the original. 就个人而言,Canvas和SVG更为强大,但是您可以使用CSS剪切一个圆圈,并使用不透明度使原始图像变暗。 But you need to check the browser support to make sure it covers the browsers you need. 但是,您需要检查浏览器支持 ,以确保它涵盖了所需的浏览器。

 div.holder { position: relative; } img.main { opacity: .4; } img.circle { position: absolute; clip-path: circle(50px at 405px 135px); } 
 <div class="holder"> <img class="circle" src="https://placedog.net/500" /> <img class="main" src="https://placedog.net/500" /> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM