issa0019
Immersion Expert
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
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.
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.
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!
Download
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"
>
- <a
class
=
"navbar-brand"
href
=
"https://sourcecodester.com"
>
Sourcecodester</
a
>
- </
div
>
- </
nav>
- <div
class
=
"col-md-3"
></
div
>
- <div
class
=
"col-md-6 well"
>
- <h3
class
=
"text-primary"
>
JavaScipt - Simple Image Transparency</
h3
>
- <hr
style
=
"border-top:1px dotted #ccc;"
/
>
- <center
><img
src
=
"image.jpg"
id
=
"image"
/
></
center
>
- <br
/
>
- <div
class
=
"col-md-6"
>
- <button
class
=
"btn btn-primary pull-right"
onclick
=
"revertImage();"
>
Add Color</
button
>
- </
div
>
- <div
class
=
"col-md-6"
>
- <button
class
=
"btn btn-danger pull-left"
onclick
=
"changeImage();"
>
Remove Color</
button
>
- </
div
>
- </
div
>
- <script
src
=
"js/script.js"
></
script
>
- </
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
;
- }
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!
Download
You must upgrade your account or reply in the thread to view hidden text.