如何使用 JavaScript 设置图像的亮度和对比度?


要设置亮度,请使用 brightness 属性,而对于对比度,请使用 contrast 属性。

示例

你可以尝试运行下面的代码,使用 JavaScript 里的图像滤镜 −

在线演示

<!DOCTYPE html>
<html>
   <body>
      <p>Click below to change the brightness and contrast of the image.</p>
      <button onclick="display()">Edit Image</button><br><br>
      <img id="myID" src="https://tutorialspoint.com/videotutorials/images/tutorial_library_home.jpg"
         alt="Tutorials Library" width="320" height="320">
      <script>
         function display() {
            document.getElementById("myID").style.filter = "brightness(50%)";
            document.getElementById("myID").style.filter = "contrast(50%)";
         }
      </script>
   </body>
</html>

更新于: 2020-6-23

3K+ 浏览量

开启你的职业生涯

完成课程,获得认证

开始
广告
© . All rights reserved.