• We just launched and are currently in beta. Join us as we build and grow the community.

JavaScipt - Simple Image Transparency

issa0019

Immersion Expert
I Rep
0
0
0
Rep
0
I Vouches
0
0
0
Vouches
0
Posts
34
Likes
191
Bits
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.
  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. <body

    >
  8. <nav class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav>
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    JavaScipt - Simple Image Transparency</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <center

    ><img

    src

    =

    "image.jpg"

    id

    =

    "image"

    /

    ></

    center

    >
  18. <br

    /

    >
  19. <div

    class

    =

    "col-md-6"

    >
  20. <button

    class

    =

    "btn btn-primary pull-right"

    onclick

    =

    "revertImage();"

    >

    Add Color</

    button

    >
  21. </

    div

    >
  22. <div

    class

    =

    "col-md-6"

    >
  23. <button

    class

    =

    "btn btn-danger pull-left"

    onclick

    =

    "changeImage();"

    >

    Remove Color</

    button

    >
  24. </

    div

    >
  25. </

    div

    >
  26. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  27. </

    body

    >
  28. </

    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. function

    convertToGrayScale(

    image)

    {
  4. var

    canvas =

    document.createElement

    (

    "canvas"

    )

    ;
  5. var

    context =

    canvas.getContext

    (

    "2d"

    )

    ;
  6. var

    width =

    image.width

    ;
  7. var

    height =

    image.height

    ;
  8. canvas.width

    =

    width;
  9. canvas.height

    =

    height;

  10. context.drawImage

    (

    image,

    0

    ,

    0

    )

    ;

  11. var

    pixels =

    context.getImageData

    (

    0

    ,

    0

    ,

    width,

    height)

    ;

  12. for

    (

    var

    i=

    0

    ;

    i<

    pixels.height

    ;

    i++

    )

    {
  13. for

    (

    var

    j=

    0

    ;

    j<

    pixels.width

    ;

    j++

    )

    {
  14. var

    x =

    (

    i*

    4

    )

    *

    pixels.width

    +

    j *

    4

    ;
  15. var

    total =

    (

    pixels.data

    [

    x]

    *

    pixels.data

    [

    x +

    1

    ]

    +

    pixels.data

    [

    x +

    2

    ]

    )

    /

    3

    ;
  16. pixels.data

    [

    x]

    =

    total;
  17. pixels.data

    [

    x +

    1

    ]

    =

    total;
  18. pixels.data

    [

    x +

    2

    ]

    =

    total;
  19. }
  20. }

  21. context.putImageData

    (

    pixels,

    0

    ,

    0

    ,

    0

    ,

    0

    ,

    pixels.width

    ,

    pixels.height

    )

    ;

  22. return

    canvas.toDataURL

    (

    )

    ;
  23. }

  24. function

    changeImage(

    )

    {

  25. if

    (

    filter ===

    false

    )

    {
  26. imageObject.src

    =

    convertToGrayScale(

    imageObject)

    ;
  27. filter =

    true

    ;
  28. }

  29. }

  30. function

    revertImage(

    )

    {
  31. imageObject.src

    =

    "image.jpg"

    ;
  32. filter =

    false

    ;
  33. }

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.
 

452,292

323,526

323,535

Top