How to Convert your Image Background into Grayscale in JavaScript

How to Convert your Image Background into Grayscale in JavaScript

Introduction

In this tutorial we will create a How to Convert your Image Background into Grayscale in JavaScript. This tutorial purpose is to teach how you can grayscale any background images. This will tackle all the basic function that will change the background color to grayscale. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system if you want to change the color of the background image. I will give my best to provide you the easiest way of creating this program Background image to grayscale. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only background images and buttons. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Convert your Image Background into Grayscale<h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-12">
  17. <img src="image.jpg" id="image" width="100%"/>
  18. <br />
  19. <br />
  20. <center><button class="btn btn-primary" onclick="convertImage();">Convert</button> <button class="btn btn-success" onclick="resetImage();">Revert</button></center>
  21. </div>
  22. </div>
  23. <script src="script.js"></script>
  24. </body>
  25. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically change your background image color to grayscale. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function convertImage(){
  2. var image=document.getElementById('image');
  3. image.style.filter="grayscale(100%)";
  4. }
  5.  
  6. function resetImage(){
  7. var image=document.getElementById('image');
  8. image.style.filter="grayscale(0%)";
  9. }

The code above is very simple and straightforward, to change our background image color to grayscale we will just create one method called convertImage(). This function will change your background image color to grayscale using the css style filterfunction. This will specifically filter the targeted object base on the filter options, in my case we use grayscale and set its value to 100& in order to grayscale our background image.

Output:

The How to Convert your Image Background into Grayscale in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Convert your Image Background into Grayscale in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment