Computer >> Máy Tính >  >> Lập trình >> Javascript

Làm cách nào để đặt độ sáng và độ tương phản của hình ảnh bằng JavaScript?


Để đặt độ sáng, hãy sử dụng độ sáng và đối với thuộc tính tương phản, hãy sử dụng thuộc tính tương phản.

Ví dụ

Bạn có thể thử chạy mã sau để sử dụng bộ lọc hình ảnh với 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://www.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>