oop javascript – canvas pixel manipulation

using javascript oop method, html5 canvas and input range

algorithm

1. load image to canvas

2. change value in input range

3. change pixel to white if greater than value limit else black

<!DOCTYPE html>
 
<html>
    <head>
        <title>Canvas from scratch</title>
        <meta charset="utf-8">
    </head>
    <body>

    <style>
    body {background-color:#eee;}
    canvas {
        background: #fff;
        border: 2px solid #aaa;
        box-shadow: 0 3px 10px #aaa;
        display: block;
        margin: 50px auto;
    }
    #imgGrid{
        width:800px;
        overflow:auto;
        margin:10px auto;
        
    }
    #imgGrid img{
        display:block;
        width:75px;
        height:75px;
        float:left;
        cursor:pointer;
    }
    </style>

<script type="text/javascript" src='js/canvas.js'></script>
<script>

    var canvas = new Canvas(800, 400);
    var imgData = undefined;
    document.body.appendChild(canvas.canvas);
    
    var grid = document.createElement("div");
    grid.id = "imgGrid";
    document.body.appendChild(grid);
    
    for (var i=0; i<7; i++){
        var img = document.createElement("img");
        img.src = "img/no-0"+i+".jpg";
        img.onclick = function(){
            canvas.loadImage(this);
            imgData = this;
        }
        grid.appendChild(img);
    }
function outputUpdate(vol) {
    canvas.loadImage(imgData); // load ulang image
    document.querySelector('#volume').value = vol; // update output text 
    canvas.blackwhite(vol); // 
}
</script>
<center>
<input id="bw-range" type="range" min=0 value=10 max=255 step=1 onchange="outputUpdate(value)"/>
<output id=volume>50</output>
</center>
    </body>
</html>



 

class javascript

var Canvas = function(width, height){
    this.canvas = document.createElement('canvas');
    this.canvas.width = width;
    this.canvas.height = height;

    this.ctx = this.canvas.getContext("2d");
}

Canvas.prototype.loadImage = function(path) {
    htmlNode.appendChild(this.canvas);
};

Canvas.prototype.loadImage = function(img) {
    this.ctx.clearRect ( 0 , 0 , this.canvas.width, this.canvas.height );
    //this.ctx.drawImage(img,0,0, this.canvas.width, this.canvas.height);
    this.ctx.drawImage(img,0,0);
};
 
Canvas.prototype.getPixelData = function(){
    return this.ctx.getImageData(0,0, this.canvas.width, this.canvas.height);
}

/**
 * filter black n white
 * input 
 */

Canvas.prototype.blackwhite = function (limit){    
    var imgData = this.ctx.getImageData(0, 0, this.canvas.width, this.canvas.height);    
    var pixels  = imgData.data;    
    var white    = 255;    
    var black    = 0;    
    for (var i = 0, n = pixels.length; i < n; i += 4) {        
        if( pixels[i] < limit || pixels[i+1] < limit || pixels[i+2] < limit) {        
            pixels[i  ] = black;        // red            
            pixels[i+1] = black;        // green            
            pixels[i+2] = black;        // blue        
            //pixels[i+3]              is alpha
        } else {
            pixels[i  ] = white;        // red        
            pixels[i+1] = white;        // green            
            pixels[i+2] = white;        // blue            
            //pixels[i+3]              is alpha
        }
    }    
    //redraw the image in black & white
    this.ctx.putImageData(imgData, 0, 0);    
}

example,

filter image with limit 60

javascript-pixel-manipulation-limit-60filter image with limit 127

javascript-pixel-manipulation-limit-127

http://easylearntutorial.com/series/watch/40/image-processing-tutorials-computer-vision

http://www.developerdrive.com/2012/07/creating-a-slider-control-with-the-html5-range-input/

http://www.htmlgoodies.com/html5/javascript/display-images-in-black-and-white-using-the-html5-canvas.html

http://andipublisher.com/produk-0513004726-teori-dan-aplikasi-pengolahan-citracd.html

Published by

G3n1k

just to remember what i had known :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s