Friday, 15 June 2012

javascript - Pixel manipulation and canvas -



javascript - Pixel manipulation and canvas -

is there way in javascript alter alpha channels of each pixel beingness transparent (a=0) while coding pixel manipulation (meaning can still alter transparency in of alpha channels desired)?

basically, i'm doing is: given info specific image, manipulate pixel array using algorithm pixels become transparent unless satisfy condition. in case of them satisfying status want them opaque, aka alpha=1. however, because of complication way algorithm works, need have info "reset"; meaning want pixel array start off having every alpha = 0. can provide code if helps in improve understanding scope of question.

thanks much.

edit: i'm looking more method/one-line code. context.globalalpha = 0 serve purposes? there pitfall should careful about?

edit2: code. globalalpha i've set i'm expecting do? i'm not sure how utilize it...

function getboundary(imagedata){ var imagedata = new array(imagedata.data.length); imagedata = imagedata.data; var w = imagedata.width; var h = imagedata.height; var color1 = []; var colorright = []; var colordown = []; context.globalalpha = 0; (var = 0; < 4*w*h; +=4) { color1 = [imagedata[i],imagedata[i+1],imagedata[i+2]]; colorright = [imagedata[i+4],imagedata[i+5],imagedata[i+6]]; colordown = [imagedata[4*w+i],imagedata[4*w+i+1],imagedata[4*w+i+2]]; if(colorright = [255,255,255]){ //if right white if(color1 = [0,0,0]){ imagedata[i+3] = 255; } else{ if(colordown = [0,0,0]){ imagedata[4*w+i+3] = 255; } } } else{ //colorright = black if(color1 = [0,0,0]){ if(colordown = [255,255,255]){ imagedata[i+3] = 255; } } else if(color1 = [255,255,255]){ imagedata[i+7] = 255; if(colordown = [0,0,0]){ imagedata[4*w+i+3] = 255; } else{ } } } } console.log("done"); imagedata.data = imagedata; homecoming imagedata; }

you can utilize getimagedata , flip alpha elements zero:

you can create function zeros alpha of pixels on canvas this:

function zeroallalpha(){ var imagedata=context.getimagedata(0,0,canvas.width,canvas.height); var data=imagedata.data; // set alpha elements 0 (fully transparent); for(var i=3;i<data.length;i+=4){ data[i]=0; } context.putimagedata(imagedata,0,0); }

and can phone call function 1 line this:

zeroallalpha();

javascript canvas global pixel

No comments:

Post a Comment