HTML5 Image Effects – HDR Simulation
Join the DZone community and get the full member experience.
Join For FreeToday I would like to return to our html5 canvas experiments. And we
will prepare nice HDR (High dynamic range) filter for images. We will
use pixel transformation in our script. I added six different images
here, so you can play with them (switch them, and apply hdr
transformation). You can apply this effect multiple times in order to
get different variations.
Now you can test prepared demonstration, and download the sources.
Live Demo
download in package
Ok, download the example files and lets start coding togetger!
Step 1. HTML Markup
In the beginning, we should prepare clean HTML document, where we should prepare our header:
<script type="text/javascript" src="https://www.google.com/jsapi"></script> <script> google.load("jquery", "1.7.1"); </script> <script src="js/script.js"></script>
We’ve linked jQuery library (with using Google services) and our main script.js file with necessary JS code of our hdr filter. Then, in body of our document we should put next code:
index.html
<canvas id="source" width="1000" height="600"></canvas> <div class="actions"> <div id="next" class="button">Next image</div> <div id="sepia" class="button">Apply HDR Effect</div> </div>
This is our main canvas object and two action buttons (to switch the images and to apply filter).
Step 2. JS
Now, let’s create a new empty file ‘js/script.js’ file and put next code inside:
// variables var canvas, ctx; var imgObj; function changeSinContrast(par) { var dPow = 4; var iMid = 128; if (par > 0 && par < iMid) { par = Math.sin( Math.PI * ((90-(par/dPow)) / 180)) * par; } else if (par >= iMid) { par = Math.sin( Math.PI * ((90-((256-par))/dPow)/180) ) * par; } return par; } function processHrdEffect() { // get current image data var imageData = ctx.getImageData(0,0,canvas.width,canvas.height); var iMid = 128; var dPow = 3; for (var i=0; i < imageData.data.length; i+=4) { imageData.data[i+0] = changeSinContrast(imageData.data[i+0]); imageData.data[i+1] = changeSinContrast(imageData.data[i+1]); imageData.data[i+2] = changeSinContrast(imageData.data[i+2]); } // put image data back to context ctx.putImageData(imageData, 0, 0); }; $(function () { // create canvas and context objects canvas = document.getElementById('source'); ctx = canvas.getContext('2d'); // load source image imgObj = new Image(); imgObj.onload = function () { // draw image ctx.drawImage(this, 0, 0, this.width, this.height, 0, 0, canvas.width, canvas.height); } imgObj.src="images/pic1.jpg"; // different onclick handlers var iCur = 1; $('#next').click(function () { iCur++; if (iCur > 6) iCur = 1; imgObj.src="images/pic" + iCur + '.jpg'; }); $('#sepia').click(function () { processHrdEffect(); }); $('#toImage').click(function () { $('#img').attr('src', canvas.toDataURL('image/jpeg')); }); });
As usual (in similar scripts), after when the page have loaded, we create canvas and context objects. Then the script loads first one image (and displays it at our context), and after – adds event handlers for our buttons. When we click at ‘Apply HDR Effect’ button, it calls ‘processHrdEffect’ function. Then, the script walks through all the pixels and changes it’s contrast. Pay attention, that it uses ‘sin’ function here. It means that it changes contrast of pixels of middle diapason more than of the border pixels. It means that light sources will become more lighter, and dark sources will become more darker – HDR simulation.
Published at DZone with permission of Andrey Prikaznov, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments