javascript - How to consider alpha channel(opacity) for canvas blending(context.globalCompositeOperation) -
we have following sample code blend 2 images global composition.
canvas blending works perfect. need detect formulas/algorithms make operation imagedatas , same result.
image.src = "landscape.png"; image.onload = function() { img = new image; img.src = "gradient.png"; img.onload = function(){ canvas.width = img.width; canvas.height = img.height; context.save(); context.drawimage(image, 0, 0, canvas.width, canvas.height); drawcustomlogo(context); var id1 = getcontextfromimg(image); var id2 = getcontextfromimg(img); var b = new blending(); var sid = b.overlay(id1, id2); var bc = document.getelementbyid('custom'); bc.width = image.width; bc.height = image.height; var bctx = bc.getcontext('2d'); bctx.putimagedata(sid, 0, 0); }
but problem in processing images alpha channel. tried following algorithms:
(target > 0.5) (1 - (1-2*(target-0.5)) * (1-blend)) + (target <= 0.5) ((2*target) * blend)
and
dst[px ] = (dra<=0.5) ? (2*src[px ]*dra/da) : 255 - (2 - 2*dra/da) * (255-src[px ]); dst[px+1] = (dga<=0.5) ? (2*src[px+1]*dga/da) : 255 - (2 - 2*dga/da) * (255-src[px+1]); dst[px+2] = (dba<=0.5) ? (2*src[px+2]*dba/da) : 255 - (2 - 2*dba/da) * (255-src[px+2]);
but result different. need find strict overlay blending algorithm
looks found right decision. best explanation stores on w3c
general formula blending filter is
color(red) = (1 - alphablending)*sourcecolor(red) + alphablending*func(colorblending(red),colorsource(red)); .... // continue other color
so blending functions should used general formula compositing , blending
Comments
Post a Comment