{{ !articles[0].partner.isSponsoringArticle ? "Platinum" : "Portal" }} Partner

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="https://www.google.com/jsapi"></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 < 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);

    // 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 {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks