HTML5 Image Effects – HDR simulation

HTML5 Image Effects – HDR simulation

4 29250
HTML5 Image Effects - HDR simulation

HTML5 Image Effects – HDR simulation

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);
    imgObj.src = 'images/pic1.jpg';
    // 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.

Live Demo


I hope that you like our html5 experiments. Welcome back.



    • Hi Junaid,
      It is pretty easy. Firstly you should develop your own upload form, then – add accepting script (PHP). Finally -display your result at the screen, or output it in image format (after effect version)

  1. hi… this is awsome effect. how can we increase weight of effect to make more hdr look pls explain working of changeSinContrast() function & How iMid dPow values process the image by putting comments in script… thanks

    • Hi Vehlad,
      As you already noticed, the ‘changeSinContrast’ function is used to change all three colors (RGB) of every image pixel. This function decreases and increases color values (depending on their initial value). You know, that the color range is 0-255, the middle is 128 (iMid param). Every time we evoke this function, it decreases (increases) the value of color with power of 4 (dPow). As it is known, HDR (High-dynamic-range) is technique in photography to reproduce a greater dynamic range (contrast), this is why we need to increase the contrast between black and white in order to achieve this HDR effect.

Leave a Reply