Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas clipping with "feather" edges effect

Tags:

People also ask

What is feathering in Photoshop?

Feathering is a way to soften the hard edges of an object in your image. By gradually fading between the colors of the pixels on the edge and the pixels surrounding it, feathering can help you make a newly added element blend in with the rest of your image.

How do I feather a shape in Photoshop?

Double-click the Marquee or Lasso tools to view the Tool Options panel. Select Feather from the Tool Options pop-up and type a value to set the feather radius. With a selection tool such as the marquee, lasso, or magic wand, select pixels within the image.


I'm currently drawing an image to an HTML5 Canvas and masking it with an arc, calling clip() before I draw the image so that only the portion that's in the arc is shown. How can I feather the edges of this arc? I know from googling around that there is no simple way to simply apply a "feather" to a shape drawn with canvas. What abut going in on the pixel data for the image where its edges touch the arc? Thanks for any help.

Here is the relevant portion of my code:

ctx.arc(canvas.width/2, canvas.height/2, 250, 0, 6.28, false);//draw the circle
ctx.restore();
ctx.save();
ctx.drawImage(background, 0, 0,
              background.width * scale, background.height * scale);
ctx.clip();//call the clip method so the next render is clipped in last path
ctx.drawImage(img, 0, 0,
              img.width * scale, img.height * scale);
ctx.closePath();
ctx.restore();

UPDATE

Thanks for the thorough answer and very helpful code/comments Ken!! I spent a few hours last night trying to work this solution in my particular use case and I'm having trouble. It seems that if I clip an image with the second-canvas technique you describe I can't redraw it on transforms the same way that I can with an arc() and clip() routine. Here's a JS Fiddle of what I'm trying to accomplis, minus the feathering on the arc, notice the click and drag events on the two layered images.

http://jsfiddle.net/g3WkN/

I tried replacing the arc() with your method, but I'm having a hard time getting that to be responsive to the transforms that happen on mouse events.