HTML5 Zone is brought to you in partnership with:

I was born in 1981 in one little city. Since I was 10y/o I programmed in different languages. My first languages were basic, then C++/MFC, after .Net (C#, VB.Net, J#, ASP.Net), XSL+XML processing). In the last 5 years I worked with web languages (HTML, CSS, PHP, SQL, XML, XSL, JavaScript). After university I worked in several different companies, eventually becoming a blogger. This is my hobby too. Andrey is a DZone MVB and is not an employee of DZone and has posted 111 posts at DZone. You can read more from them at their website. View Full User Profile

HTML5 Image Effects – HDR Simulation

  • submit to reddit

Today 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=""></script>
    google.load("jquery", "1.7.1");
<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:

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

 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 <; i+=4) {[i+0] = changeSinContrast([i+0]);[i+1] = changeSinContrast([i+1]);[i+2] = changeSinContrast([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);

    // different onclick handlers
    var iCur = 1;
    $('#next').click(function () {
        if (iCur > 6) iCur = 1;
        imgObj.src="images/pic" + iCur + '.jpg';
    $('#sepia').click(function () {
    $('#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, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)