I am interested in creating a JavaScript function that can transform an image to either a blue or yellow hue using CSS styling

Check out this code snippet.

<svg class="defs-only">
  <filter id="monochrome" color-interpolation-filters="sRGB"
          x="0" y="0" height="100%" width="100%">
    <feColorMatrix type="matrix"
      values="0.00 0 0 0  0 
              0.00 0 0 0  0  
              1 0 0 0  0 
                0  0 0 1  0" />
  </filter>
</svg>
<a class="profile-link" href="#" title="More about Amelia Bellamy-Royds">
  <img class="profile-pic" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/91525/AmeliaBR-bw.jpg"/></a>
<a class="profile-link" href="#" title="More about AmeliaBR">
  <img class="profile-pic" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/91525/AmeliaBR2-bw.jpg"/></a>

CSS

.profile-link {
  display: inline-block;
  padding: 1em;
  width: 10em;
  max-width: 80%;
}
.profile-pic {
  width: 100%;
  height: auto;
  border-radius: 50%;
  box-shadow: #222 0.2em 0.2em 1em;
}

/* More styles here */

.defs-only {
  position: absolute;
  height: 0; width: 0;
  overflow: none;
  left: -100%;
}

I want to further enhance this code by turning it into a reusable function for applying filters on uploaded images. Currently, I have integrated an image upload feature but need help executing the filtering process when a button is clicked. Here's what I've done so far:

HTML

<form>
    <input id="file-input" type="file" name="image" accept="image/*;capture=camera">
</form>
<svg class="defs-only">
  <filter id="monochrome" color-interpolation-filters="sRGB"
          x="0" y="0" height="100%" width="100%">
    <feColorMatrix type="matrix"
      values="1.00 0 0 0  0 
              1.00 0 0 0  0  
              1 0 0 0  0 
                0  0 0 1  0" />
  </filter>
</svg>
<a class="profile-link" href="#" title="More about Amelia Bellamy-Royds">
<pre id="output"></pre>
</a>

     <script>
            var input = document.getElementById("file-input");
            input.addEventListener("change", function(event) {
                var file = input.files[0],
                    img = new Image(),
                    reader = new FileReader();
                                reader.onload = function(event) {
                    var img = new Image();
                    img.width = 300;
                    img.src = event.target.result;
                    document.body.appendChild(img);
                };
                reader.readAsDataURL(file);
            }, false);
        </script>


<body>  
<button type="button" onclick="myFunction()">Try it</button>
</body>

CSS

.profile-link {
  display: inline-block;
  padding: 1em;
  width: 10em;
  max-width: 80%;
}
.profile-pic {
  width: 100%;
  height: auto;
  border-radius: 50%;
  box-shadow: #222 0.2em 0.2em 1em;
}

/* Additional CSS properties */

.defs-only {
  position: absolute;
  height: 0; width: 0;
  overflow: none;
  left: -100%;
}

Answer №1

After inserting the image into your HTML document, you can easily add a filter by including the filter class to the image:

let displayImage = document.getElementById("uploaded-image");
displayImage.classList.add("filtered-pic");

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

Combining the powers of Socket.io, node.js, and smart routing

How can I pass an already connected socket from socket.io to the controller when making a post request from the application to the server, using Vue and a Node server with Socket.io? I attempted declaring global.io = io, but the socket remains undefined. ...

Create a unique web page layout by utilizing divs and spans

Can the design shown in this image be created? https://i.sstatic.net/hsIaQ.png I've tried the code below but it's not functioning properly. Any assistance would be appreciated! The issue is that the Q and A text should be in a box that automati ...

The event listener for 'load' is not functioning properly within the React (Gatsby) environment

I'm having trouble with a sticky scroll parallax effect when the page initially loads at a position other than the top. I'm utilizing the react-scroll-parallax library (https://www.npmjs.com/package/react-scroll-parallax). To address this issue, ...

How to apply dynamic styling to a MatHeaderCell using NgStyle?

My goal is to dynamically style a MatHeaderCell instance using the following code: [ngStyle]="styleHeaderCell(c)" Check out my demo here. After examining, I noticed that: styleHeaderCell(c) It receives the column and returns an object, however ...

Tips for implementing an element onClick change within a Redux container using React.js

After coming across a similar question by another user on this link, I found the answer quite clear. However, if you're dealing with a redux container, the states are transformed into props via the mapStateToProps function. So, my query is: how shoul ...

Exploring the possibilities of infinite scroll in JavaScript using the Backbone framework

I've been grappling with this problem for three days straight. I've been attempting to incorporate scrolling into my backbone project using the https://github.com/paulirish/infinite-scroll plugin. Despite my best efforts to find a solution throu ...

Bootstrap 4 modal experiencing issues with the form end tag functionality

Currently, I am facing an issue while attempting to incorporate a confirmation delete feature using a Bootstrap 4 modal in my Laravel project. The problem arises when the modal is opened, and the submit button fails to function. Upon inspecting the browser ...

JQuery modal for creating vibrant and personalized color schemes

I have a basic jQuery modal code that uses grey as the default color. How can I customize this to create a colorful toolbox, for example changing the color from grey to blue? Also, how can I use this script for multiple dialogs, like setting #dialog1 to be ...

What is the best way to apply ngClass to style a JSON object based on its length?

Currently, I am working with a mat-table that displays data from a JSON object. My goal is to filter out all records with an ID of length 5 and then style them using ngClass in my HTML template. How can I achieve this? Below is the code I am working with: ...

Using the event object with fullCalendar.formatDate: A step-by-step guide

I am struggling to implement the formatDate function with the event object retrieved from the eventMouseOver method and it doesn't seem to be working as expected. Do you have any recommendations? Below is the snippet of my code: eventMouseover: func ...

Ways to incorporate a conditional statement within a dropdown menu

When a user interacts with a dropdown, I want to conditionally show different choices based on their selection. If the user clicks on a checkbox, a specific set of options will appear; if they click on a radio button, another set of options will appear. h ...

Developing a logic for a Tagging System in PHP and MySQL

As a novice developer, I am seeking advice on my current project. I am in the process of creating a platform where users can upload images and tag them. After researching various methods of storing tags, I came across the following structure: Tag Storag ...

Search through a JSON object, identify all keys that start with the same letter, and transfer them to a new JSON data structure

I am looking to iterate through my JSON data and extract all keys along with their values that start with the letters "QM". Below is an example of my JSON content: [ { MHF46: 'Ledig', MHF181: '06', QM6_QMSI2: '1899-12-30 ...

What steps are involved in incorporating dynamic pagination in PHP using this particular snippet of code?

I am looking for a way to display a list of numbers from 1 to 1000 in a single line with the ability to scroll. However, I need to exclude both "Prev" and "1" from this list. How can I achieve this? Below is the code snippet that I currently have: echo ...

The HTML textarea is not updating properly when using jQuery's keypress event

I am facing an issue with my forms on a webpage, each containing an html textarea: <textarea name="Comment" class="inputTextArea">Hello World!</textarea> There is a javascript event handler that automatically submits the fo ...

Conflict between JavaScript function and CSS styling

I'm facing an issue with a radio button in a large form that has some styling applied to it. The background color changes when hovered or checked, but there seems to be a conflict with an important JavaScript function in the rest of the form. It took ...

Ways to loop through HTML elements based on certain criteria?

Issue : The v-for loop is successfully iterating and binding data in the HTML template, but there is a challenge in partially iterating it based on certain conditions. Please refer to the JSFiddle link below for a demo. Objective : In the provided demo li ...

Instructions on bringing in the THREE.InfiniteGridHelper

Struggling to utilize THREE.InfiniteGridHelper due to difficulties importing the code without errors. Even after copying and pasting the code, the issue persists. The code is currently copied and pasted into my main.js page, resulting in the following err ...

What methods are available in JavaScript for determining the width of a section and implementing proper spacing for a menu?

Is it possible to use Javascript to dynamically adjust the size of each <li> element within the menuArea so that they fill the entire space correctly? // Could we achieve this with the following code... var menuSpace = Math.floor((menuAreaWidth ...

AngularJs - A directive for handling asynchronous operations with deferred or batched processing

I've exhausted my efforts searching through Google and books for a solution, so unfortunately I don't have any code to provide at the moment. My objective is the following: 1 - Develop a directive that accepts 2 parameters in this format <r ...