JavaScipt - Simple Image Transparency

In this tutorial we will create a Simple Image Transparency using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

Getting started:

First you will need toAnd download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/. Note: To make the script work make sure you are running this application to any local server like xamp, etc..

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this 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">JavaScipt - Simple Image Transparency</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <center><img src="image.jpg" id="image"/></center>
  17. <br />
  18. <div class="col-md-6">
  19. <button class="btn btn-primary pull-right" onclick="revertImage();">Add Color</button>
  20. </div>
  21. <div class="col-md-6">
  22. <button class="btn btn-danger pull-left" onclick="changeImage();">Remove Color</button>
  23. </div>
  24. </div>
  25. <script src="js/script.js"></script>
  26. </body>
  27. </html>

Creating the Script

This code contains the script of the application. This code will turn your image to a grayscale color when clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. var imageObject = document.getElementById('image');
  2. var filter = false;
  3.  
  4. function convertToGrayScale(image){
  5. var canvas = document.createElement("canvas");
  6. var context = canvas.getContext("2d");
  7. var width = image.width;
  8. var height = image.height;
  9. canvas.width = width;
  10. canvas.height = height;
  11.  
  12. context.drawImage(image, 0, 0);
  13.  
  14. var pixels = context.getImageData(0, 0, width, height);
  15.  
  16. for(var i=0; i<pixels.height; i++){
  17. for(var j=0; j<pixels.width; j++){
  18. var x = (i*4) * pixels.width + j * 4;
  19. var total = (pixels.data[x] * pixels.data[x + 1] + pixels.data[x + 2]) / 3;
  20. pixels.data[x] = total;
  21. pixels.data[x + 1] = total;
  22. pixels.data[x + 2] = total;
  23. }
  24. }
  25.  
  26. context.putImageData(pixels, 0, 0, 0, 0, pixels.width, pixels.height);
  27.  
  28. return canvas.toDataURL();
  29. }
  30.  
  31. function changeImage(){
  32.  
  33. if(filter === false){
  34. imageObject.src = convertToGrayScale(imageObject);
  35. filter = true;
  36. }
  37.  
  38. }
  39.  
  40. function revertImage(){
  41. imageObject.src = "image.jpg";
  42. filter = false;
  43. }
There you have it we successfully created a Simple Image Transparency using 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!

Add new comment