HTML5 Image Effects App – Adding Noise and Invert

Tutorials

Noise and Invert HTML5 image effects. Today we continue HTML5 canvas examples, I hope you enjoy this series of articles. Even for me, working with graphics, images brings pleasure. Sure that and for you too. Today we will be adding two new filters – Noise and Invert.

Here are our demo and downloadable package:

Live Demo

[sociallocker]

download in package

[/sociallocker]


Ok, download the example files and lets start coding !


Step 1. HTML

Here are all html of my demo

index.html

01 <!DOCTYPE html>
02 <html lang="en" >
03 <head>
04 <link href="css/main.css" rel="stylesheet" type="text/css" />
05 <script type="text/javascript" src="js/script.js"></script>
06 <title>HTML5 Image Effects App - Adding Noise and Invert (demo) | Script Tutorials</title>
07 </head>
08 <body>
09     <div class="example">
10         <h3><a href="https://www.script-tutorials.com/html5-canvas-image-effects-app/">HTML5 Image Effects App - Adding Noise and Invert | Script Tutorials</a></h3>
11         <div class="column1">
12             Reset to:<br />
13             <input type="button" onclick="resetToColor()" value="Color" />
14             <input type="button" onclick="resetToGrayscale()" value="Grayscale" /><hr />
15             Effects:<br />
16             <input type="button" onclick="resetToBlur()" value="1. Blur" /><br />
17             <input type="button" onclick="resetToNoise()" value="2. Add noise" /><br />
18             <input type="button" onclick="resetToInvert()" value="3. Invert colors" /><hr />
19             Adjustment:<br />
20             <input type="button" onclick="changeGrayValue(0.1)" value="Lighter" /><br />
21             <input type="button" onclick="changeGrayValue(-0.1)" value="Darker" /><br />
22             Red: <input type="button" onclick="changeColorValue('er', 10)" value="More" />
23             <input type="button" onclick="changeColorValue('er', -10)" value="Less" /><br />
24             Green: <input type="button" onclick="changeColorValue('eg', 10)" value="More" />
25             <input type="button" onclick="changeColorValue('eg', -10)" value="Less" /><br />
26             Blue: <input type="button" onclick="changeColorValue('eb', 10)" value="More" />
27             <input type="button" onclick="changeColorValue('eb', -10)" value="Less" />
28             Blur: <input type="button" onclick="changeBlurValue(1)" value="More" />
29             <input type="button" onclick="changeBlurValue(-1)" value="Less" /><br />
30         </div>
31         <div class="column2">
32             <canvas id="orig" width="500" height="333"></canvas>
33             <canvas id="panel" width="500" height="333"></canvas>
34         </div>
35         <div style="clear:both;"></div>
36     </div>
37 </body>
38 </html>

What I did since last our version. I sorted the buttons and added two new. Nothing serious.

Step 2. CSS

Here are used CSS styles.

css/main.css

01 body {
02     background:#eee;
03     font-family:VerdanaHelveticaArialsans-serif;
04     margin:0;
05     padding:0
06 }
07 .example {
08     background:#fff;
09     width:730px;
10     font-size:80%;
11     border:1px #000 solid;
12     margin:20px auto;
13     padding:15px;
14     position:relative;
15     -moz-border-radius: 3px;
16     -webkit-border-radius:3px
17 }
18 h3 {
19     text-align:center;
20 }
21 .column1 {
22     float:left;
23     padding-right:10px;
24     text-align:right;
25     width:185px;
26 }
27 .column2 {
28     float:left;
29     width:500px;
30     background-color:#888;
31     padding:10px;
32 }
33 input[type=button] {
34     margin:5px;
35 }

Step 3. JS

Since today I don`t will publich whole script code again and again, but will publish our new functions which we using. Full version of script you can find in our package. First function for Noise effect:

js/script.js

01 function Noise() {
02     func = 'noise'// last used function
03     var imgd = contextOrig.getImageData(0, 0, iW, iH);
04     var data = imgd.data;
05     for (var i = 0, n = data.length; i < n; i += 4) {
06        // generating random color coefficients
07        var randColor1 = 0.6 + Math.random() * 0.4;
08        var randColor2 = 0.6 + Math.random() * 0.4;
09        var randColor3 = 0.6 + Math.random() * 0.4;
10         // assigning random colors to our data
11         data[i] = data[i]*p2*randColor1+er; // green
12         data[i+1] = data[i+1]*p2*randColor2+eg; // green
13         data[i+2] = data[i+2]*p3*randColor3+eb; // blue
14     }
15     // put image date back to context
16     context.putImageData(imgd, 0, 0);
17 }

We will generate colored noise. As you can see, during walking through all pixels we generating different coefficients for each color channel (red/green/blue). And in result – multiply our source pixel info to these coefficients. In result – we have noise effect. Next function is Invert, here are code:

01 function Invert(vContext) {
02     func = 'color'// last used function
03     var imgd = vContext.getImageData(0, 0, iW, iH);
04     var data = imgd.data;
05     for (var i = 0, n = data.length; i < n; i += 4) {
06         // assigning inverted colors to our data
07         data[i] = 255 - data[i]; // green
08         data[i+1] = 255 - data[i+1]; // green
09         data[i+2] = 255 - data[i+2]; // blue
10     }
11     // put image date back to context
12     vContext.putImageData(imgd, 0, 0);
13 }
14 .........
15 function resetToInvert() {
16     p1 = 1;
17     p2 = 1;
18     p3 = 1;
19     er = eg = eb = 0;
20     iBlurRate = 1;
21     if (func == '') Color();
22     Invert(context);
23     Invert(contextOrig);
24 }

Invert function is pretty easy, we will subtract current color value from 255 (white). So we will get reversed color (negative) for each point.


Live Demo

Conclusion

Not bad, isn`t it? Today we added two new interesting effects to our application – Noise and Invert. I will be glad to see your thanks and comments. Good luck!

Rate article