newbie keeps losing alpha values in canvas -


i'm doing boneheaded here i'm having difficulties getting alpha values cooperate in canvas. i'm trying sample opaque color spot on canvas, make more transparent, , lay down in spot -- alpha part doesn't seem working. stripped down goes sort of (condensed functions strewn across script):

p = ground.ctx.getimagedata(loc.x, loc.y, 1, 1).data;  col = {r: p[0], g: p[1], b: p[2], a: p[3]}; col.a = col.a - 0.1; ground.ctx.fillstyle = 'rgba(' + col.r + ', ' + col.g + ', ' + col.b + ', ' + col.a + ')'; ground.ctx.fillrect(nuloc.x, nuloc.y, sqrsize, sqrsize); 

it runs, when test value of fillstyle standard rgb "#fa674c" or whatever -- no mention of alpha -- , when getimagedata() newly drawn rect value opaque again.

another thing haven't been able figure out either empirically or reading every tutorial (and spec) whether alpha wants 0-1.0 or 0-255. sources talk 0-1.0 -- getimagedata() returns 0-255... , can't make work either way.

use context.globalalpha instead of using rgba fill:

p = ground.ctx.getimagedata(loc.x, loc.y, 1, 1).data;  col = {r: p[0], g: p[1], b: p[2], a: p[3]};  // note: globalalpha uses scale of 0-1 // , getimagedata uses scale of 0-255 ground.ctx.globalalpha = a/255-.1;  ground.ctx.fillstyle = 'rgb(' + col.r + ', ' + col.g + ', ' + col.b + ')'; ground.ctx.fillrect(nuloc.x, nuloc.y, sqrsize, sqrsize);  // reset globalalpha when you're done ground.ctx.globalalpha = 1; 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -