I am creating a Canvas with an image inside using
context.drawImage(image, (cWidth - iRatio)/2, 0, iRatio, cHeight);
And behind the canvas there is a Blue Wallpaper inside a Div.
As you can see on the image (done on Photoshop), the Canvas (Image with the Sun) is fading from opacity 1 to 0.
How can I do it in Canvas with JS or CSS ? I want the Canvas edges(All, so Left, Right, Top and botoom) to have an opacity 0 and fading to center with opacity 1.
2
Answers
You can use
mask
:MDN has many example – you can use gradients, svg, and even pngs to get the effect you want.
There’s also
mask-border
but it isn’t as well supported (for example, Firefox doesn’t support it yet).Using filter
Use
ctx.filter
to create a blur and use that blur to keep pixels you want and remove pixels from edge usingctx.globalCompoiteOperation = "detination-in"
Example below uses function
featherEdge
feather edges of canvas content.