JavaScipt - Simple Image Transparency
Submitted by razormist on Thursday, January 17, 2019 - 21:08.
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...
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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <br />
- <div class="col-md-6">
- </div>
- <div class="col-md-6">
- </div>
- </div>
- </body>
- </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.- var imageObject = document.getElementById('image');
- var filter = false;
- function convertToGrayScale(image){
- var canvas = document.createElement("canvas");
- var context = canvas.getContext("2d");
- var width = image.width;
- var height = image.height;
- canvas.width = width;
- canvas.height = height;
- context.drawImage(image, 0, 0);
- var pixels = context.getImageData(0, 0, width, height);
- for(var i=0; i<pixels.height; i++){
- for(var j=0; j<pixels.width; j++){
- var x = (i*4) * pixels.width + j * 4;
- var total = (pixels.data[x] * pixels.data[x + 1] + pixels.data[x + 2]) / 3;
- pixels.data[x] = total;
- pixels.data[x + 1] = total;
- pixels.data[x + 2] = total;
- }
- }
- context.putImageData(pixels, 0, 0, 0, 0, pixels.width, pixels.height);
- return canvas.toDataURL();
- }
- function changeImage(){
- if(filter === false){
- imageObject.src = convertToGrayScale(imageObject);
- filter = true;
- }
- }
- function revertImage(){
- imageObject.src = "image.jpg";
- filter = false;
- }
Add new comment
- 116 views