Skip to content

Instantly share code, notes, and snippets.

@rakibhasansabbir
Last active February 29, 2020 20:04
Show Gist options
  • Save rakibhasansabbir/a0b269f3acf2019d06cb4b9e1e09d886 to your computer and use it in GitHub Desktop.
Save rakibhasansabbir/a0b269f3acf2019d06cb4b9e1e09d886 to your computer and use it in GitHub Desktop.
HTML canvas rounded image draw under clipping area
// save the context in its unaltered state
ctx.save();
// fill canvas with black
ctx.fillStyle="black";
ctx.fillRect(0,0,canvas.width,canvas.height);
// create clipping region which will display portion of image
// The image will only be visible inside the circular clipping path
ctx.beginPath();
ctx.arc(100,100,100,0,Math.PI*2);
ctx.closePath();
ctx.clip();
// draw the image into the clipping region
ctx.drawImage(img,0,0);
// restore the context to its unaltered state
ctx.restore();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment