How can I adjust the brightness, contrast, saturation, and hue in HTML5?

Is there a way to adjust the brightness without turning the image grey? I've been attempting to do so, but only managed to change it to greyscale.

My objective is to increase and decrease the brightness of the image.

Here is the current code:

HTML

<h1>Effects Brightness</h1>
<table>
  <tr>
    <td style="vertical-align:text-top;">
      <h2>BEFORE effect</h2>      
      <img id="cvs-src" src="/images/a.jpg" width=640 height=480/> 
    </td>

    <td>
      <h2>AFTER effect</h2>
      <canvas width=1024 height=768></canvas> 
    </td>    
  </tr> 
</table>

JS

(function() {
  window.onload = greyImages;

  function greyImages() {
    var img = document.getElementById("cvs-src");
    var imageData; 
    var px; 
    var length; 
    var i = 0; 
    var grey;

    var can = document.getElementsByTagName("canvas")[0].getContext('2d');    
    can.drawImage(img, 0, 0);
    imageData = can.getImageData(0, 0, 1024, 768);
    px = imageData.data;
    length = px.length;

    for ( ; i < length; i+= 4 ) {
      grey = px[i] * .3 + px[i+1] * .59 + px[i+2] * .11;
      px[i] = px[i+1] = px[i+2] = grey;
      grey = px[i] * .3 + px[i+1] * .59 + px[i+2] * .11;
      px[i] = px[i+1] = px[i+2] = grey;
      grey = px[i] * .1 + px[i+1] * .1 + px[i+2] * .1;  
      px[i] = px[i+1] = px[i+2] = grey;    
    }

    can.putImageData(imageData, 0, 0);      
  }
})();

EDIT: copy and paste it

<script> 
function greyImages() {
  var img = document.getElementById("cvs-src");
  var imageData; 
  var px; 
  var length; 
  var i = 0; 
  var grey;
  var can = document.getElementsByTagName("canvas")[0].getContext('2d');    
  can.drawImage(img, 0, 0);
  imageData = can.getImageData(0, 0, 1024, 768);
  px = imageData.data;
  length = px.length;
  for ( ; i < length; i+= 4 ) {
    grey = px[i] * .3 + px[i+1] * .59 + px[i+2] * .11;
    px[i] =  grey;     
  }
  can.putImageData(imageData, 0, 0);        
}  

</script> 

<h1>Effects</h1>
<table>
  <tr>
    <td style="vertical-align:text-top;">
      <h2>BEFORE effect</h2>      
      <button id="take" onclick="greyImages();" />
Download: http://people.opera.com/shwetankd/webm/sunflower.webm

      <video id="cvs-src" autoplay="autoplay" src="/images/1.webm"  
             type="video/webm" width=640 height=480></video>
    </td>

    <td>
      <h2>AFTER effect</h2>
      <canvas width=1024 height=768></canvas> 
    </td>    
  </tr> 
</table>

Answer №1

Alright, this solution is almost there for adjusting brightness up and down.

<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script> 
function adjustImageBrightness() {
  var img = document.getElementById("cvs-src");
  var imageData; 
  var px; 
  var length; 
  var i = 0; 
  var can = document.getElementsByTagName("canvas")[0].getContext('2d');    
  can.drawImage(img, 0, 0);
  imageData = can.getImageData(0, 0, 1024, 768);
  px = imageData.data;
  length = px.length;

  // Adjust Brightness
  for ( ; i < length; i+= 4 ) {   
    px[i]   -= 40 ;
    px[i+1] -= 40 ;
    px[i+2] -= 40 ;        
  }  

  can.putImageData(imageData, 0, 0);
} 

$(document).ready(function() {

});
</script> 

<h1>Effects</h1>
<table>
  <tr>
    <td style="vertical-align:text-top;">
      <h2>Original</h2>      
      <button id="adjust-btn" onclick="adjustImageBrightness();" />          
      <video id="cvs-src" autoplay="autoplay" src="/images/1.webm"  
             type="video/webm" width=640 height=480></video>
    </td>

    <td>
      <canvas width=1024 height=768></canvas> 
    </td>    
  </tr> 
</table>

Answer №2

Now you have the option to achieve this by setting ctx.filter = "...";. For more information, refer to this valuable MDN resource.

Take a look at this sample:

ctx.filter = 'blur(5px) hue-rotate(45deg)';
ctx.font = '48px serif';
ctx.strokeText('Hello world', 50, 100);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adjust the width of a container to exceed 32767 using jquery in the Opera browser

I am looking to create a compact timeline using jQuery, and I want this timeline to have a width exceeding 32767 pixels. Interestingly, when I attempt to modify the width using the jQuery code $(".timelinecontainer").width(32767);, it doesn't seem to ...

Customizable form fields in AngularJS

Consider the scenario of the form below: First Name: string Last Name: string Married: checkbox % Display the following once the checkbox is selected % Partner First Name: Partner Last Name: [Submit] How can a form with optional fields be created in Angu ...

