DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. HTML5 Image Effects – HDR Simulation

HTML5 Image Effects – HDR Simulation

Andrey Prikaznov user avatar by
Andrey Prikaznov
·
May. 23, 12 · Interview
Like (0)
Save
Tweet
Share
7.62K Views

Join the DZone community and get the full member experience.

Join For Free

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

HTML

Published at DZone with permission of Andrey Prikaznov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Running Databases on Kubernetes
  • Integrate AWS Secrets Manager in Spring Boot Application
  • Building the Next-Generation Data Lakehouse: 10X Performance
  • 10 Most Popular Frameworks for Building RESTful APIs

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: