Skip to content Skip to sidebar Skip to footer

How To Convert Image Pixel Into )|( -curve Shape In Canvas

I have an image with the following pattern: I want to convert the straight lines in the image into a )|( shape. I have implemented C-type and S-type curves using canvas and its pr

Solution 1:

Use the distance from the horizontal center to apply a sin wave to the image scale. You only want part of the sin wave (PI/2 to PI) or (cos 0 to PI/2)

var img = new Image;
img.src = "//i.stack.imgur.com/UvqUP.gif";
var ctx = can.getContext("2d");
img.onload = function () {
    var w,h,ch,cw,amount,x,scale;
    w = can.width = this.width;
    h = can.height = this.height;
    cw = w / 2;
    ch = h / 2;
    amount = 50;  // amount to bend in pixels
    amount *= 1 / ch;  // convert to unit scale
    for(var x = 0; x < w; x++) {
    	scale = 1.0 - (Math.cos(((cw - x) / cw) * Math.PI * 0.5)) * amount;
        ctx.drawImage(this, x, 0, 1, h, x, ch - ch * scale , 1, h * scale); 
    }
}
<canvas id="can"></canvas>

Post a Comment for "How To Convert Image Pixel Into )|( -curve Shape In Canvas"