Creating a form in NextJS to securely transfer user input data to MongoDB

Being new to JavaScript, I have a basic understanding and some lack of experience, but I am eager to learn more. Recently, I embarked on a project using NextJS, an efficient framework that integrates with ReactJS. My current challenge lies in creating a si ...

Using JQuery to apply a CSS class or check for a CSS class on multiple button elements that

Being new to Jquery and Javascript, I am facing challenges in understanding how to implement the following. Throughout my site, I use the same button class, and I am attempting to make Jquery apply a "clicked" class only to the button that was clicked, no ...

Maintaining form data while dynamically including more instances of a <div> element to the form

I am currently developing a SpringMVC Webapp with a view that contains a dynamic form. The dynamic elements of the form are listed below: At the moment, I am passing the variable ${worksiteCount} from my controller to the view (stored in my portlet sessio ...

Validating Input in a Textbox Using Jquery When Pasting Data

My HTML textbox is set up to prevent special characters from being entered, but I've noticed that the validation doesn't trigger when I paste text using Ctrl + V. Is there a way to address this issue? ...

A modern web application featuring a dynamic file treeview interface powered by ajax and php technology

Currently, I am developing a web-based document management system that operates as a single page using an ajax/php connection. The code snippet below shows how I display folders and files in a file tree view: if (isset($_GET['displayFolderAndFiles&apo ...

Design a unique border for a div element that extends a top-border all the way through the header and a bottom-border that

I am trying to achieve the design displayed in this mockup: https://i.sstatic.net/sYEPu.png Here is my current progress: https://codepen.io/raney24/pen/VOmjBY .header-border { width: 100%; margin-bottom: 10px; border-left: red solid 1px; border ...

Issue with Bootstrap 5 Card Header not extending to full width

I have implemented tables within cards using a CDN. While the table looks fine in wide widths, it does not resize to fill the entire width when squeezed. Below is the HTML code: <div class="card table-responsive"> <div clas ...

Tips for individually collapsing dynamic accordion panels within a Vue.js v-for loop

Is there a way to collapse accordions individually that are created inside a Vue.js v-for loop? I believe assigning a dynamic id to each accordion will help with this, but I am not sure where to add this id. Below is my HTML code: <div role="tablist"& ...

How can I position 7 images absolutely within a specific div?

I've been working on a website where users can have their avatars displayed using a JS function that loads 7 different images onto the page. These images correspond to different elements such as skin base, hair, eyes, mouth, shirt, shoes, and pants, a ...

Replacing an array element by a specific index number in a React application

I am dealing with an array of [false,true,false,false,true] and I am looking to update the boolean values based on their index numbers. I have been trying to solve this problem in react, but haven't found a workable solution yet. this.setState({ ...

Navigating through a Collection of Pictures using Mouse Movement and Left-click Button

Trying to create a customized PACS viewer for scrolling through a series of CT head images. However, encountering an issue with scrolling through all 59 images. Currently, able to scroll from the first to the 59th image, but struggling to loop back to the ...

Is it Possible to Stack Multiple Rows of Images with HTML5/CSS3?

I am struggling to arrange images in rows, but I'm facing unexpected outcomes. The images only break to a new line when the page is filled. I attempted to use overflow:hidden, however, the expanding images end up hidden under white space. I can't ...

What are the benefits of storing dist in both a GitHub repository and on npm?

I'm curious about why some repositories include a dist folder. Shouldn't repositories just store source code and not any builds or compiled files? Let's take a look at an example with ES6 code. package.json { "files": [ "dist", ...

Locate all nodes in neo4j that are connected to nodes matching a specified list of property values of a certain length

To clarify, I am interested in achieving the following (preferably using node/JS): Imagine you have a set of 3 job requirements ('JavaScript', 'PHP', 'MySQL'). In my graph setup, each Person node can be linked to multiple Sk ...

Is it possible to use ref in React to reference various elements based on specific actions?

I'm having trouble targeting the clicked button using a ref as I always get the second one. Any ideas on how to solve this issue? Also, if I have a native select element with two optgroups, is it possible to determine from which optgroup the selection ...

Is there a more concise method to reference the parent scope in AngularJS in this scenario?

Is there a shorter way to reference the parent scope within this controller? $scope.tables = []; $scope.newTable = function(){ $scope.tables.push({name:"Table " + ($scope.tables.length+1),cols:[]}); $scope.selected = $scope.tables.length-1; }; I ...

I would like to check if a given username and password exist in my JSON data

Trying different methods but not achieving the desired outcome. I have limited knowledge about JSON, gathered some information from online sources. var loginDataList = [{ "username": "abc", "password": "abc123" }, { "username": "richa", "p ...

CSS hover image resizing not functioning as expected

Is there a way to dynamically scale up an image when hovered over? I'm trying to achieve an effect where the image increases from 100x75 pixels to 300x225 pixels. Everything seems to be working fine with the layout, but the width doesn't seem to ...